Introduction
As you may know, SharePoint can be made to work with Forms Base 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.
This code shows how to build your own Content Editor Web Part, and build custom properties for it by inheriting from the EditorPart
class. You may wonder why not just inherit the Content Editor Web Part itself. Well it’s a sealed class, but luckily the code to accomplish this task is still surprisingly compact. In this case, I needed the Content Editor Web Part to target the content by FBA Role but the ability to build these custom Properties into SharePoint web Parts can be useful in many different ways.
This code shows examples of using a Rich Text Editor and a ListBox
as Web Part Properties in Share Point. This is useful because the controls can only be reached by user with permissions to edit a page in SharePoint, and you can make use of saving date to the SharePoint Data Base abstractly with context to that Web Part.
I am not as big on long drawn out articles as I am on clear and concise code samples. So here it is. If you have any questions just post them below.
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;
}
}
}
}
}
History
- 23rd December, 2008: Initial post