Updating SharePoint List View For Multiple Lists In Site, Site collection Or List Of Sites With Remote PowerShell

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:

  • Client side Remote PowerShell.
  • A .csv file if script needs to run on multiple sites (SPWeb).
  • User credentials who has administrative access to the sites/environment.

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