kwizcom banner advertisement

How to Rename SharePoint Column Programmatically (PowerShell / C#)?

Here is how we can rename SharePoint list or site field or column programmatically with PowerShell and C# object model code:

PowerShell Code to Rename a SharePoint Field:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$Web= Get-SPWeb "http://sharepoint.crescent.com"

#Get the "Risk Metrics" List
$list= $web.Lists.TryGetList("Risk Metrics")

#Get "Project Name" Field
$field = $list.fields | where {$_.Title -eq "Project Name"}

#Set Field Display Name and Update
$field.Title ="Program Name"
$field.Update()

Rename a SharePoint Field Programmatically (C#):
using(SPSite site=new SPSite("http://sharepoint.crescent.com"))
 {
    //Get the "Risk Metrics" List
     SPList list = site.RootWeb.Lists["Risk Metrics"];

    //Get "Project Name" Field
    SPField field = list.Fields["Project Name"];

    //Set Field Display Name and Update
    field.Title = "Program Name";
    field.Update();

    //You can also use: list.Fields["Project Name"].Title = "Project Name";
     //list.Update();                 
  } 

PowerShell to change field display name in SharePoint site collection:
Add-PSSnapin "Microsoft.SharePoint.Powershell" -EA SilentlyContinue

#Variables
$SiteURL = "http://sharepoint.crescent.com/"
$OldColumnName ="Department"
$NewColumnName ="Entity"

#Get the Site
$site = Get-SPSite $SiteURL

 #Iterate through each web (Sub-Site)
 foreach ($web in $site.AllWebs)
 {   
  #Array to Hold Lists with specific column
  $ListsToProcess= @()
  #Get All Lists
  $Lists = $web.Lists
  #ITerate through each list
     foreach ($list in $lists)
     {
         #Iterate through each column
         foreach ($column in $list.Fields)
         {
             #Check for the specific column
             if ($column.Title -eq $OldColumnName)
             {
                   Write-Host "Found the Column in list: " $list.Title " at "$web.Url
                   #Send it to an Array
                   $ListsToProcess+=$List
             }
         }
     }
  
  #Process each list and rename the columns
  foreach ($list in $ListsToProcess)
     {
         #Get the Old column by its name
         $column = $list.fields | where {$_.Title -eq $OldColumnName}
   
         #rename sharepoint list column powershell
         $column.Title = $NewColumnName
         $column.Update()
  }
  $web.Dispose()
 }
$site.Dispose()
Similarly, to rename a site column, use:
$Column = $web.Fields.getFieldByInternalName("Title")
$Column.Title = "Project Title"
$Column.PushChangesToLists = $True
$Column.Update()
How to Rename SharePoint Column Programmatically (PowerShell / C#)? How to Rename SharePoint Column Programmatically (PowerShell / C#)? Reviewed by Salaudeen Rajack on April 01, 2013 Rating: 5

1 comment:

  1. Hi Salaudeen,

    How to rename the column/field using sharepoint webservices ?Can i do that ?

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.