How-To Delete the Web Part from Page Using PowerShell Scripts for the Existing Projects Sites

Posted By Posted by: Ahmed Taee on June 22, 2017

Sometimes we are required to delete the web part from an existing project’s site. In this blog, I will guide you step-by-step on how to delete the web part using a PowerShell script:

Run an SQL query to get the list of the projects and their URL

SELECT [ProjectName] ,[ProjectWorkspaceInternalHRef]

FROM [MSP_EpmProject_UserView]

Copy the results with header, paste in Excel sheet and save it as a CSV file as “DelProjects_CSV”

Run the PowerShell script below:

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )

{

Add-PSSnapin Microsoft.SharePoint.PowerShell

}

$path = “C:\Path of CSV file”                #Path of the CSV File

$file = “DelProjects_CSV”                    #Name of the CSV File

$tofile   = “$path$file.csv”

$ProjectSites = Import-Csv -path $tofile

ForEach($site in $ProjectSites)

{

$siteUrl = $site.ProjectWorkspaceInternalHRef

$spweb = Get-SpWeb $siteUrl

 

Declare the absolute path to the SharePoint page.

$pagePath = “/default.aspx”

$pageUrl = $siteUrl + $pagePath

write-host “Processing site: “, $siteUrl

write-host “Processing page: “, $pageUrl

 

Initialize the web part manager for the specified profile page.

$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl,     [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

List all the web parts in the specified page.

foreach ($webpart in $spWebPartManager.WebParts)

{

write-host $siteUrl +”: Existing Web part – ” + $webpart.Title + ” : ” + $webpart.ID

break;

}

 

Remove the Share Documents web part from that page.

foreach ($webpart in ($spWebPartManager.WebParts | Where-Object {$_.Title -eq ” Name of deleted web part“}))

{

write-host $siteUrl +”: Existing Web part – ” + $webpart.Title + ” : ” + $webpart.ID

$webpart1 = $webpart

break;

}

Delete the existing web part.

$spWebPartManager.DeleteWebPart($spWebPartManager.WebParts[$webpart1.ID])

write-host “Deleted the existing Shared Document web part.”

$spWeb.Dispose()

}


Blog Posted In Blog Posted In: Blog, How to, Project Server
Blog Posted In 

Leave a Reply

Your email address will not be published. Required fields are marked *