Introduction
Hi guys, let's discuss an amazing way for a SharePoint List Column to be updated as an Owner Column User Field Type via PowerShell scripting.
It's used after a quick export all the Excel sheet based records onto a SharePoint List with different type of field types.
But what if the Title column has values which have to be converted to the User field property?
Note
The discussed approach is only applicable on an SP On-Prem site collection!
Prerequisites
Windows PowerShell ISE should be installed on the system.
Install the SharePoint Client DLLs after downloading them in some respective paths and mention them in the PS code as shown below.
Valid login details need to be hard coded in the last UserName, Password section in order to run the script without giving the login details at the run time and achieve a seamless program running.
PowerShell script
- #reference the SharePoint Client DLLs
- Add - Type - Path ".\Microsoft.SharePoint.Client.dll" | Out - Null
- Add - Type - Path ".\Microsoft.SharePoint.Client.Runtime.dll" | Out - Null
- #Add - Type - Path "C:\Users\123594\Documents\Veera Kaveri\BoxUpdate\BoxUpdate\Microsoft.SharePoint.Client.dll" | Out - Null
- #Add - Type - Path "C:\Users\123594\Documents\Veera Kaveri\BoxUpdate\BoxUpdate\Microsoft.SharePoint.Client.Runtime.dll" | Out - Null
-
- function Get - SPOSites {
- Param(
- [Microsoft.SharePoint.Client.ClientContext] $Context,
- [Microsoft.SharePoint.Client.Web] $RootWeb)
- #Create array variable to store data
- # $siteitems = $null
- #$siteitems = @()
- $RunDateTime = Get - Date - Format G
- #get all webs under root web
- $Webs = $RootWeb.Webs
- $Context.Load($Webs)
- $Context.ExecuteQuery()
- #loop through the webs
- ForEach($sWeb in $Webs) {
- Write - Host $sWeb.url
- #Create array variable to store data
- $siteitems = $null
- $siteUrl = $sWeb.Url;
- #if($siteUrl - Match 'http://test.sample.com/sites/EU2/teams/e-Business') {
- #if($siteUrl - match 'http://test.sample.com/sites/EU3/tvoe') {
- if ($siteUrl - match 'http://eu.test.sample.com/sites/AdvEU3/Megamigration') {
- #get all lists in web
- $AllLists = $sWeb.Lists
- $Context.Load($AllLists)
- $Context.ExecuteQuery()
- #loop through all lists in web
- ForEach($list in $AllLists) {
- Write - Host List: $list.Title
- #get list title
- $listTitle = $list.Title;
- If($listTitle - eq '3rdpartytosample_test') {
- $itemPosition = $null
- $Count = 0;
- Do {
- $camlQuery = New - Object Microsoft.SharePoint.Client.CamlQuery
- $camlQuery.ViewXml = '<View Scope="RecursiveAll"><RowLimit Paged="TRUE">1000</RowLimit></View>'
- $camlQuery.ListItemCollectionPosition = $itemPosition;
- $AllItems = $list.GetItems($camlQuery)
- $Context.Load($AllItems)
- $Context.ExecuteQuery()
- $itemPosition = $AllItems.ListItemCollectionPosition;
- Write - Host itemPosition: $itemPosition
- If($AllItems.Count - gt 0) {
- ForEach($item in $AllItems) {
- #use internal name
- $userEmail = $item["Title"]
- if ($userEmail - match 'sample.com') {
- $userEmail = $userEmail.Replace('ap.sample.com', 'sample.com')
- $userEmail = $userEmail.Replace('am.sample.com', 'sample.com')
- $userEmail = $userEmail.Replace('eu.sample.com', 'sample.com')
- $userEmail = $userEmail.Replace('jp.sample.com', 'sample.com')
- $User = $Context.Web.EnsureUser($userEmail);
- $Context.Load($user);
- $item["Owner"] = $User;
- $item.Update();
- $Context.ExecuteQuery()
- }
- }
- #end loop
- for all items in list
- }
- #check
- if item count is > 0
- }
- While($itemPosition - ne $null)
- }
- #check
- if it is a 'do not inventory'
- list
- }
- #end loop
- for all lists in site
- }
- Get - SPOSites - RootWeb $sWeb - Context $Context #recursive call
- }
- #end loop
- for all sites in site collection
- }
- #Set parameter values
- $SiteURL = "http://eu.test.sample.com/sites/AdvEU3/Megamigration"
- #$LibraryName = "Documents"
- $UserName = "###########"
- $PassWord = "############"
- $securePassword = ConvertTo - SecureString $PassWord - AsPlainText - Force
- $spoCred = New - Object System.Net.NetworkCredential($UserName, $securePassword)
- #Setup the context
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
- $ctx.Credentials = $spoCred
- $Web = $ctx.Web
- $ctx.Load($Web)
- $ctx.ExecuteQuery()
- Get - SPOSites - RootWeb $Web - Context $ctx
Enter your respective details wherever highlighted or required in the above PowerShell script.
Benefit
Once the above process is finished we can re-use that User field property converted Ttile column to use in many kind of List views.
We can also use the same List column for Filtering, Sorting options on all the custom List views wherever required.
Conclusion
Finally, we got an important way to run PowerShell script to make a given SharePoint List column to be updated as an Owner Column User Field Type via PowerShell scripting...