{"id":667,"date":"2015-10-14T11:58:26","date_gmt":"2015-10-14T11:58:26","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=667"},"modified":"2024-05-06T12:14:56","modified_gmt":"2024-05-06T12:14:56","slug":"working-with-web-parts-in-powershell","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/working-with-web-parts-is-powershell\/","title":{"rendered":"Working with Web Parts in PowerShell"},"content":{"rendered":"<p>Working with web parts programmatically across pages is awkward but possible in PowerShell. Let\u2019s start by generating a report of the web parts. This iterates through non-admin web apps, site collections, and pages:<\/p>\n<pre lang=\"php\">$oContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService;\r\n[Microsoft.SharePoint.Administration.SPWebApplicationCollection]$waCollection = $oContentService.WebApplications;\r\n$log = \".\\results.txt\"    # output file name and path\r\n$pagepath = \"\/default.aspx\"    # you can change page name or page path  \r\n\"Site URL; WebPart Title ; Webpart ID\" | out-file $log\r\n$waCollection1 = $waCollection | where-object {$_.IsAdministrationWebApplication -eq $FALSE}\r\nforeach ($wa in $waCollection1)\r\n{\r\nforeach ($obj in $wa.Sites) \r\n{\r\nwrite-host \"Processing site: \" , $siteURL\r\n$siteURL = $obj.URL\r\n  \r\n$site=new-object Microsoft.SharePoint.SPSite($siteURL)\r\n  \r\n$pageURL = $siteURL + $pagepath \r\n$web=$site.Openweb()   \r\n$webpartmanager=$web.GetLimitedWebPartManager($pageURL,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)   \r\nforeach ($webpart in $webpartmanager.WebParts)\r\n{   \r\n  $siteURL + \"; \" + $webpart.Title + \" ;  \" + $webpart.ID | out-file $log -append  \r\n}                           \r\n}\r\n&lt;\/pre&gt;\r\n\r\nAs an example, we can remove web parts programmatically, by specifying the site collection and Web Part GUID:\r\n\r\n<\/pre>\n<pre lang=\"php\">$siteURL = \"ht tp:\/\/SharePoint\/sites\/specialsite\";  # first constant: site URL\r\n$webpartId = \";   # second argument:  webpart GUID\r\n$pagepath =  \"\/default.aspx\"        # change page name or page path here\r\n$pageURL = $siteURL + $pagepath\r\nwrite-host \"Processing site: \", $siteURL\r\nWrite-host \"Processing page: \" , $pageURL\r\nwrite-host \"Processing webpart ID: \" , $webpartID\r\n$site=new-object Microsoft.SharePoint.SPSite($siteURL)\r\n$web=$site.Openweb()\r\n$webpartmanager=$web.GetLimitedWebPartManager($pageURL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)\r\n$webpartmanager.DeleteWebPart($webpartmanager.Webparts[$webpartId])\r\n$web.Update()\r\n$web.Dispose()\r\nwrite-host \"Finished.\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Working with web parts programmatically across pages is awkward but possible in PowerShell. Let\u2019s start by generating a report of the web parts. This iterates through non-admin web apps, site collections, and pages: $oContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService; [Microsoft.SharePoint.Administration.SPWebApplicationCollection]$waCollection = $oContentService.WebApplications; $log = &#8220;.\\results.txt&#8221; # output file name and path $pagepath = &#8220;\/default.aspx&#8221; # you can change [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":671,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":11,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":693,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/667\/revisions\/693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/671"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}