SharePoint 2013: Change The Content Type Using Powershell Script

Introduction

Content types are nothing, just a set of columns that are grouped together to serve a specific purpose. For example, you could create a set of columns like school student details and it can be useful for many lists/libraries.

And the content type is such that if anytime you need a specific set of columns, you can add it to your list/library. So you don’t want to use it again and it is reusable.

A CT can only exist within a Site Collection. If you want to reuse it in another, you will have to recreate it but Microsoft having alternative way in SharePoint, called the Content Type Hub (CTH).

You can use many content types to single list/library.

Here, I would like to explain how to create a content type very quickly.

Go to, Site settings, Under Web designer Galleries, then select site Content Types, create a Content Types.

Steps:

step

step
Then Content type is created and columns looks like below,

type

Then you can add Site Columns in this Content Type using the “Add from existing site columns” option and save it.

Go to the list settings and select the "Advanced settings" and select the check box like "Allow management of content types" true,

type

setting

At last go to the List Settings Page under Content Types AND select “Add from existing site content types” to add your newly added Content Type.

Then select your custom content type it will be assigned to your list/library.

Now we will go to the  actual requirement. Here we will use the PowerShell script to change the existing attached content type to new one.

Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

  1. #Variables Declaration  
  2.   
  3. Function CTChange {  
  4.     param(  
  5.         [Microsoft.SharePoint.SPWeb]$Web = $(throw "Enter  the 'Web' url"),  
  6.         [string]$ListName = $(throw "Enter the  'ListName'"),  
  7.         [string]$OldContentTypeName = $(throw "Enter the 'ExistingContentTypeName'"),  
  8.         [string]$NewContentTypeName = $(throw "Enter the 'NewContentTypeName' ")  
  9.     )     
  10.     
  11.       
  12.     $GetList = $Web.Lists[$ListName]  
  13.    
  14.     $NewCT = $GetList.ContentTypes[$NewContentTypeName]  
  15.     
  16.      
  17.     foreach($item in $GetList.Items)  
  18.     {  
  19.         if($item.File.CheckOutStatus -ne "None")  
  20.         {  
  21.                         
  22.             $item.File.CheckIn("File Checked In By Administrator")  
  23.             write-host "checked-in on item: "$item.id  
  24.         }  
  25.     }  
  26.    
  27.       
  28.     foreach($item in $GetList.Items)  
  29.     {  
  30.         if($item.CT.Name -eq $OldContentTypeName)  
  31.         {         
  32.               
  33.             $item.File.CheckOut()  
  34.             $item["ContentTypeId"] = $NewContentType.Id  
  35.             $item.SystemUpdate()  
  36.             $item.File.CheckIn("Content type changed to " + $NewContentType.Name, 1)  
  37.             Write-host "Content type changed for item: "$item.id  
  38.         }  
  39.     }  
  40. }  
  41.    
  42.   
  43. $WebURL ="http://gowtham.sharepoint.com"  
  44. $ListName ="DataEntry"  
  45. $ContentTypeName ="NewDataEntry"  
  46.    
  47.   
  48. $Web = Get-SPWeb $WebURL  
  49. $List = $Web.lists.TryGetList($ListName)  
  50.    
  51. if($list -ne $null)  
  52. {  
  53.       
  54.     CTChange $web $list "document" "NewDataEntry"  
  55. }  
Yes ! Content type has been changed in the SharePoint List/Library.

Read more articles on SharePoint:

Up Next
    Ebook Download
    View all
    Learn
    View all