SharePoint Online: Change Content Type of List Items using PowerShell

Requirement: Change Content Type of All Items in a SharePoint Online List.

How to Change Content Type in SharePoint Online?
You can change content type of existing list items by editing the particular item and switching "Content Type" field.
sharepoint online change content type powershell

Well, Lets bulk change content type in SharePoint Online with PowerShell.

Before changing the content type of list items, You must add the content type to the list first: How to Add a Content Type to List in SharePoint Online using PowerShell?

Change Content type in SharePoint Online using PowerShell:
Lets change the content type "Crescent Project V1" with "Crescent Project V2" for all list items (whichever item using the content type "Crescent Project V1"). 
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Change the Content Type of All Items in SharePoint Online list using powershell
Function Change-SPOContentType
{
    param
    (
        [string]$SiteURL  = $(throw "Enter the Site URL!"),
        [string]$ListName = $(throw "Enter the List Name!"),
        [string]$OldContentTypeName = $(throw "Enter the Old Content Type Name!"),
        [string]$NewContentTypeName = $(throw "Enter the New Content Type Name!")
    )
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
        
        #Get the List & List items
        $List = $Web.lists.GetByTitle($ListName)
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()      

        #Get Content Types from the list
        $ContentTypes = $List.ContentTypes
        $Ctx.Load($ContentTypes)
        $Ctx.ExecuteQuery()

        #Get the content type to Update
        $OldContentType = $ContentTypes | Where {$_.Name -eq $OldContentTypeName}
        $NewContentType = $ContentTypes | Where {$_.Name -eq $NewContentTypeName}

        If($NewContentType -ne $Null)
        {
            #Get All List Items with Old Content Type
            $ItemsToUpdate = $ListItems | Where {$_["ContentTypeId"].StringValue -eq ($OldContentType.ID)}
            Write-host "Number of Items Found with Old Content Type:" $ItemsToUpdate.count
            
            #Iterate through each item with old content type and update the conent type
            ForEach($Item in $ItemsToUpdate)
            {
                $Item["ContentTypeId"]  = $NewContentType.Id
                $Item.Update()
            }
            $Ctx.ExecuteQuery()
            write-host "Content Type Updated for All List Items" -ForegroundColor  Green
        }
        else
        {            
            Write-Host "Content Type '$NewContentTypeName' Doesn't Exist in the list. Add the Content Type to the List First!" -ForegroundColor Yellow
        }
    }
    Catch {
        write-host -f Red "Error Changing Content Type!" $_.Exception.Message
    }
}

#Variables
$SiteURL = "https://crescenttech.sharepoint.com/"
$ListName ="Projects"
$OldContentTypeName= "Crescent Project V1"
$NewContentTypeName= "Crescent Project V2"

#Call the function to Change the content type
Change-SPOContentType -SiteURL $SiteURL -ListName $ListName -OldContentTypeName $OldContentTypeName -NewContentTypeName $NewContentTypeName 

This PowerShell changes the content type programmatically for all items in SharePoint Online list.
SharePoint Online: Change Content Type of List Items using PowerShell SharePoint Online: Change Content Type of List Items using PowerShell Reviewed by Salaudeen Rajack on February 02, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.