Search This Blog

Saturday, May 23, 2015

Creating custom SharePoint people picker control


1) First you need to create control inherited from Microsoft.SharePoint.WebControls.PeopleEditor class

public class MyPeopleEditor : PeopleEditor
{
public string Department { get; set; }
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
PickerDialogType = typeof(MyPeoplePickerDialog);
}
public override PickerEntity ValidateEntity(PickerEntity entity)
{
entity.IsResolved = string.IsNullOrEmpty(Department) || ((entity.EntityData["Department"] as string) == Department);
return entity;
}
}
In created control you should override OnInit and ValidateEntity functions. In OnInit you should set PickerDialogType to your custom dialog class, in ValidateEntity you should implement your custom validation logic. In my example I just check if user is from proper department.


2)  Then you need to create custom people picker dialog class inherited from Microsoft.SharePoint.WebControls.PickerDialog

public class MyPeoplePickerDialog : PickerDialog
{
public MyPeoplePickerDialog()
: base(new MyUserQueryControl(), new TableResultControl(), new MyPeopleEditor())
{
ArrayList columnDisplayNames = ((TableResultControl)base.ResultControl).ColumnDisplayNames;
columnDisplayNames.Clear();
columnDisplayNames.Add(“Name”);
columnDisplayNames.Add(“Department”);
columnDisplayNames.Add(“Job Title”);
ArrayList columnNames = ((TableResultControl)base.ResultControl).ColumnNames;
columnNames.Clear();
columnNames.Add(“Name”);
columnNames.Add(“Department”);
columnNames.Add(“JobTitle”);
ArrayList columnWidths = ((TableResultControl)base.ResultControl).ColumnWidths;
columnWidths.Clear();
columnWidths.Add(“40%”);
columnWidths.Add(“30%”);
columnWidths.Add(“30%”);
}
}

In this class you should implement only construction, inherited from constructor of base class. Note, that you need to pass several controls in base construction – first is your custom query control, second is result control (I use standard TableResultControl in my example), third is your custom people control, that you’ve created earlier. Constructor also contains code for TableResultControl initialization.

3) The last step is to create custom query control, inherited from SimpleQueryControl. This control will implement custom business logic for people picker dialog populating.

public class MyUserQueryControl : SimpleQueryControl
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
mColumnList.Visible = false;
if (!Page.IsPostBack)
{
IssueQuery(string.Empty, string.Empty, 0, 0);
}
}
DataTable ConvertProfilesToDataTable(IEnumerable profiles)
{
DataTable table = new DataTable();
table.Columns.Add(“Name”);
table.Columns.Add(“Department”);
table.Columns.Add(“AccountName”);
table.Columns.Add(“JobTitle”);
table.Columns.Add(“Email”);
table.Columns.Add(“SIPAddress”);
table.Columns.Add(“WorkPhone”);
foreach (UserProfile profile in profiles)
{
DataRow row = table.NewRow();
row["Name"] = profile.DisplayName;
row["Department"] = profile[PropertyConstants.Department].Value;
row["AccountName"] = profile[PropertyConstants.AccountName].Value;
row["JobTitle"] = profile[PropertyConstants.JobTitle].Value;
row["Email"] = profile[PropertyConstants.WorkEmail].Value;
row["SIPAddress"] = profile[PropertyConstants.SipAddress].Value;
row["WorkPhone"] = profile[PropertyConstants.WorkPhone].Value;
table.Rows.Add(row);
}
return table;
}
protected IEnumerable SearchProfiles(string search, string department)
{
SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Site);
UserProfileManager profileManager = new UserProfileManager(context);

string[] patterns = (from p in (new string[] {search, department})
where !string.IsNullOrEmpty(p)
select p).ToArray();
IEnumerable res = null;
if (patterns.Count() > 0)
{
//execute query
ProfileBase[] searchResult = profileManager.Search(patterns);
res = from p in searchResult
where (p is UserProfile) &&
(string.IsNullOrEmpty(department)||
(((p as UserProfile)[PropertyConstants.Department].Value as string) == department))
select (p as UserProfile);
}
else
{
//get all profiles
List profiles = new List();
foreach (UserProfile profile in profileManager)
{
profiles.Add(profile);
}
res = profiles;
}
return res;
}
protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize)
{
List pickerEntities = new List();
SPWebApplication application = SPContext.Current.Site.WebApplication;
MyPeopleEditor editor = PickerDialog.EditorControl as MyPeopleEditor;
DataTable tablePrinciples = ConvertProfilesToDataTable(SearchProfiles(search, editor.Department));
PickerDialog.Results = tablePrinciples;
PickerDialog.ResultControl.PageSize = tablePrinciples.Rows.Count;
return tablePrinciples.Rows.Count;
}

public override PickerEntity GetEntity(DataRow dr)
{
PickerEntity entity = new PickerEntity();
entity.DisplayText = dr["Name"] as string;
entity.Key = dr["AccountName"] as string;
entity.EntityData["Department"] = dr["Department"] as string;
entity.EntityData["JobTitle"] = dr["JobTitle"] as string;
entity.EntityData["Email"] = dr["Email"] as string;
entity.EntityData["SIPAddress"] = dr["SIPAddress"] as string;
entity.EntityData["WorkPhone"] = dr["WorkPhone"] as string;
entity.IsResolved = true;
return entity;
}
}

No comments:

Post a Comment