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
  1. #reference the SharePoint Client DLLs
  2. Add - Type - Path ".\Microsoft.SharePoint.Client.dll" | Out - Null
  3. Add - Type - Path ".\Microsoft.SharePoint.Client.Runtime.dll" | Out - Null
  4. #Add - Type - Path "C:\Users\123594\Documents\Veera Kaveri\BoxUpdate\BoxUpdate\Microsoft.SharePoint.Client.dll" | Out - Null
  5. #Add - Type - Path "C:\Users\123594\Documents\Veera Kaveri\BoxUpdate\BoxUpdate\Microsoft.SharePoint.Client.Runtime.dll" | Out - Null
  6. function Get - SPOSites {
  7. Param(
  8. [Microsoft.SharePoint.Client.ClientContext] $Context,
  9. [Microsoft.SharePoint.Client.Web] $RootWeb)
  10. #Create array variable to store data
  11. # $siteitems = $null
  12. #$siteitems = @()
  13. $RunDateTime = Get - Date - Format G
  14. #get all webs under root web
  15. $Webs = $RootWeb.Webs
  16. $Context.Load($Webs)
  17. $Context.ExecuteQuery()
  18. #loop through the webs
  19. ForEach($sWeb in $Webs) {
  20. Write - Host $sWeb.url
  21. #Create array variable to store data
  22. $siteitems = $null
  23. $siteUrl = $sWeb.Url;
  24. #if($siteUrl - Match 'http://test.sample.com/sites/EU2/teams/e-Business') {
  25. #if($siteUrl - match 'http://test.sample.com/sites/EU3/tvoe') {
  26. if ($siteUrl - match 'http://eu.test.sample.com/sites/AdvEU3/Megamigration') {
  27. #get all lists in web
  28. $AllLists = $sWeb.Lists
  29. $Context.Load($AllLists)
  30. $Context.ExecuteQuery()
  31. #loop through all lists in web
  32. ForEach($list in $AllLists) {
  33. Write - Host List: $list.Title
  34. #get list title
  35. $listTitle = $list.Title;
  36. If($listTitle - eq '3rdpartytosample_test') {
  37. $itemPosition = $null
  38. $Count = 0;
  39. Do {
  40. $camlQuery = New - Object Microsoft.SharePoint.Client.CamlQuery
  41. $camlQuery.ViewXml = '<View Scope="RecursiveAll"><RowLimit Paged="TRUE">1000</RowLimit></View>'
  42. $camlQuery.ListItemCollectionPosition = $itemPosition;
  43. $AllItems = $list.GetItems($camlQuery)
  44. $Context.Load($AllItems)
  45. $Context.ExecuteQuery()
  46. $itemPosition = $AllItems.ListItemCollectionPosition;
  47. Write - Host itemPosition: $itemPosition
  48. If($AllItems.Count - gt 0) {
  49. ForEach($item in $AllItems) {
  50. #use internal name
  51. $userEmail = $item["Title"]
  52. if ($userEmail - match 'sample.com') {
  53. $userEmail = $userEmail.Replace('ap.sample.com', 'sample.com')
  54. $userEmail = $userEmail.Replace('am.sample.com', 'sample.com')
  55. $userEmail = $userEmail.Replace('eu.sample.com', 'sample.com')
  56. $userEmail = $userEmail.Replace('jp.sample.com', 'sample.com')
  57. $User = $Context.Web.EnsureUser($userEmail);
  58. $Context.Load($user);
  59. $item["Owner"] = $User;
  60. $item.Update();
  61. $Context.ExecuteQuery()
  62. }
  63. }
  64. #end loop
  65. for all items in list
  66. }
  67. #check
  68. if item count is > 0
  69. }
  70. While($itemPosition - ne $null)
  71. }
  72. #check
  73. if it is a 'do not inventory'
  74. list
  75. }
  76. #end loop
  77. for all lists in site
  78. }
  79. Get - SPOSites - RootWeb $sWeb - Context $Context #recursive call
  80. }
  81. #end loop
  82. for all sites in site collection
  83. }
  84. #Set parameter values
  85. $SiteURL = "http://eu.test.sample.com/sites/AdvEU3/Megamigration"
  86. #$LibraryName = "Documents"
  87. $UserName = "###########"
  88. $PassWord = "############"
  89. $securePassword = ConvertTo - SecureString $PassWord - AsPlainText - Force
  90. $spoCred = New - Object System.Net.NetworkCredential($UserName, $securePassword)
  91. #Setup the context
  92. $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
  93. $ctx.Credentials = $spoCred
  94. $Web = $ctx.Web
  95. $ctx.Load($Web)
  96. $ctx.ExecuteQuery()
  97. 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...