Programatically add a user to people web part
-
30-01-2021 - |
Question
I'm trying to programatically(c#) add a user to a sharepoint People web part on a modern page but with no success.
I try to set the "persons"-property the same way it looks after adding a person manually.
var peopleComp = page.InstantiateDefaultWebPart(DefaultClientSideWebParts.People);
dynamic json = JObject.Parse(peopleComp.PropertiesJson.ToString());
json["persons"] = "[{\"id\":\"i:0#.f|membership|user@mail.com\",\"upn\":\"user@mail.com\",\"role\":\"IT\",\"department\":\"IT\",\"phone\":\"\",\"sip\":\"\"}]";
page.AddControl(peopleComp);
page.Save(model.Url);
I get no errors but the control is added with no user.
Anyone knows how to do this?
Solution
var peopleComp = page.InstantiateDefaultWebPart(DefaultClientSideWebParts.People);
if (peopleComp != null)
{
peopleComp.Title = "people";
peopleComp.PropertiesJson = "{'layout':1, 'persons':[{'id':'i:0#.f|membership|USERMAIL'}],'title':''}";
page.AddControl(peopleComp);
}
page.Save(your site url)
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange