Click here to Skip to main content
12,300,379 members (52,111 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

65.7K views
21 bookmarked
Posted

Custom Content Editor Web Part for SharePoint

, 23 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This Custom Content Editor Web Part filters content using Forms Based Authentication

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)

Share

About the Author

AdamNThompson
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionconflicts Pin
Tin Nay Lar2-Apr-12 18:06
memberTin Nay Lar2-Apr-12 18:06 
AnswerRe: conflicts Pin
AdamNThompson3-Apr-12 2:57
memberAdamNThompson3-Apr-12 2:57 
GeneralMy vote of 1 Pin
luismiguel.gamiz11-Apr-11 23:46
memberluismiguel.gamiz11-Apr-11 23:46 
Generalthe menu don´t show export option [modified] Pin
dromero8822-Feb-10 2:19
memberdromero8822-Feb-10 2:19 
GeneralRe: the menu don´t show export option Pin
AdamNThompson25-Feb-10 4:05
memberAdamNThompson25-Feb-10 4:05 
GeneralRe: the menu don´t show export option Pin
dromero8825-Mar-10 3:34
memberdromero8825-Mar-10 3:34 
GeneralGreat but Pin
chill_out_again25-Aug-09 0:30
memberchill_out_again25-Aug-09 0:30 
GeneralRe: Great but Pin
AdamNThompson25-Aug-09 5:45
memberAdamNThompson25-Aug-09 5:45 
GeneralRe: Great but Pin
radhika.sajin17-Feb-10 18:04
memberradhika.sajin17-Feb-10 18:04 
GeneralRe: Great but Pin
AdamNThompson18-Feb-10 4:42
memberAdamNThompson18-Feb-10 4:42 
Generalthts Great Pin
SGhouse13-Jul-09 4:01
memberSGhouse13-Jul-09 4:01 
GeneralRe: thts Great Pin
AdamNThompson13-Jul-09 5:00
memberAdamNThompson13-Jul-09 5:00 
GeneralRe: thts Great Pin
SGhouse13-Jul-09 5:34
memberSGhouse13-Jul-09 5:34 
GeneralRe: thts Great Pin
AdamNThompson13-Jul-09 5:44
memberAdamNThompson13-Jul-09 5:44 
GeneralGreat! Pin
nEo.X24-Dec-08 17:24
membernEo.X24-Dec-08 17:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 23 Dec 2008
Article Copyright 2008 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid