Synch User Fields when copying lists from one sitecollection to another

If you copy a list from one sitecollection to another one with a list template and the copied list has user fields than you'll have other users in the destionation list.

This occures because SharePoint saves all user in a own UserInfo table for each sitecollection. A user in SiteCollection 1 can have the ID 5 and in another the ID 3.

As the SPUserField is a LookUp Field to the User Info List the name of the User in the destination list can change.

Two little PowerShell Scripts can help here. The first one reads the user info for a field in the source list and creates a csv with the information. The second one reads the csv and updates the destination list. Copy the code to two .ps1 files.

Open the SharePoint 2010 Management Shell and run the "Dump user Info" script to dump the infos to a .csv file. Afterwards run the second script to update the destination list.


Dump User Info

 # Adapt these values  
 $web = Get-SPWeb "http://sharepoint/sites/sourcelist/"   
 $list = $web.Lists["SourceList"]  
 $field = "UserField"  
 $csv = "C:\temp\user.csv"  
 # ---------------------  
 $userInfos = @()  
 foreach($item in $list.Items) {  
   $userFld = [Microsoft.SharePoint.SPFieldUser] $item.Fields.GetField($field)    
   if ($null -ne $userFld) {          
     if ($null -ne $item[$field]) {  
       $fieldVal = new-object Microsoft.SharePoint.SPFieldUserValue($web, $item[$field].ToString())      
       if ($null -ne $fieldVal){  
         $user = $fieldVal.User        
         $userInfos += New-Object PSObject -Property @{  
           Title = $item.Title  
           Login = $user.LoginName  
         }  
       }  
     }  
   }else{  
     Write-Host "Field: " $field " not found in list!" -foregroundcolor red  
   }  
 }  
 $userinfos | Export-CSV $csv -NoTypeInformation  
 Write-Host "END - Script"  

UpdateUser Info

 $web = Get-SPWeb "http://sharepoint/sites/io/"   
 $list = $web.Lists["Services"]  
 $field = "Verantwortlich_x0020__x0028_ad_x"  
 $items = $list.Items  
 $csv = "C:\temp\user.csv "  
 $log = "C:\temp\usersynch.log"  
 Start-Transcript -path $log   
 $import = Import-Csv $csv  
 foreach ($obj in $import)  
 {   
   Write-Host "Searching for " $obj.Title -foregroundcolor blue  
   $items | Where-Object { $_.Title -eq $obj.Title} | foreach {  
     Write-Host " >> Updating item to new user: " $obj.Login -foregroundcolor green  
     try  
     {  
       $_[$field] = $web.EnsureUser($obj.Login).ID  
       $_.Update()      
     } catch [Microsoft.SharePoint.SPException] {  
       Write-Host " >> Error: could not find user: " $obj.Login -foregroundcolor red            
     }   
   }  
   Write-Host ""  
 }  
 Write-Host "End Synch Process"  
 Stop-Transcript  

Comments

Anonymous said…
Thx, work with author field ;) Islay MatiK

Popular posts from this blog

Ways to redirect http requests in SharePoint

Open SharePoint 2010/2013 Display, Edit, New Forms in Modal Dialogs

How to create a FAQ in SharePoint with OOB features