Create People Picker at run-time using SharePoint C# code, Programmatically.
Code:
protected void CreatePeoplePicker()
{
permissionPplPicker = new PeopleEditor();
permissionPplPicker.MultiSelect = true;
permissionPplPicker.AllowEmpty = false;
permissionPplPicker.AutoPostBack = true;
permissionPplPicker.PlaceButtonsUnderEntityEditor = true;
permissionPplPicker.ID = "PeopleEditor";
permissionPplPicker.SelectionSet = "User,SPGroup";
permissionPplPicker.Rows = 1;
permissionPplPicker.BorderStyle = BorderStyle.Outset;
LiteralControl styleControl = new LiteralControl();
styleControl.Text = "<style type='text/css'>.ms-inputuserfield{ font-size:8pt; font-family:Verdana,sans-serif; border:1px solid #a5a5a5;} div.ms-inputuserfield a{color:#000000;text-decoration: none;font-weight:normal;font-style:normal;} div.ms-inputuserfield{padding-left:1px;padding-top:2px;}</style> ";
this.Controls.Add(styleControl);
this.Controls.Add(permissionPplPicker);
}
Create People picker SharePoint Application page .aspx Page ,
Code:
<table style="MARGIN-TOP: 8px" class='ms-formtable' border='0' cellspacing='0' cellpadding='0' width="100%">
<tr>
<td class='ms-formlabel' valign='top' width='190' nowrap>
<h3 class='ms-standardheader'>
<nobr>People picker control<span class='ms-formvalidation' title="This is Required filed."> *</span></nobr>
</h3>
</td>
<td class='ms-formbody' valign='top'><span dir='none'>
<SharePoint:PeopleEditor ID=" pplPicker " runat="server" AutoPostBack="false" MultiSelect="false" MaximumEntities="1" ValidatorEnabled="true" SelectionSet="User" CssClass="ms-long" Width="99%" EnableViewState="true" />
<SharePoint:InputFormRequiredFieldValidator ID="ADValidator" runat="server" Display="Dynamic" SetFocusOnError="true"
ControlToValidate="pplPicker" BreakBefore="true" ErrorMessage="You must specify a value for this required field." />
</span></td>
</tr></table>
Get SPUser or SharePoint web user from People picker Programmatically C#
Code:
SPUser oUser = null;
if (pplPicker.Entities.Count > 0)
{
PickerEntity ppl = (PickerEntity) pplPicker.Entities[0];
oUser = SPContext.Current.Web.EnsureUser(ppl.DisplayText);
}
For more than one User we can iterate pplPicker.Entities collection in foreach loop.
Or you can use ResolvedEntities in place of .Entities
Save or update people picker User / SPUser programmatically in/to SharePoint List Item
Code:
using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb oWeb = oSite.OpenWeb("//Site Relative Url//", false))
{
try
{
SPListItem user = oWeb.Lists["User List"].GetItemById(Convert.ToInt32(ItemID))
user["Team Lead"] = oUser;
adminWeb.AllowUnsafeUpdates = true;
user.Update();
}
catch(SPException ex){ }
finally
{
adminWeb.AllowUnsafeUpdates = false;
adminWeb.Dispose();
}
}
}
Code:
SPListItem oItem = oWeb.Lists["User List"].GetItemById(Convert.ToInt32(ItemID));
SPUser oUser = GetSPUser(oItem, "User");
protected SPUser GetSPUser(SPListItem item, string user)
{
SPUser oUser = null;
SPFieldUserValue userValue = new SPFieldUserValue(item.Web, Convert.ToString(item[user]));
if (userValue != null)
{
oUser = userValue.User;
}
return oUser;
}
No comments:
Post a Comment