Click here to Skip to main content
6,822,123 members and growing! (17,387 online)
Email Password   helpLost your password?
Enterprise Systems » SharePoint Server » Web Parts License: The Code Project Open License (CPOL)

Custom Content Editor Web Part for SharePoint

By AdamNThompson

This Custom Content Editor Web Part filters content using Forms Based Authentication
C#.NET2.0
Posted:23 Dec 2008
Views:15,794
Bookmarked:15 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 2.39 Rating: 5.00 out of 5

1

2

3

4
3 votes, 100.0%
5

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


Member
I am a .NET Developer for company that builds custom web applications. It's an interesting job because each site is different. We build anything that the mind can imagine, and the client can afford.

Fun stuff... Smile

CP is my favorite site for code samples, news, and articles. I like the community here and I like the fact that it mainly caters to developers using the .NET platform.
Occupation: Web Developer
Location: United States United States

Other popular SharePoint Server articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralGreat but Pinmemberchill_out_again1:30 25 Aug '09  
GeneralRe: Great but PinmemberAdamNThompson6:45 25 Aug '09  
Generalthts Great PinmemberSGhouse5:01 13 Jul '09  
GeneralRe: thts Great PinmemberAdamNThompson6:00 13 Jul '09  
GeneralRe: thts Great PinmemberSGhouse6:34 13 Jul '09  
GeneralRe: thts Great PinmemberAdamNThompson6:44 13 Jul '09  
GeneralGreat! PinmembernEo.X18:24 24 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 23 Dec 2008
Editor: Deeksha Shenoy
Copyright 2008 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project