PowerShell script to change the People web part properties on a modern SharePoint site

Manimekalai
 
Technology Specialist
October 14, 2018
 
Rate this article
 
Views
5307

In this article, let us see how to change the site contact properties on a People web part for the multiple site since we can’t go and change it manually every time if we need to update the properties for the multiple site collection. To avoid this manual activity, we can use PowerShell script

The below script is used to get the site owner information from the excel file and retrieve the web part present on the site’s home page and update the propertyJSON for the people web part with the values provided in the excel file.

 

 Write-Output -msg "Start : Module loading Process"
 $modulePath = "C:Program FilesSharePoint Online Management ShellSharePointPnPPowerShellOnline"
         import-module ($modulePath + "SharePointPnPPowerShellOnline.psd1")
         Add-Type -Path ($modulePath + "SharePointPnP.PowerShell.Online.Commands.dll")
         Add-Type -Path ($modulePath + "Microsoft.SharePoint.Client.dll")
         Add-Type -Path ($modulePath + "Microsoft.SharePoint.Client.Runtime.dll")
         Add-Type -Path ($modulePath + "Microsoft.Online.SharePoint.Client.Tenant.dll")
 Write-Output -msg "End : Module loading Process"
 $inputFilePath="D:XXXInput.xlsx"
 $UserName="test@xxx.onmicrosoft.com"
 $pwd ="xxxxxxx"
 try
 {
 #Get the input sites from Excel File
 $objExcel = New-Object -ComObject Excel.Application
 $objExcel.Visible = $false
 $objExcel.UserControl= $True
 [threading.thread]::CurrentThread.CurrentCulture = 'en-US'
 $WorkBook = $objExcel.Workbooks.Open($inputFilePath)
 $WorkSheet = $WorkBook.sheets.item("Sheet1")
 $rowMax = ($WorkSheet.UsedRange.Rows).count
 for ($i=2; $i -le $rowMax; $i++)
 {   
     $createdSiteURL=$worksheet.Rows.Item($i).Columns.Item(1).Text
     $primaryOwner=$worksheet.Rows.Item($i).Columns.Item(2).Text
     $secondaryOwner=$worksheet.Rows.Item($i).Columns.Item(3).Text 
     $encpassword = convertto-securestring -String $pwd -AsPlainText -Force
     $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $UserName, $encpassword
 # Connect Site
     Connect-PnPOnline -Url $createdSiteURL -Credentials $cred
     Write-Output $createdSiteURL
 #Get the Webparts available in the home page and update the propertiesjson for People Webpart
     $webpart=Get-PnPClientSideComponent -Page Home 
     foreach($w in $webpart)
     {
       if($w.Title -eq "People")
       {
         $peoplewbInstanceId=$w.InstanceId
         $peopleName="[{`"id`":`"i:0#.f|membership|"+$primaryOwner+"`"},{`"id`":`"i:0#.f|membership|"+$secondaryOwner+"`"}]"
         $jsonp='{"title":"Site Contact","layout":1,"persons":'+$peopleName+'}'
         Set-PnPClientSideWebPart -Page "Home" -Identity $peoplewbInstanceId -PropertiesJson $jsonp
         Set-PnPClientSidePage -Identity "Home" -Publish
       }
   
     }
 
    }
 }
 catch
 {
      Write-Output $_.Exception.Message
 }
 $objExcel.quit()
 

 

 

Hope this helps!

Category : SharePoint, SPFx

Author Info

Manimekalai
 
Technology Specialist
 
Rate this article
 
Having 6 years of experience in design and development of SharePoint applications, Mani has expertise in developing applications using Office 365, SharePoint 2013 & 2010, SPFX, SP Designer Workflows, Visual ...read more
 

Leave a comment