Adding XSLT List View Web Part In SharePoint 2013 Sites Using PowerShell

In this article, you will learn how to add XSLT list view web part in SharePoint 2013 sites, using PowerShell.

Introduction to SharePoint Migration Plan

In today’s article, I will share my knowledge obtained during a migration project. Our project involves migration from SharePoint 2010 to SharePoint 2013.

Usually a SharePoint migration project involves configuring the number of pages with the various out of the box Web parts and xslt list view Webpart on the pages on the target site from the source site.

In our migration, we had around 60 to 65 sites to be migrated to 2013 version and there were about 40 to 50 sites, which were configured with only xslt list view Webparts on the home page and we are required to configure those Web parts on the 2013 site home pages. As per analysis, almost all of those 40 to 50 sites had same Web parts configured on the home page, hence our team decided to write a PowerShell script that will add default XSLT list view Web part on the home pages of the 2013 migrated sites.

Preparation of the CSV file as the input 

We have to prepare a CSV file with the appropriate columns and the required data, so that the script will fetch the data from the file. The CSV file as prepared had only one column namely “Site URL”, which is the site URL of 2013 migrated site. To execute PowerShell script, the admin is required to provide the path of CSV file (mandatory parameter).

Explanation of PowerShell Script 

  1. Add - PSSnapin microsoft.sharepoint.powershell  
  2. Clear - Host  
  3.   
  4. function GetList() {  
  5.     Param([Parameter(Mandatory = $true)]  
  6.         [string] $path)  
  7.     try {  
  8.         $csvcontents = Import - Csv - Path $path  
  9.         foreach($content in $csvcontents) {  
  10.             $siteurl = Get - SPWeb - Identity $content.  
  11.             'Site Url'  
  12.             $welcomepageurl = $siteurl.RootFolder.WelcomePage  
  13.             $list1 = $siteurl.Lists.TryGetList('Announcements')  
  14.             AddWebPart($list1)  
  15.             $list2 = $siteurl.Lists.TryGetList('Documents')  
  16.             AddWebPart($list2)  
  17.             $list3 = $siteurl.Lists.TryGetList('Shared Documents')  
  18.             AddWebPart($list3)  
  19.         }  
  20.     } catch {  
  21.         $exception = $_.Exception.Message  
  22.         $exception | Out - File "your log path"  
  23.     }  
  24. }  

  • GetList is the function which is called at start and we are calling this function with the mandatory parameter, which is the path of CSV file.
  • We are getting all the contents from CSV file, using import-CSV module in PowerShell and looping through each, we will get site URL and for each of the site URL, we will get the welcome page URL.
  • Next step will involve getting the required list from the current site and we are calling AddWebPart function by passing obtained list as the mandatory parameter for the function. 
  1. function AddWebPart() {  
  2.     Param([microsoft.sharepoint.splist] $list1)  
  3.     try {  
  4.         $wpmgr = $siteurl.GetLimitedWebPartManager($welcomepageurl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
  5.         $listviewwebpart = New - Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart  
  6.         $listviewwebpart.Title = $list1.Title  
  7.         $listviewwebpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleAndBorder  
  8.         $listviewwebpart.ListName = $list1.ID.ToString("B")  
  9.         $listviewwebpart.ViewGuid = $list1.DefaultView.ID.ToString("B")  
  10.         $listviewwebpart.ExportMode = "All"  
  11.         $wpmgr.AddWebPart($listviewwebpart, "Header", 1)  
  12.     } catch {  
  13.         $exception = $_.Exception.Message  
  14.         $exception | Out - File "your log path"  
  15.     }  
  16. }   

GetList

  • In this function, we are getting Webpart manager instance, which is created for the current site by passing the welcome page URL and personalization scope (it is used to access various properties of the Web parts control) and the personalization scope is set to be shared, which indicates that it applies to all users for personalizable controls on page and is able to save the changes to page

  • We are creating the new instance XsltListViewWebpart, followed by setting various properties like title, chrometype (this is used to set the appearance of the Web part on the page), ListName. This property is usually the list Id of the list, ViewGuid is the guide that is set for the Web part. Here, the guide is the value of the default view of the list, which we are passing as the parameter to the function

  • ExportMode=”All ” indicates that the user can export the Web part properties as an XML file (usually this is disabled by default).

  • The final step is to add the XSLTListView Webpart with Web Part zone and zone Id in the AddWebpart method, followed by calling our GetList function.

After successful execution of the script, you should be able to see the three XSLT list view Web parts on the home page of 2013 sites

Best Practice

Always include your function or code with try-catch block and log the exception with an appropriate content, so that if a script fails, you should be able to see due to what issue it failed.

Note 1

The script above is used only for the sites which have the home page as a Web part page. If the same script is executed on the sites which have Wiki pages as home page, the web part gets added, but if you try to edit the page, you will not be able to see the Web parts and the reason for this is the wiki page does not have any web part zone.

Note 2

If the same script is executed again after a one time execution without a change in any of the parameters, you will not be able to open the Web part page shown by it  and will see that “Web part with this Id already exists”, since we have added the guide of the Web part with the Id, which is equal to the List Default View Id, as it will be added for the first time and will not allow us to add it again.