Change Content Type of Existing Items in SharePoint using PowerShell
Requirement:
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.
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.
PowerShell script to change item content type in SharePoint
Here is my another post PowerShell for SharePoint Online to Change Content Type
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.
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 { param( [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: "$item.id } } #Iterate through each item in the list foreach($item in $list.Items) { if($item.ContentType.Name -eq $OldContentTypeName) { #Change the content type $item.File.CheckOut() $item["ContentTypeId"] = $NewContentType.Id $item.SystemUpdate() $item.File.CheckIn("Content type changed to " + $NewContentType.Name, 1) Write-host "Content type changed for item: "$item.id } } } #Variables for processing $WebURL ="http://sales.crescent.com" $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.
Here is my another post PowerShell for SharePoint Online to Change Content Type
Is there any way to do this in SharePoint online?
ReplyDeleteYes. Here you go: How to change content type in SharePoint Online using PowerShell
DeleteProbably with Patterns and practises.
ReplyDeleteHello, first good job!
ReplyDeleteI have a question: This scenario is specific to a list or library. Is it possible to perform for the entire Site Collection?
Another detail: We deal with the change from "Document" to "Sales Proposal". I ask, can I include more parameters? For example:
Do I need to change the content types that are "Document" or "Proposals" or "Files" to "Sales Proposal"?
Thank you and regards from Portugal.
Rafael SharePoint
1. You can iterate through each list from the site collection, as:
Delete#Iterate through All webs of the site collection
Get-SPSite "http://your-sitecoll-url" | Get-SPWeb -Limit All | ForEach-Object
#Iterate through All Lists
Foreach ($List in $_.Lists)
{
#Do something here
}
2. Yes, Add more parameters to the function and use "OR" operator to check
If($item.ContentType.Name -eq $OldContentTypeName1 or $item.ContentType.Name -eq $OldContentTypeName2 or $item.ContentType.Name -eq $OldContentTypeName3)
{
#Do something here
}
How to change content type of documents in SharePoint in all versions of it
ReplyDelete