Articles about European Sharepoint Hosting Service
SharePoint Hosting – List All Content Editor Webparts Present In A SharePoint OnPremise Web Application Using PowerShell
In this article, I will provide information on how to get all the content editor webparts present under a web application using PowerShell.
Below are the components used in this document.
- PowerShell
- SharePoint 2016
Some time ago we get a request to collect an inventory on the current SharePoint environment; including how many content editor Webparts and the number of lists and libraries under a web application. Below is the step by step way to get all the content editor webparts present under a web application using PowerShell.
Power Shell File
Step 1
Create a text file. Name it as “GetTotalCEWP.ps1” file. Note that you will have to change the extension from “.txt” to “.ps1”
Step 2
Insert the below code to get the web application URL as an input from the user.
- [CmdletBinding()]
- param( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage=“Input the Web Application URL.”)] [string]$WebApp)
Step 3
Insert the below code to load the SharePoint PowerShell snap in.
1 2 3 4 |
if ((Get - PSSnapin - Name Microsoft.SharePoint.PowerShell - ErrorAction SilentlyContinue) - eq $null) { Write - Host "Loading SharePoint PowerShell" Add - PSSnapin Microsoft.SharePoint.PowerShell } |
Step 4
Insert the below code to get all the publishing pages and then fetch the content editor webparts from those pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$SPWebApp = Get - SPWebApplication $WebApp - EA SilentlyContinue if ($SPWebApp - eq $null) { Write - Error "$WebApp url is not a valid!" } else { $allsites = $SPWebApp.Sites foreach($site in $allsites) { try { $allwebs = $site.AllWebs foreach($web in $allwebs) { try { if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) { $pubpages = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) $allpages = $pubpages.GetPublishingPages() foreach($page in $allpages) { GetCEWPFunction - url $page.Url } } } catch {} finally { $web.Dispose() } } } catch {} finally { $site.Dispose() } } } |
Step 5
There might be scenarios where we have added the content Editor webparts inside the List/Libraries forms (edit/view/display). Insert the below code to retrieve the same. We have to insert this code just before the first try/catch ends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
GetCEWPFunction - url $page.Url } } $lists = $web.GetListsOfType("DocumentLibrary") | ? { $_.IsCatalog - eq $false } foreach($list in $lists) { $allviews = $list.Views foreach($view in $allviews) { GetCEWPFunction - url $view.Url } $allforms = $list.Forms foreach($form in $allforms) { GetCEWPFunction - url $form.Url } } } catch {} finally { $web.Dispose() } } } catch {} finally { $site.Dispose() } } } |
Step 6
Insert the below code to have the function named “GetCEWPFunction “ which would actually count the content editor webpart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function GetCEWPFunction([string] $url) { $manager = $web.GetLimitedWebPartManager($url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $allwebParts = $manager.WebParts if ($allwebParts.Count - ne 0) { foreach($webPart in $allwebParts) { if ($webPart.GetType() - eq[Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) { if ($webPart.ContentLink.Length - gt 0) { $file = $web.GetFile($webPart.ContentLink) $data = $file.OpenBinary() $encode = New - Object System.Text.ASCIIEncoding $contents = $encode.GetString($data) if ($contents.ToLower().Contains("<script>")) { Write - Output "$($web.Url)/$url (CONTENTLINK)" } break } if ($webPart.Content.InnerText.Contains("<script>")) { Write - Output "$($web.Url)/$url (HTML)" } } } } } |
Step 7
The full file will look as attached with this article.
Step 8
Save your PowerShell File and test it by running below command. It will list down all the content editor webparts present under a web application
.\GetTotalCEWP.ps1 -WebApp http://portal.intranet.com
That is it. I hope you have learned something new from this article and will utilize this in your work.
Print article | This entry was posted by Peter on August 24, 2020 at 8:11 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |