Click here to Skip to main content
Click here to Skip to main content

Custom Content Editor Web Part for SharePoint

By , 23 Dec 2008
 

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AdamNThompson
Web Developer
United States United States
No Biography provided
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionconflictsmemberTin Nay Lar2-Apr-12 18:06 
AnswerRe: conflictsmemberAdamNThompson3-Apr-12 2:57 
GeneralMy vote of 1memberluismiguel.gamiz11-Apr-11 23:46 
Generalthe menu don´t show export option [modified]memberdromero8822-Feb-10 2:19 
GeneralRe: the menu don´t show export optionmemberAdamNThompson25-Feb-10 4:05 
GeneralRe: the menu don´t show export optionmemberdromero8825-Mar-10 3:34 
GeneralGreat butmemberchill_out_again25-Aug-09 0:30 
GeneralRe: Great butmemberAdamNThompson25-Aug-09 5:45 
GeneralRe: Great butmemberradhika.sajin17-Feb-10 18:04 
GeneralRe: Great butmemberAdamNThompson18-Feb-10 4:42 
Generalthts GreatmemberSGhouse13-Jul-09 4:01 
GeneralRe: thts GreatmemberAdamNThompson13-Jul-09 5:00 
GeneralRe: thts GreatmemberSGhouse13-Jul-09 5:34 
GeneralRe: thts GreatmemberAdamNThompson13-Jul-09 5:44 
GeneralGreat!membernEo.X24-Dec-08 17:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 23 Dec 2008
Article Copyright 2008 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid