1
votes

I am developing a PowerShell Script and this script is supposed to do following task:

  1. Create a Document Library (E.g. Financian Documents)
  2. Attach a Content Type to this Document Library
  3. Setup this Content Type as a Default Content Type

I am stuck at third point and not able to setup default content type using PowerShell. Here is the code that I am using to update Content Type:

Code

$ctidary = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]
$ctidary.insert($ctID)
$list.rootfolder.uniquecontenttypeorder = $ctidary
$list.rootfolder.update()
$clientContext.executeQuery()

Error

Cannot find an overload for "insert" and the argument count: "1"

Please suggest me how can I set up a Content Type as a default Content type for a Document Library in SharePoint Online using Powershell Script.

Thanks in advance!

2

2 Answers

1
votes

In my scenario below solution worked for my SharePoint Online Tenant:

$cttitle = "<Title of Content Type>"
$list = $clientContext.web.lists.GetByTitle("<Title of List/Library>")
$clientContext.load($list)
$clientContext.load($list.contenttypes)
$clientContext.executeQuery()

$currentOrder = $list.ContentTypes
$result = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]

foreach ($ct in $currentOrder)
{
    if ($ct.Name.Contains($cttitle))
    {
        $result.Add($ct.Id)
    }
}

$list.RootFolder.UniqueContentTypeOrder = $result
$list.RootFolder.Update()

$clientContext.executeQuery()

In this solution, instead of iterating through all the content types when I tried to add $ct directly in $result, it didn't worked for me, So I iterated through all the content type and retrieved ID of Content Type.

0
votes

How about this?

$ctidary = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]
$ctidary.Add($ctID)
$list.RootFolder.UniqueContentTypeOrder = $ctidary
$list.RootFolder.Update()