How can I retrieve value of SharePoint:PeopleEditor
control in a string programmatically in Server Side Object Model (C#)?
2 Answers
Sample ASP control:
<SharePoint:PeopleEditor runat="server" ID="spOwner" />
Sample C# code for update item based on this control:
string[] users= spOwner.CommaSeparatedAccounts.Split(',');
SPFieldUserValueCollection usersSPList = new SPFieldUserValueCollection();
foreach (string user in users)
{
try
{
web.EnsureUser(user);
SPUser usersSP = web.SiteUsers[user];
SPFieldUserValue usersSPValue = new SPFieldUserValue(web, usersSP.ID, usersSP.LoginName);
usersSPList.Add(usersSPValue);
}
catch (SPException)
{
// your code
}
}
itemList[fieldOwner.Id] = usersSPList;
itemList.Update();
Considering your control is multi selection allowed. peUser
is your SharePoint:PeopleEditor
control.
Below code will do the work
string selectedUser = String.Empty;
if (peUser.IsValid)
{
for (int index = 0; index <= peUser.ResolvedEntities.Count - 1; ++index)
{
PickerEntity entity = (PickerEntity)peUser.ResolvedEntities[index];
if (String.IsNullOrEmpty(selectedUser))
selectedUser += entity.Key;
else
selectedUser += ";" + entity.Key;
}
}
-
Gaurrav, I appreciate your response but I am looking for single select value bcoz I have disabled multi-select and that is my requirement.– Zakir HCCommented Sep 20, 2016 at 12:11
-
1