Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: How to Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint?
In this post, I will show you how to Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint.
The following section explains the flow for getting web parts from a publishing page.
Add the references using the Add-Type command with necessary reference paths. The necessary references are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll" Initialize client context object with the site URL. $siteURL = "" $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL) If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. #Not required for on premise site - Start $userId = "" $pwd = Read-Host -Prompt "Enter password" -AsSecureString $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd) $ctx.credentials = $creds #Not required for on premise site - End |
If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server. Get the page from the respective library using server relative url. Load the file and execute the query to access the page components.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#page to be viewed $pageName = "TestPage1.aspx" #Get the page $file = $ctx.Web.GetFileByServerRelativeUrl("/Pages/TestPage1.aspx") $ctx.Load($file) $ctx.ExecuteQuery() Then from the page, we will get the web parts present. From the file object, using web part manager and personalization scope we can access the page web parts. Load and execute the query to access the web parts. #Get all the webparts Write-Host "Retrieving webparts" $wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared) $webparts = $wpManager.Webparts $ctx.Load($webparts) $ctx.ExecuteQuery() |
Access the web parts.
Check the web part count and if web parts present, access the individual web parts using foreach loop.
For each web part, the context should be loaded with web part properties and executed.
Then $webpart.Id will get us web part GUID and $webpart.WebPart.Properties.FieldValues will have all the properties defined for a web part. you can use foreach loop to get the necessary properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if($webparts.Count -gt 0){ Write-Host "Looping through all webparts" foreach($webpart in $webparts){ $ctx.Load($webpart.WebPart.Properties) $ctx.ExecuteQuery() $propValues = $webpart.WebPart.Properties.FieldValues Write-Host "ID: $webpart.id" foreach($property in $propValues){ Write-Host "Title: " $property.Title Write-Host "Description: " $property.Description Write-Host "Chrome Type: " $property.ChromeType } } } |
Like wise other properties can also be retrieved. The following snapshot shows the properties which can be retrieved for any web part present on the page. This image shows the content editor web part properties present on the page.
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.
Print article | This entry was posted by Peter on March 21, 2017 at 7:28 am, and is filed under European SharePoint Server 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |