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.
- CLS
- Write-Host "`t ----------------------------------------------------------------"
- Write-Host "`t Hide Migration Source URL column from List View for eRoom Migration SharePoint Sites"
- Write-Host "`t ----------------------------------------------------------------"
-
- Write-Host -ForegroundColor Magenta "`t `t Choose target SharePoint environment"
- Write-Host -ForegroundColor Cyan "`t `t `t 1. Office 365"
- Write-Host -ForegroundColor Cyan "`t `t `t 2. SharePoint 2013"
- Write-Host -ForegroundColor Cyan "`t `t `t 3. SharePoint 2010"
- $envChoice = Read-Host "Select an option 1-3 "
- switch($envChoice)
- {
- 1{$targetScope = "Office365"}
- 2{$targetScope = "SharePoint2013"}
- 3{$targetScope = "SharePoint2010"}
-
- }
-
- Write-Host
- Write-Host -ForegroundColor Magenta "`t Select the scope of the script excution"
- Write-Host -ForegroundColor Cyan "`t `t 1. Site"
- Write-Host -ForegroundColor Cyan "`t `t 2. Web"
- Write-Host -ForegroundColor Cyan "`t `t 3. List of Web (CSV Path Input)"
- $scopeChoice = Read-Host "Enter your choice"
- #User Credentials
- Write-Host -ForegroundColor Magenta "`t `t Do you have access to "$targetScope " environment"
- Write-Host -ForegroundColor Cyan "`t `t `t 1. Yes"
- Write-Host -ForegroundColor Cyan "`t `t `t 2. No. Provide alternate user credentials (domain\login)"
- $accessChoice = Read-Host "Select an option 1-2"
- Write-Host "accessChoice:"$accessChoice
- switch($accessChoice)
- {
- 1{$accessScope = "HasAccess"}
- 2{$accessScope= "NoAccess"}
- default{$accessScope ="HasAccess"}
- }
-
- $credentials = $null
- #Getting user credentials
- if($accessScope -eq "NoAccess")
- {
- $credentials = Get-Credential -Credential (whoami)
- }
-
- $IsSite = $false
- $csvFilePath = $null
- $csvSPWebList = @()
-
-
- switch ($scopeChoice)
- {
- 1 { $scope = "Site"}
- 2 { $scope ="Web"}
- 3 { $scope = "WebList"}
- }
-
- if($scope -eq "Site")
- {
- $targetUrl = Read-Host "Enter Site Collection Url"
- if($targetScope -eq "Office365")
- {
-
- Hide-MigrationSourceUrl -WebUrl $targetUrl -IsOnline $true -IsSite $true
- }
- elseif($targetScope -eq "SharePoint2013")
- {
- Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $true
-
- }
- elseif($targetScope -eq "SharePoint2010")
- {
- Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $true -Is2010 $true
-
- }
-
- }
- elseif($scope -eq "Web")
- {
- $IsSite = $false
- $targetUrl = Read-Host "Enter Web Url"
- if($targetScope -eq "Office365")
- {
-
- Hide-MigrationSourceUrl -WebUrl $targetUrl -IsOnline $true -IsSite $false
- }
- elseif($targetScope -eq "SharePoint2013")
- {
- Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $false
-
- }
- elseif($targetScope -eq "SharePoint2010")
- {
- Hide-MigrationSourceUrl -WebUrl $targetUrl -UserCredential $credentials -IsSite $false -Is2010 $true
-
- }
- }
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.
- elseif($scope -eq "WebList")
- {
- $IsSite = $false
- while($csvFilePath -eq $null)
- {
- try
- {
- #reading .csv file
- $csvFilePath = Read-Host "Enter path of .csv file which contains SPWeb Urls"
- $csvContent = Import-Csv -Path $csvFilePath -ErrorAction Stop
- if($csvContent -ne $null)
- {
- Write-Host "No. of Entries found:"$csvContent.Count
-
- foreach($entry in $csvContent)
- {
- $csvWebUrl = $entry.WebUrl
- Write-Host "Processing Web:"$csvWebUrl
-
- if($targetScope -eq "Office365")
- {
-
- Hide-MigrationSourceUrl -WebUrl $csvWebUrl -IsOnline $true -IsSite $false
- }
- elseif($targetScope -eq "SharePoint2013")
- {
- Hide-MigrationSourceUrl -WebUrl $csvWebUrl -UserCredential $credentials -IsSite $false
-
- }
- elseif($targetScope -eq "SharePoint2010")
- {
- Hide-MigrationSourceUrl -WebUrl $csvWebUrl -UserCredential $credentials -IsSite $false -Is2010 $true
-
- }
-
- }
- }
-
- }
- catch
- {
- $csvFilePath = $null
- Write-Host -ForegroundColor Red "Error:"$_.Exception.Message "Please try again."
- Write-Host
- }
- }
-
- }
Hide: MigrationSourceUrl is PowerShell function, which use Client Side SharePoint PowerShell module and creates SharePoint Client Context, Client Site object and Client web object.
- function Hide-MigrationSourceUrl
- {
- [CmdletBinding()]
- Param
- (
- [Parameter(Mandatory=$true,Position=0, HelpMessage='Enter Url of the site')]
- [ValidateNotNullOrEmpty()]
- [System.Uri]$WebUrl,
- [Parameter(Mandatory=$false,Position=1, HelpMessage='Is URL of Site or Web? Enter:$true/$false')]
- [Bool]$IsSite = $false,
-
- [Parameter(Mandatory=$false,Position=2, HelpMessage='Enter credentials which has access to the site')]
- [System.Management.Automation.PSCredential]$UserCredential,
-
- [Parameter(Mandatory=$false,Position=3, HelpMessage='Is it SharePoint Online site? Enter:$true/$false')]
- [Bool]$IsOnline = $false,
-
- [Parameter(Mandatory=$false,Position=4, HelpMessage='Is it SharePoint 2010 site? Enter:$true/$false')]
- [Bool]$Is2010 = $false,
-
- [Parameter(Mandatory=$false,Position=5, HelpMessage='Enter SharePoint Online UserName if it is SharePoint Online site')]
- [String]$OnlineUsername,
-
- [Parameter(Mandatory=$false,Position=6, HelpMessage='Enter SharePoint Online Password if it is SharePoint Online site')]
- [String]$OnlinePassword
- )
-
- Begin
- {
- Write-Host "Importing Module SPPS"
- Import-Module .\spps.psm1
-
- }
- Process
- {
- try
- {
-
- if($IsOnline)
- {
- Initialize-SPPS -siteURL $WebUrl -IsOnline $IsOnline
- }
- if($Is2010)
- {
- Initialize-SPPS -siteURL $WebUrl -UserCredential $UserCredential -Is2010 $Is2010
- }
- else
- {
- #SharePoint 2013
- Initialize-SPPS -siteURL $WebUrl -UserCredential $UserCredential
- }
- if($IsSite)
- {
- Write-Host "Processing as site"
-
- #getting subsite
- $w = Get-SPWebs -ctx $SPPS -spWeb $Web
-
-
- foreach($subWeb in $w)
- {
-
- #Write-Host $subWeb.ServerRelativeUrl
- Update-ListDefaultView -ParamWeb $subWeb
- }
- }
- else
- {
- Write-Host "Processing as Web"
- Update-ListDefaultView -ParamWeb $web
- }
-
- }
-
- catch [System.Net.WebException],[System.Exception]
- {
- Write-Host "Error:"$_.Exception.ToString()
- }
-
- }
- End
- {}
- }
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.
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.