当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 设置默认内容类型

作者:精品下载站 日期:2024-12-14 15:05:39 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 设置默认内容类型


要求:使用 PowerShell 设置 SharePoint Online 列表中的默认内容类型。

如何更改 SharePoint Online 中的默认内容类型?

SharePoint 列表可以有多种内容类型。在某些情况下,您可能希望在新按钮中设置默认内容类型。您可以通过设置列出的内容类型的顺序来指定列表或库的默认可用类型。要设置默认内容类型,请执行以下操作:

  1. 导航到需要管理内容类型的 SharePoint Online 列表或库。
  2. 转到列表或库设置 >> 在设置页面上,单击内容类型部分下的“更改新按钮顺序和默认内容类型”链接。
  3. 在“更改新按钮顺序”页面上,您可以将相关内容类型移至第一个位置。显示在第一个位置的内容类型将成为列表的默认内容类型。
  4. 单击“确定”按钮保存更改。

您可以更新列表或库的内容类型排序和可见性详细信息。

[玩转系统] SharePoint Online:使用 PowerShell 设置默认内容类型

使用 PowerShell 在 SharePoint Online 列表中设置默认内容类型

如何在 SharePoint Online 中设置默认内容类型?下面是用于对 SharePoint Online 列表中的内容类型重新排序的 PowerShell 脚本。第一个索引中的项目自动成为默认内容类型。此脚本不会从列表中删除或重新添加内容类型。它只是更改内容类型序列的顺序。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Business Contacts"
$ContentTypeName = "Contacts"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the List Content Types
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List.ContentTypes)
    $Ctx.ExecuteQuery()

    #Create a List for content types
    $ContentTypeOrder = New-Object System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]

    #Add the Default content type to the First Index  
    ForEach($ContentType in $List.ContentTypes)
    {
        If($ContentType.Name -eq $ContentTypeName)
        {
            $ContentTypeOrder.Add($ContentType.Id)
        }
    }
    #Add Rest of the content types to the Content type Order List
    ForEach($ContentType in $List.ContentTypes)
    {
        If($ContentType.Name -ne $ContentTypeName -and $ContentType.Name -ne "Folder")
        {
            $ContentTypeOrder.Add($ContentType.Id)
        }
    }
    
    #Set Content type Order
    $List.RootFolder.UniqueContentTypeOrder = $ContentTypeOrder
    $List.RootFolder.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Default Content Type has been updated successfully!"
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

使用 PnP PowerShell 设置 SharePoint Online 列表中的默认内容类型

要更改 SharePoint Online 列表或库中的默认内容类型,请使用以下 PowerShell:


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Projects"
$ContentTypeName = "Crescent Projects V2"

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

#Set Default Content Type of the List
Set-PnPDefaultContentTypeToList -List $ListName -ContentType $ContentTypeName

请注意,内容类型必须已添加到列表中!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯