## CONTACT INFO ## ------------------------------------------------ ## CREATED : 2013.03.30 ## MODIFIED : ## AUTHOR : Jason Lasby ## EMAIL : digitalslavery@gmail.com ## ## DESCRIPTION ## ------------------------------------------------ ## Script to loop through a set of Subsites and ## Delete a web part from a page stored in the SitePages ## Document Library named Dashboard.aspx ## ## EXPLAINATION ## ------------------------------------------------ ## This example illustrates how to delete a webpart ## from a set of sites that use the same page layout ## and configured with the same web parts, think about ## a company dashboard site where each department may ## report standard data like who is in who is out, ## work related accidents, finance data, shipping ## information, etc... We can loop through our sites ## and add or remove web parts for all sites at once. ## ## BEGIN ## ------------------------------------------------ ## Load up the snapin so Powershell can work with SharePoint ## if the snapin was already loaded don't alert the user Add-PSSnapin microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue ## Clear any errors or output to the screen CLS ## Set variables ## This variable will be the site where I want to start $parentSite = "http://sp2010/dashboards" function RemoveWebPart { foreach($wp in $wpm.WebParts | ?{$_.Title -like "* Banner *"}) { if($wp) { $listOfWebParts = $wp.ID foreach($matchFound in $listOfWebParts) { $wpm.DeleteWebPart($wpm.WebParts[$matchFound]) Write-Host "$matchFound was deleted for $webSite" } } } } Start-SPAssignment -Global ## Create a variable to hold the SharePoint site, invoke the connection ## to the site we defined in the $parentSite variable $web = Get-SPWeb $parentSite ## Check to make sure that url exists if($web) { ## Loop through all the sub sites of the site url we got foreach($webSite in $web.Webs) { $webSiteName = $webSite.Name $pageUrl = "SitePages/dashboard.aspx" $page = $webSite.GetFile($pageUrl) $wpm = $webSite.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) if($wpm) { RemoveWebPart } else { Write-Host "Web part not found" } } } else { ## Gracefully let the user know that the site they requested ## was not available or was not found. Write-Host "$webSite not found, check the url and try again." } Stop-SPAssignment -Global
SharePoint 2007/2010 development using C# and Powershell. Mostly just things I have found that make my job easier but I had to search around to find answers to.
Saturday, March 30, 2013
Powershell Delete a webpart from a SharePoint webpart page loop
Today I find that when I create new things, often times I create too many things during my testing of say dynamically adding a webpart to a SharePoint webpart page. So I need to have a way to quickly loop through my hundereds of sites and delete the webpart.
Subscribe to:
Post Comments (Atom)
1 comment:
Please confirm if this step has no error.
$listOfWebParts = $wp.ID
foreach($matchFound in $listOfWebParts)
------------------------
$wp.ID is a single value not a collection. I'm confused why there is foreach for $listOfWebParts, where as this is just an ID.
Post a Comment