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

[玩转系统] SharePoint Online:使用 PowerShell 启用内容类型

作者:精品下载站 日期:2024-12-14 14:09:55 浏览:16 分类:玩电脑

SharePoint Online:使用 PowerShell 启用内容类型


顾名思义,内容类型定义内容的类型,例如项目或文档。在现实场景中,内容类型可以指发票模板、项目计划文档等。内容类型提供了一种将相关列(元数据)、文档模板、工作流程和策略分组在一起的有效方法,并有助于标准化模板以供重复使用。通常,内容类型是在顶级站点创建的,因为它们本质上是分层的。换句话说,在根站点创建的内容类型会自动继承到其所有子站点。这可以节省大量时间,因为您只需创建该内容类型一次并在需要时重复使用它。

如何在 SharePoint Online 中启用内容类型?

在将内容类型添加到列表或库之前,我们必须在列表或库设置中启用内容类型管理。要在 SharePoint Online 列表中启用内容类型,请按以下说明分步操作:

  1. 导航至列表>>设置>>列表设置>>高级设置。
  2. 将“允许管理内容类型?”设置为“是”并点击“确定”按钮保存您的更改。

    [玩转系统] SharePoint Online:使用 PowerShell 启用内容类型

如果您返回列表设置,您将找到一个名为“内容类型”的部分,其中列出了特定列表或库的默认内容类型(例如,自定义列表的项目!)。您现在可以将任何现有的内容类型添加到列表中!

PowerShell 使用 PowerShell 在 SharePoint Online 列表中启用内容类型

如果您是 SharePoint Online 用户,您可能需要不时启用内容类型,这可以使用 PowerShell 轻松完成。让我向您展示如何在 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"

#function to turn ON Content Type in SharePoint Online list or library
Function Enable-ContentTypes()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List object
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if content types are already enabled
        If($List.ContentTypesEnabled -eq $True)
        {
            Write-host "Content Types Already Enabled in the given List!" -ForegroundColor Yellow
        }
        else
        {
            #enable content types in the list
            $List.ContentTypesEnabled=$True
            $List.Update()
            $Ctx.ExecuteQuery()

            Write-host "Content Types Enabled for given List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error enabling Content Type!" $_.Exception.Message
    } 
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

#Call the function
Enable-ContentTypes -SiteURL $SiteURL -ListName $ListName

如果您需要快速启用特定列表或库的内容类型,或者想要禁用内容类型,这会很有帮助。

PnP PowerShell 在 SharePoint Online 列表上启用内容类型:

您还可以使用 PnP PowerShell 启用列表中的内容类型。如果您想要自定义列表的功能或者需要创建新的内容类型并将其添加到列表中,这会很有帮助。让我向您展示如何使用 PowerShell 在 SharePoint Online 列表中启用内容类型:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"

#Connect to Site collection from PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Enable Content Type
Set-PnPList -Identity $ListName -EnableContentTypes $True

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

取消回复欢迎 发表评论:

关灯