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

[玩转系统] SharePoint Online:如何使用 PowerShell 获取内容类型 ID?

作者:精品下载站 日期:2024-12-14 14:21:38 浏览:13 分类:玩电脑

SharePoint Online:如何使用 PowerShell 获取内容类型 ID?


SharePoint 中的内容类型用于定义列表中文档或项目的元数据和行为,并由称为内容类型 ID 的唯一 ID 进行标识。我的另一篇文章介绍了如何使用 SharePoint UI 和 PowerShell for SharePoint On-premises 在 SharePoint 中查找内容类型 ID;这篇文章提供了一个脚本,可以通过编程方式为 SharePoint Online 获取内容类型。

如何在 SharePoint Online 中查找内容类型 ID

在 SharePoint Online 中获取内容类型 ID 的一种方法是使用 SharePoint Online 用户界面。就是这样:

要在 SharePoint Online 中获取内容类型 ID,您可以转到列表/网站内容类型 >> 选择您的内容类型 >> URL 将包含带有内容类型 ID 的“Ctype”参数!

[玩转系统] SharePoint Online:如何使用 PowerShell 获取内容类型 ID?

SharePoint Online:使用 CSOM 获取内容类型 ID

以下是使用 CSOM 获取 SharePoint 中的内容类型 ID 的 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"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ContentTypeName="Crescent Project Report"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get content types of the web
    $ctx.Load($ctx.Web.ContentTypes)
    $Ctx.ExecuteQuery()

    #Get the content type by its name
    $ContentType = $Ctx.Web.ContentTypes | Where {$_.Name -Match $ContentTypeName}
    $Ctx.Load($ContentType)    
    $Ctx.ExecuteQuery()

    #sharepoint online get content type id
    write-host -f Green "Content Type ID:" $ContentType.Id
}
Catch {
    write-host -f Red "Error Getting Content Type ID!" $_.Exception.Message
}

从 SharePoint Online 列表获取内容类型 ID

让我们获取列表内容类型的内容类型 ID:


#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"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$ListName="Projects"
$ContentTypeName="Project Portfolio"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the List and Its content types
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.load($List)
    $Ctx.load($List.ContentTypes)
    $Ctx.ExecuteQuery()

    #Get the content type by its name
    $ContentType = $List.ContentTypes | Where {$_.Name -Match $ContentTypeName}
    $Ctx.Load($ContentType)    
    $Ctx.ExecuteQuery()

    #sharepoint online get content type id
    write-host -f Green "Content Type ID:" $ContentType.Id
}
Catch {
    write-host -f Red "Error Getting Content Type ID!" $_.Exception.Message
}

此脚本使用 PowerShell 从名称获取内容类型 ID。

用于在 SharePoint Online 中获取内容类型 ID 的 PnP PowerShell

使用 Get-PnPContentType cmdlet 检索内容类型及其属性。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Project Proposal V1"

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

#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName

#Get Content Type ID
$ContentType.Id.ToString() 

同样,使用“List”参数获取列表级别的内容类型 ID。例如。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Project Documents"
$ContentTypeName ="Crescent Invoice Template V2"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

#Get Content Type ID
$ContentType.Id.ToString() 

将变量替换为站点 URL、列表和要检索的内容类型的内容类型名称。

总之,可以通过用户界面或使用 PowerShell 来获取 SharePoint Online 中的内容类型 ID。内容类型 ID 是内容类型的唯一标识符,用于以编程方式引用它。通过使用上述方法,您可以轻松检索 SharePoint Online 中特定内容类型的内容类型 ID。

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

取消回复欢迎 发表评论:

关灯