Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Forms Based Authentication Filtered Content Editor for SharePoint


By Adam Thompson
Printer Friendly Version
View My Articles
37 Views
    

As you may know SharePoint can be made to work with Forms Based Authentication. While this eases the task of creating custom registration, Target Audiences won’t work with Forms Based Authentication. In my case the client wanted to be able to use the Content Editor Web Part to manage their own content and then make it accessible by authentication. Since Forms Based Authentication will not work with Target Audiences I wrote a Custom Content Editor Web Part that will filter content by FBA Role.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using FreeTextBoxControls;
using System.Collections;

namespace Everpure.SharePoint.Registration.Controls
{
    public class FbaFilteredContentEditor : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable 
    { 
        private string _content;  
        [WebBrowsable(false)]        
        [Personalizable(PersonalizationScope.Shared)] 
        public string Content 
        { 
            get { return _content; } 
            set { _content = value; } 
        }

        private ArrayList _fbaRoles = new ArrayList();
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public ArrayList FbaRoles
        {
            get { return _fbaRoles; }
            set { _fbaRoles = value; }
        }

        protected override void Render(HtmlTextWriter writer) 
        { 
            base.Render(writer); 
            
            string userName = System.Web.HttpContext.Current.User.Identity.Name;
            string[] userRoles = System.Web.Security.Roles.GetRolesForUser(userName);

            int x = 0;

            foreach (object role in FbaRoles)
            {
                foreach (string s in userRoles)
                {
                    if (role.ToString() == s.ToString())
                    {
                        x += 1;
                    }
                }
            }

            if (x != 0)
            {
                writer.WriteLine(string.Format("{0}", Content));
            }
        }

        EditorPartCollection IWebEditable.CreateEditorParts() 
        { 
            List<EditorPart> editors = new List<EditorPart>(); 
            editors.Add(new MyEditorPart(this.ID));
            editors.Add(new MyListBoxPart(this.ID));
            return new EditorPartCollection(editors); 
        } 

        object IWebEditable.WebBrowsableObject 
        { 
            get { return this; } 
        } 
    }

    public class MyEditorPart : EditorPart 
    { 
        private FreeTextBox _message = new FreeTextBox();
        

        public MyEditorPart(string webPartID) 
        { 
            this.ID = "MyEditorPart" + webPartID; 
        }
        
        protected override void CreateChildControls() 
        { 
            base.CreateChildControls(); 
            _message = new FreeTextBox(); 
            Controls.Add(_message); 
        } 

        public override bool ApplyChanges() 
        { 
            EnsureChildControls(); 
            FbaFilteredContentEditor webPart = WebPartToEdit as FbaFilteredContentEditor; 
            
            if (webPart != null) 
            { 
                webPart.Content = _message.Text; 
            } 
            
            return true; 
        } 
        
        public override void SyncChanges() 
        { 
            EnsureChildControls(); 
            FbaFilteredContentEditor webPart = WebPartToEdit as FbaFilteredContentEditor; 
            
            if (webPart != null) 
            { 
                _message.Text = webPart.Content; 
            } 
        } 
    }

    public class MyListBoxPart : EditorPart
    {
        private ListBox _roles;

        public ListBox RolesListBox
        {
            get { return _roles; }
        }
            
        public MyListBoxPart(string webPartID)
        {
            this.ID = "MyListBoxPart" + webPartID;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            _roles = new ListBox();
            _roles.SelectionMode = ListSelectionMode.Multiple;
            _roles.Height = 400;
            _roles.Width = 300;

            string[] _fbaRoles = System.Web.Security.Roles.GetAllRoles();

            foreach (string role in _fbaRoles)
            { 
                _roles.Items.Add(role.ToString());
            }

            Controls.Add(_roles);
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            FbaFilteredContentEditor webPart = WebPartToEdit as FbaFilteredContentEditor;

            if (webPart != null)
            {
                ArrayList _fbaRoles = new ArrayList();

                foreach (ListItem selectedItem in _roles.Items)
                {
                    if (selectedItem.Selected == true)
                    { 
                        _fbaRoles.Add(selectedItem.ToString());
                    }
                }

                webPart.FbaRoles = _fbaRoles;
            }

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            FbaFilteredContentEditor webPart = WebPartToEdit as FbaFilteredContentEditor;

            if (webPart != null)
            {
                ArrayList _fbaRoles = webPart.FbaRoles;

                foreach (object item in _fbaRoles)
                {
                    _roles.Items.FindByText(item.ToString()).Selected = true;
                }
            }
        }
    }
}


button
Article Discussion: Forms Based Authentication Filtered Content Editor for SharePoint
Adam Thompson posted at Tuesday, December 23, 2008 10:35 AM
Original Article