Saturday, January 25, 2014

Copy SharePoint List Column Values from One to Another

In some situations, we may have to copy SharePoint list column values from one column to another. Say, for E.g.

  • OOTB column doesn't provide a way to enter more than 255 characters in Multiple Lines of Text field. As user already entered values in it and wants to make it allow more than 255 chars.
  • When a SharePoint field's internal name needs to be changed.
  • There is requirement to add a new column, whose values are partially based on an existing column
While the Data Sheet view is one solution, Its not available for all lists. And even in datasheet view, some columns like people picker values can't be copy/pasted. So, the solution is: PowerShell

Here is the PowerShell script to copy SharePoint list / library field data from one column to another column:
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Using Get-SPSite in MOSS 2007
function global:Get-SPSite($url)
 {
    return new-Object Microsoft.SharePoint.SPSite($url)
 }

Function global:Get-SPWeb($url)
{
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();
       
            }
    return $web
}
 
 #Parameters
 $web = Get-SPweb "http://sharepoint.crescent.com/"
 $listName = "Pictures"
 
 #Use the Display Names
 $CopyFromColumnName = "Description" #column copy source
 $CopyToColumnName = "Desc"  #destination column

  #Get the List
  $list = $web.lists[$ListName]

     #Get all Items  
     $Items = $list.Items

     foreach ($Item in $items)
        {
            #copy data from one column to another
            $item[$copyToColumnName] = $item[$copyFromColumnName]

            #Do a system update to avoid Version and to Keep same metadata
            $item.SystemUpdate($false)
        } 



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...