SOLVED

Adding People to the People Web Part Programmatically (using PowerShell maybe?)

Brass Contributor

There doesn't seem to be any documentation relating to the modern web part components.

 

I can add a modern web part component to the page like this:

 

Add-PnPClientSideWebPart -Page $home -DefaultWebPartType People

But I cannot work out how to actually add people to it.

 

Any thoughts greatfully received (although I don't hold out much hope -- it's very quiet around here).

 

6 Replies
best response confirmed by VI_Migration (Silver Contributor)
Solution

Hi @Darren Parkinson,

 

I ran Get-PnPProvisioning Template before adding a people web part

 

Then I added a people web part and then ran Get-PnPProvisioningTemplate again

 

The difference is :

 

<pnp:CanvasControl WebPartType="People" JsonControlData="{&quot;layout&quot;:1,&quot;persons&quot;:[{&quot;id&quot;:&quot;i:0#.f|membership|pieter@mytenant.onmicrosoft.com&quot;,&quot;role&quot;:&quot;&quot;}]}" ControlId="7f718435-ee4d-431c-bdbf-9c4ff326f46e" Column="1" />

 

On the Add-PnPClientSideWebPart I couldn't find anything to add the data, but you could create a template whcih just includes the  above webpart details and then add the web part with Apply-ProvisioningTemplate.

 

 

 

 

Interesting that it's only adding the JsonControlData field which is what I've be trying to do via PowerShell. I'll give it another go with that before giving up.

Woot!  Finally managed it.  It's just so frustrating how simple it looks when you finally work it out.

 

$homePage = Get-PnPClientSidePage -Identity "Home"
$peoplePart = $homePage.Controls | ? {$_.Title -eq "People"}
$peoplePart.PropertiesJson = '{"layout":1,"persons":[{"id":"i:0#.f|membership|dparkinson@yourtenant.com","role":""}],"title":"Account Team"}'
$homePage.Save()

Note: this assumes only a single control on the page called "People"; and this also changes the title.

 

You can actually set the title by just using:

 

$peoplePart.Title = "My New Title"
$homePage.Save()

But the PropertiesJson already includes it. 

 

Thanks @Pieter Veenstra your tip was very helpful in pointing the right direction.  I always forget about Get-PnPProvisioning for looking at specific items.

 

Kind regards,

D.

 

@Darren Parkinson 

@Pieter Veenstra 

Thanks a lot for this information.

 

I would like to put 2 variables in this code. I can't find the solution.

 

Something like this (First time I use Json :) ) :

 

$userMail = "dparkinson@yourtenant.com"
$title = "Account Team"
$homePage = Get-PnPClientSidePage -Identity "Home"
$peoplePart = $homePage.Controls | ? {$_.Title -eq "People"}
$peoplePart.PropertiesJson = '{"layout":1,"persons":[{"id":"i:0#.f|membership|$userMail","role":""}],"title":$title}'
$homePage.Save()

 

 

I tried this but it doesn't work :

$user = "dparkinson@yourtenant.com"
$title = "Account Team"
$homePage = Get-PnPClientSidePage -Identity "$PageName"
$peoplePart = $homePage.Controls | ? {$_.Title -eq "People"}
$peoplePart.PropertiesJson = '{{"layout":1,"persons":[{"id":"i:0#.f|membership|{0}","role":""}],"title":"{1}"}}' -f $user,$title

 

Any idea ?

@Jeremy GOUPIL 

Worked for me:

 

$userMail = "user@tenant.com"
$title = "Test User"
$homePage = Get-PnPClientSidePage -Identity "Home"
$peoplePart = $homePage.Controls | ? {$_.Title -eq "People"}
$peoplePart.PropertiesJson = '{"layout":1,"persons":[{"id":"i:0#.f|membership|$userMail","role":""}],"title":"$title"}'
$homePage.Save()

 

Do you have an idea how to add multiple users to the webpart?

 

Thanks. 

@Andrei Rachita 

$SiteAdmin1 ="someuser@domain.com"
$SiteAdmin2 ="someuser2@domain.com"

$homePage = Get-PnPClientSidePage -Identity "Home"
$peoplePart = $homePage.Controls | ? {$_.Title -eq "People"}
$peopleName='[{"id":"i:0#.f|membership|'+$SiteAdmin1+'"},{"id":"i:0#.f|membership|'+$SiteAdmin2+'"}]'
$jsonp='{"title":"Site Contact","layout":1,"persons":'+$peopleName+'}'
$peoplePart.PropertiesJson = $jsonp
$homePage.Save()
$homePage.Publish()

 

Kind regards

Vikram Rai (Sopra Steria, Norway)

1 best response

Accepted Solutions
best response confirmed by VI_Migration (Silver Contributor)
Solution

Hi @Darren Parkinson,

 

I ran Get-PnPProvisioning Template before adding a people web part

 

Then I added a people web part and then ran Get-PnPProvisioningTemplate again

 

The difference is :

 

<pnp:CanvasControl WebPartType="People" JsonControlData="{&quot;layout&quot;:1,&quot;persons&quot;:[{&quot;id&quot;:&quot;i:0#.f|membership|pieter@mytenant.onmicrosoft.com&quot;,&quot;role&quot;:&quot;&quot;}]}" ControlId="7f718435-ee4d-431c-bdbf-9c4ff326f46e" Column="1" />

 

On the Add-PnPClientSideWebPart I couldn't find anything to add the data, but you could create a template whcih just includes the  above webpart details and then add the web part with Apply-ProvisioningTemplate.

 

 

 

 

View solution in original post