Update people field
The following code sample add a new member to the SharePoint 2013 Community Site Member list. It update the "Member" field.
using (SPSite site = new SPSite("http://siteurl/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList members = web.Lists.TryGetList("Community Members");
SPListItem newMember = members.AddItem();
SPUser ensuredUser2 = web.EnsureUser("domain\\loginName");
SPFieldUserValue userValue = new SPFieldUserValue(web, ensuredUser2.ID, ensuredUser2.LoginName);
newMember["Member"] = userValue;
newMember.Update();
}
}
using (SPSite site = new SPSite("http://siteurl/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList members = web.Lists.TryGetList("Community Members");
SPListItem newMember = members.AddItem();
SPUser ensuredUser2 = web.EnsureUser("domain\\loginName");
SPFieldUserValue userValue = new SPFieldUserValue(web, ensuredUser2.ID, ensuredUser2.LoginName);
newMember["Member"] = userValue;
newMember.Update();
}
}
Comments