0

How can I retrieve value of SharePoint:PeopleEditor control in a string programmatically in Server Side Object Model (C#)?

2 Answers 2

1

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();
0
0

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;
                }
            }
2
  • 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 HC
    Commented Sep 20, 2016 at 12:11
  • 1
    It will work in both case
    – Gaurravs
    Commented Sep 20, 2016 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.