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 using PnP PowerShell
Here is how to change content type SharePoint online with PowerShell PnP Module
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects"
$ListName = "Project Proposal"
$OldContentTypeName = "Crescent Proposal V1"
$NewContentTypeName = "Crescent Project Proposal V2"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the New Content Type from the List
$NewContentType = Get-PnPContentType -List $ListName | Where {$_.Name -eq $NewContentTypeName}

#Get List Items of Old content Type
$ListItems = Get-PnPListItem -List $ListName -Query "<Query><Where><Eq><FieldRef Name='ContentType'/><Value Type='Computed'>$OldContentTypeName</Value></Eq></Where></Query>"
Write-host "Total Number of Items with Old Content Type:"$ListItems.count

ForEach($Item in $ListItems)
{
    #Change the Content Type of the List Item
    Set-PnPListItem -List $ListName -Identity $Item -ContentType $NewContentType
}
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.