Hide Webpart – SharePoint Online by PowerShell

I came across a scenario where I need to hide the OOTB list form WebPart from the SharePoint List “NewForm.aspx” page by PowerShell. I have written a script to perform the same in SharePoint online.

In the below script, I am getting the list of WebParts in the NewForm.aspx page and setting the hidden property. If you have more than one WebPart in a page, you can find the specific WebPart and set the required properties to that.

Note:

  • Update the url, username and password with your tenant details.
  • Keep the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime DLLs in the place where you saved the PowerShell script file or change the location as per your location.

Script

cls

$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase

Add-Type -Path $scriptBase”\Microsoft.SharePoint.Client.dll”
Add-Type -Path $scriptBase”\Microsoft.SharePoint.Client.Runtime.dll”

$url = “https://xyz.sharepoint.com/sites/dev”
$username = “admin@xyz.onmicrosoft.com”
$password = “Password”

$Password = $password |ConvertTo-SecureString -AsPlainText -force
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$Context.Credentials = $credentials

$List = $Context.Web.Lists.GetByTitle(“Test List”)
$Context.Load($List)
$Context.ExecuteQuery()

$Pages = $List.RootFolder.Files
$Context.Load($Pages)
$Context.ExecuteQuery()

$Page = $List.RootFolder.Files | Where {$_.Name -eq “NewForm.aspx”}
$Context.Load($Page)
$Context.ExecuteQuery()

$WPM = $Page.GetLimitedWebPartManager(“Shared”)
$Context.Load($WPM)
$Context.ExecuteQuery()

$WebParts = $WPM.WebParts
$Context.Load($WebParts)
$Context.ExecuteQuery()

$WebPart = $WebParts[0]
$Context.Load($WebPart)
$Context.ExecuteQuery()

$WebPart.WebPart.Hidden = $true
$WebPart.SaveWebPartChanges()
$Context.ExecuteQuery()

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Comments are closed.

%d bloggers like this: