Recently, we did eRoom to SharePoint 2013 migration with the help of Metalogix. When you migrate eRoom content to SharePoint, Metalogix eRoom console by default add ‘MigrationSourceUrl’ column to every list and library created during migration. This column contains source eRoom object URL from where this object is migrated. The value in this column is useful from mapping perspective and link correction perspective. In Metalogix you can't avoid creating this column in target SharePoint. Though in our case, end user don’t want to see this column by default in SharePoint. So we decided to hide this column from ‘Default’ list/library view after migration is complete. This was again a task which involves lot of manual efforts. Ultimate choice was to create PowerShell script which could be re-used now and in future. What if it is Remote PowerShell? It provides added advantage that you don’t have to login to CA box and run the script and also no burden on your server.

Pre-requisite:

Details

The script first understands the target SharePoint scope like Site Collection, Site or List of Sites based on user inputs. It also tries to understand if current user has access to the target SharePoint environment. Based on user inputs, it takes various approaches to get list of webs and list of Lists.

  1. CLS
  2. Write-Host "`t ----------------------------------------------------------------"
  3. Write-Host "`t Hide Migration Source URL column from List View for eRoom Migration SharePoint Sites"
  4. Write-Host "`t ----------------------------------------------------------------"
  5. Write-Host -ForegroundColor Magenta "`t `t Choose target SharePoint environment"
  6. Write-Host -ForegroundColor Cyan "`t `t `t 1. Office 365"
  7. Write-Host -ForegroundColor Cyan "`t `t `t 2. SharePoint 2013"
  8. Write-Host -ForegroundColor Cyan "`t `t `t 3. SharePoint 2010"
  9. $envChoice = Read-Host "Select an option 1-3 "
  10. switch($envChoice)
  11. {
  12. 1{$targetScope = "Office365"}
  13. 2{$targetScope = "SharePoint2013"}
  14. 3{$targetScope = "SharePoint2010"}
  15. }
  16. Write-Host
  17. Write-Host -ForegroundColor Magenta "`t Select the scope of the script excution"
  18. Write-Host -ForegroundColor Cyan "`t `t 1. Site"
  19. Write-Host -ForegroundColor Cyan "`t `t 2. Web"
  20. Write-Host -ForegroundColor Cyan "`t `t 3. List of Web (CSV Path Input)"
  21. $scopeChoice = Read-Host "Enter your choice"
  22. #User Credentials
  23. Write-Host -ForegroundColor Magenta "`t `t Do you have access to "$targetScope " environment"
  24. Write-Host -ForegroundColor Cyan "`t `t `t 1. Yes"
  25. Write-Host -ForegroundColor Cyan "`t `t `t 2. No. Provide alternate user credentials (domain\login)"
  26. $accessChoice = Read-Host "Select an option 1-2"
  27. Write-Host "accessChoice:"$accessChoice
  28. switch($accessChoice)
  29. {
  30. 1{$accessScope = "HasAccess"}
  31. 2{$accessScope= "NoAccess"}
  32. default{$accessScope ="HasAccess"}
  33. }
  34. $credentials = $null
  35. #Getting user credentials
  36. if($accessScope -eq "NoAccess")
  37. {
  38. $credentials = Get-Credential -Credential (whoami)
  39. }
  40. $IsSite = $false
  41. $csvFilePath = $null
  42. $csvSPWebList = @()
  43. switch ($scopeChoice)
  44. {
  45. 1 { $scope = "Site"}
  46. 2 { $scope ="Web"}
  47. 3 { $scope = "WebList"}
  48. }
  49. if($scope -eq "Site")
  50. {
  51. $targetUrl = Read-Host "Enter Site Collection Url"
  52. if($targetScope -eq "Office365")
  53. {
  54. Hide-MigrationSourceUrl -WebUrl $targetUrl -IsOnline $true -IsSite $true
  55. }
  56. elseif($targetScope -eq "SharePoint2013")
  57. {
  58. Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $true
  59. }
  60. elseif($targetScope -eq "SharePoint2010")
  61. {
  62. Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $true -Is2010 $true
  63. }
  64. }
  65. elseif($scope -eq "Web")
  66. {
  67. $IsSite = $false
  68. $targetUrl = Read-Host "Enter Web Url"
  69. if($targetScope -eq "Office365")
  70. {
  71. Hide-MigrationSourceUrl -WebUrl $targetUrl -IsOnline $true -IsSite $false
  72. }
  73. elseif($targetScope -eq "SharePoint2013")
  74. {
  75. Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $false
  76. }
  77. elseif($targetScope -eq "SharePoint2010")
  78. {
  79. Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $false -Is2010 $true
  80. }
  81. }
If script understands that the scope is a list of sites (SPWeb), it requests the path of .csv file which contains Url to each site (SPWeb). It reads the .csv file and work on each site.
  1. elseif($scope -eq "WebList")
  2. {
  3. $IsSite = $false
  4. while($csvFilePath -eq $null)
  5. {
  6. try
  7. {
  8. #reading .csv file
  9. $csvFilePath = Read-Host "Enter path of .csv file which contains SPWeb Urls"
  10. $csvContent = Import-Csv -Path $csvFilePath -ErrorAction Stop
  11. if($csvContent -ne $null)
  12. {
  13. Write-Host "No. of Entries found:"$csvContent.Count
  14. foreach($entry in $csvContent)
  15. {
  16. $csvWebUrl = $entry.WebUrl
  17. Write-Host "Processing Web:"$csvWebUrl
  18. if($targetScope -eq "Office365")
  19. {
  20. Hide-MigrationSourceUrl -WebUrl $csvWebUrl -IsOnline $true -IsSite $false
  21. }
  22. elseif($targetScope -eq "SharePoint2013")
  23. {
  24. Hide-MigrationSourceUrl -WebUrl $csvWebUrl -UserCredential $credentials -IsSite $false
  25. }
  26. elseif($targetScope -eq "SharePoint2010")
  27. {
  28. Hide-MigrationSourceUrl -WebUrl $csvWebUrl -UserCredential $credentials -IsSite $false -Is2010 $true
  29. }
  30. }
  31. }
  32. }
  33. catch
  34. {
  35. $csvFilePath = $null
  36. Write-Host -ForegroundColor Red "Error:"$_.Exception.Message "Please try again."
  37. Write-Host
  38. }
  39. }
  40. }
Hide: MigrationSourceUrl is PowerShell function, which use Client Side SharePoint PowerShell module and creates SharePoint Client Context, Client Site object and Client web object.
  1. function Hide-MigrationSourceUrl
  2. {
  3. [CmdletBinding()]
  4. Param
  5. (
  6. [Parameter(Mandatory=$true,Position=0, HelpMessage='Enter Url of the site')]
  7. [ValidateNotNullOrEmpty()]
  8. [System.Uri]$WebUrl,
  9. [Parameter(Mandatory=$false,Position=1, HelpMessage='Is URL of Site or Web? Enter:$true/$false')]
  10. [Bool]$IsSite = $false,
  11. [Parameter(Mandatory=$false,Position=2, HelpMessage='Enter credentials which has access to the site')]
  12. [System.Management.Automation.PSCredential]$UserCredential,
  13. [Parameter(Mandatory=$false,Position=3, HelpMessage='Is it SharePoint Online site? Enter:$true/$false')]
  14. [Bool]$IsOnline = $false,
  15. [Parameter(Mandatory=$false,Position=4, HelpMessage='Is it SharePoint 2010 site? Enter:$true/$false')]
  16. [Bool]$Is2010 = $false,
  17. [Parameter(Mandatory=$false,Position=5, HelpMessage='Enter SharePoint Online UserName if it is SharePoint Online site')]
  18. [String]$OnlineUsername,
  19. [Parameter(Mandatory=$false,Position=6, HelpMessage='Enter SharePoint Online Password if it is SharePoint Online site')]
  20. [String]$OnlinePassword
  21. )
  22. Begin
  23. {
  24. Write-Host "Importing Module SPPS"
  25. Import-Module .\spps.psm1
  26. }
  27. Process
  28. {
  29. try
  30. {
  31. if($IsOnline)
  32. {
  33. Initialize-SPPS -siteURL $WebUrl -IsOnline $IsOnline
  34. }
  35. if($Is2010)
  36. {
  37. Initialize-SPPS -siteURL $WebUrl -UserCredential $UserCredential -Is2010 $Is2010
  38. }
  39. else
  40. {
  41. #SharePoint 2013
  42. Initialize-SPPS -siteURL $WebUrl -UserCredential $UserCredential
  43. }
  44. if($IsSite)
  45. {
  46. Write-Host "Processing as site"
  47. #getting subsite
  48. $w = Get-SPWebs -ctx $SPPS -spWeb $Web
  49. foreach($subWeb in $w)
  50. {
  51. #Write-Host $subWeb.ServerRelativeUrl
  52. Update-ListDefaultView -ParamWeb $subWeb
  53. }
  54. }
  55. else
  56. {
  57. Write-Host "Processing as Web"
  58. Update-ListDefaultView -ParamWeb $web
  59. }
  60. }
  61. catch [System.Net.WebException],[System.Exception]
  62. {
  63. Write-Host "Error:"$_.Exception.ToString()
  64. }
  65. }
  66. End
  67. {}
  68. }
Once you have client context, it then calls another helper function which does actual finding target list and target list view which needs to be updated.
  1. function Update-ListDefaultView
  2. {
  3. [CmdletBinding()]
  4. [OutputType([int])]
  5. Param
  6. (
  7. [Parameter(Mandatory=$true,
  8. Position=0)]
  9. [Microsoft.SharePoint.Client.Web]$ParamWeb
  10. )
  11. Begin
  12. {
  13. }
  14. Process
  15. {
  16. try
  17. {
  18. $ParamWebUrl = $ParamWeb.ServerRelativeUrl
  19. Write-Host -ForegroundColor DarkCyan "`t Processing Web:"$ParamWebUrl
  20. #Getting Lists
  21. $Lists = $ParamWeb.Lists
  22. $SPPS.Load($Lists)
  23. $SPPS.ExecuteQuery()
  24. #Filtering hidden list
  25. $Lists = $Lists | ?{$_.Hidden -eq $false}
  26. foreach($list in $Lists)
  27. {
  28. Write-Host
  29. Write-Host -ForegroundColor Blue "`t Processing List:"$list.Title
  30. try
  31. {
  32. $listFields = $list.Fields
  33. $SPPS.Load($listFields)
  34. $SPPS.ExecuteQuery()
  35. }
  36. catch
  37. {
  38. Write-Host -ForegroundColor Red "Error while loading List fields"$_.Exception.ToString()
  39. }
  40. #Checking if MigrationSourceURL field is part of the list
  41. #$migrationSourceUrlField = $null
  42. if($listFields -ne $null)
  43. {
  44. try
  45. {
  46. $migrationSourceUrlField = $listFields | ?{$_.Title -eq "MigrationSourceUrl"} -ErrorAction Stop
  47. }
  48. catch [System.InvalidCastException], [System.Exception]
  49. {
  50. Write-Host -ForegroundColor Red "Error while checking if list contains MigrationSourceURL field"$_.Exception.ToString()
  51. }
  52. if($migrationSourceUrlField -ne $null)
  53. {
  54. #Migration Source Url Field Exists in the list or library
  55. Write-Host -ForegroundColor Cyan "`t `t Processing default list view for:"$list.Title
  56. $defaultListView = $null
  57. try
  58. {
  59. $listViews = $list.Views
  60. $SPPS.Load($listViews)
  61. $SPPS.ExecuteQuery()
  62. }
  63. catch
  64. {
  65. Write-Host -ForegroundColor Red "Error while loading views:"$_.Exception.ToString()
  66. }
  67. try
  68. {
  69. $defaultListView = $listViews | ?{$_.DefaultView -eq $true}
  70. }
  71. catch
  72. {
  73. Write-Host -ForegroundColor Red "Error while loading default view:"$_.Exception.ToString()
  74. }
  75. if($defaultListView -ne $null)
  76. {
  77. try
  78. {
  79. $defaultViewFields = $defaultListView.ViewFields
  80. $SPPS.Load($defaultViewFields)
  81. $SPPS.ExecuteQuery()
  82. Write-Host "`t `t Checking if default view has MigrationSourceURL field"
  83. $fieldToDelete = $defaultViewFields | ?{$_.equals("MigrationSourceURL")}
  84. if($fieldToDelete -ne $null)
  85. {
  86. Write-Host -ForegroundColor Magenta "`t `t Removing MigrationSourceURL from "$defaultListView.Title " of list "$list.Title
  87. $defaultViewFields.Remove("MigrationSourceURL")
  88. $defaultListView.Update()
  89. $SPPS.ExecuteQuery()
  90. Write-Host -ForegroundColor Green "`t `t MigrationSourceURL field successfully removed from "$defaultListView.Title " of list "$list.Title
  91. }
  92. else
  93. {
  94. Write-Host -ForegroundColor Yellow "`t `t MigrationSourceURL field not found in view "$defaultListView.Title " of list "$list.Title
  95. }
  96. }
  97. catch
  98. {
  99. Write-Host -ForegroundColor Red "Error while updating default list view:"$_.Exception.ToString()
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. Catch [System.Exception]
  107. {
  108. Write-Host -ForegroundColor Red "Error in Update-ListDefaultView:"$_.Exception.ToString()
  109. }
  110. }
  111. End
  112. {
  113. }
  114. }
The above function is specific to my use case scenario. Your use case scenario might be different but can be easily tweaked to satisfy your needs.

In my scenario, I am finding all the lists for particular web (SPWeb) which contains ‘MigrationSourceUrl’ column. Then I found their default view. Once I had list default view, I then loaded all the ViewFields of that view. After that I saw if this view fields contains field I need to hide and if yes, I deleted that field from ViewFields collection.

The complete PowerShell script is attached with this article along with the sample .csv file. Your feedback will help me to correct any issues or improve the script.