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

[玩转系统] SharePoint Online:使用 PowerShell 更改新菜单中的内容类型顺序

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

SharePoint Online:使用 PowerShell 更改新菜单中的内容类型顺序


要求:使用 PowerShell 更改 SharePoint Online 列表中的内容类型顺序。

如何更改 SharePoint Online 列表或库中的内容类型顺序?

在具有多种内容类型的 SharePoint Online 列表中,您可能想要更改它们在“新建”菜单中显示的顺序,甚至想要在新建按钮中隐藏它们。新菜单用于在库或列表中创建新项目,例如文档或列表项目,内容类型的顺序决定选择哪种类型作为默认类型。通过更改新菜单中内容类型的顺序,您可以确保轻松访问最常用的内容类型。

[玩转系统] SharePoint Online:使用 PowerShell 更改新菜单中的内容类型顺序

  1. 导航到需要管理内容类型的 SharePoint Online 列表或库。
  2. 转到列表或库设置 >> 在设置页面上,单击内容类型部分下的“更改新按钮顺序和默认内容类型”链接。
  3. 在“更改新按钮顺序”页面上,您可以对可用内容类型重新排序,并通过设置可见性复选框启用或禁用它们出现在新菜单中。显示在第一个位置的内容类型将成为列表的默认内容类型。

    [玩转系统] SharePoint Online:使用 PowerShell 更改新菜单中的内容类型顺序

  4. 单击“确定”按钮保存更改。

PowerShell 用于更改 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 = "Contacts"
$ContentTypesOrder = @("Contact", "Item", "Announcement")

#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
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.Load($List.ContentTypes)
    $Ctx.ExecuteQuery()

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

    #Arrange Content Types Order
    ForEach($ContentType in $ContentTypesOrder)
    {
        #Get the Content Type from List and Add to Content Type Order List
        $CType = $List.ContentTypes | Where {$_.Name -eq $ContentType}
        If($CType -ne $Null)
        {
            $ContentTypeOrder.Add($CType.Id)
        }
    }
    #Set Content type Order
    $List.RootFolder.UniqueContentTypeOrder = $ContentTypeOrder
    $List.RootFolder.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Content Type Order has been updated Successfully!"#>
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

请注意,此脚本不会从列表中删除或重新添加任何内容类型。它只是更改内容类型序列的顺序,第一个索引中的内容类型将成为默认内容类型。

要从“新建”菜单中隐藏内容类型,只需将其名称保留在 $ContentTypesOrder 变量中即可!

PnP PowerShell 在新菜单中设置内容类型顺序

这是脚本的 PnP PowerShell 版本!


#Set Variables
$SiteURL= "https://crescent.SharePoint.com/sites/Retail"
$ListName = "Contacts"
$ContentTypesOrder = @("Contact", "Item", "Announcement")

#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List
$List = Get-PnPList -Identity $ListName -Includes ContentTypes,RootFolder.UniqueContentTypeOrder

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

#Arrange Content Types Order
ForEach($ContentType in $ContentTypesOrder)
{
    #Get the Content Type from List and Add to Content Type Order List
    $CType = $List.ContentTypes | Where {$_.Name -eq $ContentType}
    If($CType -ne $Null)
    {
        $ContentTypeOrder.Add($CType.Id)
    }
}

#Set Content type Order
$List.RootFolder.UniqueContentTypeOrder = $ContentTypeOrder
$List.RootFolder.Update()
Invoke-PnPQuery
Write-host -f Green "Content Type Order has been updated Successfully!"

总之,在 SharePoint Online 中的库或列表的新菜单中设置内容类型顺序是一个简单的过程,有助于提高工作流程的效率。通过更改顺序,团队可以确保轻松访问最常用的内容类型,从而减少创建新项目所需的时间和精力。

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

取消回复欢迎 发表评论:

关灯