Monday, April 27, 2009

WebParts and Audiences - Part 2: Create a custom ToolPart/EditorPart to configure audiences for your custom WebPart

This is part 2 of a series I will write on how to deal with audiences in your custom SharePoint WebParts.

In this post, I will show you how to create a custom ToolPart/EditorPart for your WebPart that allows to configure audience settings for the WebPart. In my example I've implemented to possible settings:

  • Audience targeting for the whole WebPart (if a user is not in the right audience the WebPart will be invisible --> see Part 1)
  • Audience targeting for the data items that are showed in the WebPart (see in a later part of the series).

In order to allow the user to pick the audiences, I am using the standard-SharePoint AudienceEditor web control:

_wpAudiences.Types = AudienceEditor.AudienceType.DL | AudienceEditor.AudienceType.GlobalAudience | AudienceEditor.AudienceType.SharePointGroup;
_wpAudiences.Visible = true;
_wpAudiences.Width = Unit.Pixel(0x182);
this.AddConfigurationOption("WebPart Audiences", "Members of these audiences can view the Web Part. If left blank, everyone can see the Web Part",
_wpAudiences);

here is full implementation:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebPartPages;

namespace YourNameSpace
{

/// Base class for all custom ToolParts.

public class BaseToolPart : ToolPart, INamingContainer
{

#region Overridden Members


/// Called if a user has commited a configuration change.

public override void ApplyChanges()
{
this.OnAppliedChanges(EventArgs.Empty);
base.ApplyChanges();
}

#endregion

#region Protected Members
/// Creates a label and a corresponding control.



///
protected virtual void AddConfigurationOption(string title, Control inputControl)
{
this.AddConfigurationOption(title, null, inputControl);
}

///
/// Creates a label and a corresponding control.

protected virtual void AddConfigurationOption(string title, string description, Control inputControl)
{
this.AddConfigurationOption(title, description, new List(new Control[]{inputControl}));
}

///
/// Creates a label and a corresponding control.

protected virtual void AddConfigurationOption(string title, string description, IEnumerable inputControls)
{
HtmlGenericControl divSectionHead = new HtmlGenericControl("div");
divSectionHead.Attributes.Add("class", "UserSectionHead");
this.Controls.Add(divSectionHead);

HtmlGenericControl labTitle = new HtmlGenericControl("label");
labTitle.InnerHtml = HttpUtility.HtmlEncode(title);
divSectionHead.Controls.Add(labTitle);

HtmlGenericControl divUserSectionBody = new HtmlGenericControl("div");
divUserSectionBody.Attributes.Add("class", "UserSectionBody");
this.Controls.Add(divUserSectionBody);

HtmlGenericControl divUserControlGroup = new HtmlGenericControl("div");
divUserControlGroup.Attributes.Add("class", "UserControlGroup");
divUserSectionBody.Controls.Add(divUserControlGroup);

if (!string.IsNullOrEmpty(description))
{
HtmlGenericControl spnDescription= new HtmlGenericControl("div");
spnDescription.InnerHtml = HttpUtility.HtmlEncode(description);
divUserControlGroup.Controls.Add(spnDescription);
}

foreach (Control inputControl in inputControls)
{
divUserControlGroup.Controls.Add(inputControl);
}

HtmlGenericControl divUserDottedLine = new HtmlGenericControl("div");
divUserDottedLine.Attributes.Add("class", "UserDottedLine");
divUserDottedLine.Style.Add(HtmlTextWriterStyle.Width, "100%");
this.Controls.Add(divUserDottedLine);
}

#endregion

#region Events & Handlers
///
/// Fires after a user has commited a configuration change.
///
public event EventHandler AppliedChanges;

///
/// Called after a user has commited a configuration change (ApplyChanges).
///
protected virtual void OnAppliedChanges(EventArgs e)
{
if (this.AppliedChanges != null)
this.AppliedChanges(this, e);
}

#endregion
}
}

-----------------------------------------------------------

using System.Web.UI.WebControls;
using Microsoft.Office.Server.WebControls;

namespace YourNameSpace
{
public class AudiencesToolPart : BaseToolPart
{

#region Constants and Private Members
private AudienceEditor _wpAudiences = new AudienceEditor();
private CheckBox _chkFilterContentByAudience = new CheckBox();
private bool _showWebPartAudiences = true;
private bool _showFilterContentByAudiences = true;

#endregion

public AudiencesToolPart()
{
this.Title = "Audiences";
}

#endregion

#region Overridden members

protected override void CreateChildControls()
{
if (_showWebPartAudiences)
{
_wpAudiences.Types = AudienceEditor.AudienceType.DL | AudienceEditor.AudienceType.GlobalAudience | AudienceEditor.AudienceType.SharePointGroup;
_wpAudiences.Visible = true;
_wpAudiences.Width = Unit.Pixel(0x182);
this.AddConfigurationOption("WebPart Audiences", "Members of these audiences can view the Web Part. If left blank, everyone can see the Web Part",
_wpAudiences);
}

if (_showFilterContentByAudiences)
{
this._chkFilterContentByAudience.ID = "chkContentFiltering";
this.AddConfigurationOption("Content filtering", "If checked, audience-filtering will be applied to the contents of this Web Part",
this._chkFilterContentByAudience);
}

base.CreateChildControls();
}


#endregion

#region Public Members

public bool FilterContentByAudience
{
get
{
return this._chkFilterContentByAudience.Checked;
}
set
{
this._chkFilterContentByAudience.Checked = value;
}
}

public string WebPartAudiences
{
get
{
return this._wpAudiences.Text;
}
set
{
this._wpAudiences.Text = value;
}
}


public bool ShowWebPartAudiences
{
get { return this._showWebPartAudiences; }
set { this._showWebPartAudiences = value; }
}

public bool ShowFilterContentByAudiences
{
get { return this._showFilterContentByAudiences; }
set { this._showFilterContentByAudiences = value; }
}
#endregion

}
}







Post a Comment

Blog Popularty Partners

  ©All Right Reserved.

Back to TOP