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

  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

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.