Monday, April 21, 2014

Change Content Type of Existing Items in SharePoint using PowerShell

We've an existing document library with 100's of Sales proposal documents in it. To enforce standardization, sales team created a content type "Sales Proposal". We got a requirement to change content type of all existing documents stored in a particular document library with the default content type "Document". In other words, All the documents now need to be assigned a new content type "Sales Proposal".

How to change content type of existing Items (documents) in SharePoint?
You can change the content type of the existing items by getting into edit properties page of the particular item and switching "Content Type" field.

sharepoint powershell change item content type
While its relatively easy and straightforward to change content type of existing items in SharePoint, you do not want to go through each and every single document in the document library and manually assign the new content type to it, as it will be time consuming, isn't it?

Well, let us use PowerShell to make it quick and easy! This script iterates through all documents stored in the given document library, checks for the content type of each document and if it is a specific content type , it  replaces old content type with the new content type.
Before running this script, Add your new content type to your library first! Find my script: Add Content Type to SharePoint Library using PowerShell

PowerShell script to change item content type in SharePoint
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom PowerShell Function to switch Content type of all items stored in a SharePoint library
Function Change-ItemContentType {
        [Microsoft.SharePoint.SPWeb]$Web = $(throw "Please provide the 'Web' Parameter!"),
        [string]$ListName = $(throw "Please provide the 'ListName' Parameter!"),
        [string]$OldContentTypeName = $(throw "Please provide the 'OldContentTypeName' Parameter!"),
        [string]$NewContentTypeName = $(throw "Please provide the 'NewContentTypeName' Parameter!")
    #Get the list
    $List = $Web.Lists[$ListName]

    #Get the new content type
    $NewContentType = $List.ContentTypes[$NewContentTypeName]
    #Checkin any checked out file 
    foreach($item in $list.Items)
        if($item.File.CheckOutStatus -ne "None")
            #check file in            
            $item.File.CheckIn("Checked In By Administrator") 
            write-host "Checked-out file checked-in on item: "$

    #Iterate through each item in the list 
    foreach($item in $list.Items)
        if($item.ContentType.Name -eq $OldContentTypeName)
            #Change the content type
            $item["ContentTypeId"] = $NewContentType.Id
            $item.File.CheckIn("Content type changed to " + $NewContentType.Name, 1)
            Write-host "Content type changed for item: "$

#Variables for processing
$WebURL =""
$ListName ="Q1 Sales Proposals"
$ContentTypeName ="Sales Proposal"

#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)

if($list -ne $null)
    #Call the function to add content type
    Change-ItemContentType $web $list "document" "Sales Proposal"
This will change the old content type "document" of all documents to a new content type 'Sales Proposal'. Next time, when users edits the documents, they'll be prompted to enter required fields from the new content type.

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. Is there any way to do this in SharePoint online?


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...