Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 5

This is my fourth ​blog post in the Automating SharePoint Online using PowerShell + CSOM Series. Here are the list of old posts in this series.

Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 1​

Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 2​

Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 3

Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 4

Just to reiterate, what we want to achieve is to create site columns and content types and then adding site columns to content type and then finally adding content type to list.

So in this blog post I am going to show you how to Content Type to a SharePoint List/Library using PowerShell and CSOM.

 

$ErrorActionPreference = "silentlycontinue"
    # ......START Authenticate....................
   .\1_Authenticate.ps1
    #........END Authenticate.....................
    #........START - Get Web......................
    $web = $ctx.Web
    $list = $web.Lists.GetByTitle("SPSOttawa")
    $ctx.Load($web)
    $ctx.Load($list)
    $ctx.ExecuteQuery()
     #........END - Get Web...............

    $contentTypes = $web.ContentTypes;
    $ctx.Load($contentTypes);
    $ctx.ExecuteQuery();

    $spsOttawaCT = $contentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB2");
    $ctx.Load($spsOttawaCT);
    $ctx.ExecuteQuery();
    $listContentTypes = $list.ContentTypes
    $ctx.Load($listContentTypes);
    $ctx.ExecuteQuery();

    $myCT = $list.ContentTypes.AddExistingContentType($spsOttawaCT);
    $ctx.load($myCT); 
    $ctx.ExecuteQuery() ;

 

That would add your content type to your list.