Monday, September 2, 2013

How to Change Column Type in SharePoint List using PowerShell

Recently, There was a requirement to change column types from "Single line of text" to "Multiple Lines of Text" in multiple lists. Needless to say, PowerShell is the most efficient way to get such things done in bulk.

Here is the PowerShell Script to change column type in SharePoint 2010 programmatically:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Web where Lists Live
$web= Get-SPWeb ""

#Define Array to hold Lists Names to Scan
$ListNames =  ("Design Documents", "Project Closure", "Metrics")

#Column to Change
$ColumnName ="Closing Notes"

 #Iterate through provided Lists 
 foreach($ListName in $ListNames)
    #Get the List
 $list = $web.Lists.TryGetList($ListName) 
    if( $list -ne $null)
     #Check if the list has our target column to change
     #Get the Column to Change
       $column = $List.Fields[$ColumnName]
       #Change the Column type to "Multiple Lines of Text"
    $column.Type = [Microsoft.SharePoint.SPFieldType]::Note
    Write-Host "Field type updated on: $($List.Title)"


Done! Check SPFieldType in MSDN to get all available column types in SharePoint.

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

Check out these SharePoint products:

1 comment :

  1. Thanks for sharing the tip.
    The solution you gave indeed works, the problem is that the field schema can remain a bit inconsistent. The Type="Text" is correctly changed into Type="Note" but other artefacts can remain. For example, you may have MaxLength set in it, which is not supported for Note fields.
    It seems to be safer to replace the SchemaXml of the field altogether. You can take the old SchemaXml, change Type to Note, remove MaxLength, add NumLines if needed.
    This may look like (when altering a site column):
    [Microsoft.SharePoint.SPField]$field = $web.Fields.GetFieldByInternalName("SingleLineToAlter");
    $field.SchemaXml = '<Field Type="Note" DisplayName="SingleLineToAlter" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" NumLines="6" Group="Custom Columns" ID="{xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" SourceID="{xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" StaticName="SingleLineToAlter" Name="SingleLineToAlter"></Field>'


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...