【问题标题】:How can I setup default content type for a Document Library in SharePoint Online using PowerShell?如何使用 PowerShell 在 SharePoint Online 中为文档库设置默认内容类型?
【发布时间】:2016-07-05 01:54:54
【问题描述】:

我正在开发一个 PowerShell 脚本,这个脚本应该执行以下任务:

  1. 创建文档库(例如金融文档)
  2. 将内容类型附加到此文档库
  3. 将此内容类型设置为默认内容类型

我被困在第三点,无法使用 PowerShell 设置默认内容类型。这是我用来更新内容类型的代码:

代码

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

错误

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

请建议我如何使用 Powershell 脚本将内容类型设置为 SharePoint Online 中文档库的默认内容类型。

提前致谢!

【问题讨论】:

    标签: powershell sharepoint office365 sharepoint-online


    【解决方案1】:

    在我的场景中,以下解决方案适用于我的 SharePoint Online 租户:

    $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()
    

    在这个解决方案中,当我尝试直接在$result 中添加$ct 时,它并没有遍历所有内容类型,它对我不起作用,所以我遍历了所有内容类型并检索了 Content 的 ID输入。

    【讨论】:

      【解决方案2】:

      这个怎么样?

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

      【讨论】:

      • 感谢西蒙的回答。我已经尝试过了,但这也不起作用。它需要Microsoft.SharePoint.dll 16.0,因为我正在使用 SharePoint Online。因此,对于 On-Premise 解决方案,这很好,但不适用于我的场景。
      • 什么不起作用,当然我提到 $ctidary=New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentType] 您必须使用 .Add 来添加内容类型。
      • 我已经尝试过 .Add ,但它也给了我同样的错误。所以我用 .insert 试了一下。
      猜你喜欢
      • 2011-03-26
      • 2017-09-25
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 2018-12-17
      • 2022-11-03
      相关资源
      最近更新 更多