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

[玩转系统] SharePoint Online:使用 PowerShell 从内容类型获取列

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

SharePoint Online:使用 PowerShell 从内容类型获取列


要求:使用 PowerShell 获取 SharePoint Online 中的内容类型字段。

PowerShell 从 SharePoint Online 中的内容类型获取列:

您是否曾经需要获取 SharePoint Online 内容类型中所有字段的列表?那么,您可以从内容类型设置页面获取内容类型的所有字段。

[玩转系统] SharePoint Online:使用 PowerShell 从内容类型获取列

导出特定内容类型的所有元数据怎么样? 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 Get-SPOContentTypeFields()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )
 
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #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 web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()

        #Get the content type from web
        $ContentTypeColl = $Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
 
        #Get the content type
        $CType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($CType -ne $Null)
        {
            $Ctx.Load($CType.FieldLinks)
            $Ctx.ExecuteQuery()

            #Get columns from the content type
            ForEach($FieldLink in $CType.FieldLinks)
            {           
                Write-Host -f Green $FieldLink.Name
            } 
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
            Return 
        }
   }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Announcement"
 
#Call the function
Get-SPOContentTypeFields -SiteURL $SiteURL -ContentTypeName $ContentTypeName

SharePoint Online:PowerShell 从列表中获取内容类型字段

这是我的 PowerShell,用于获取列表内容类型及其列,循环遍历它们,并输出包含以下数据的 CSV 文件:

  • 栏目标题
  • 列内部名称
  • 列号
  • 列组
  • 栏目说明

#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 Get-SPOListCTypeFields()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $CSVPath
    )
 
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get content types from web
        $ContentTypes = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypes)
        $Ctx.ExecuteQuery()
 
        #Get the content type
        $CType = $ContentTypes | Where {$_.Name -eq $ContentTypeName}
        If($CType -ne $Null)
        {
            #Get Columns from the content type
            $Ctx.Load($CType.Fields)
            $Ctx.ExecuteQuery()
 
            $ResultArray = @()
            #Loop through the Fields in the Content Type
            ForEach ($Field in $CType.Fields)
            {
                #Create a new custom object to hold our row of data with property names:
                $Result = New-Object PSObject
                $Result | Add-Member -MemberType NoteProperty -Name "Title" -Value $Field.Title
                $Result | Add-Member -MemberType NoteProperty -Name "Internal Name" -Value $Field.InternalName
                $Result | Add-Member -MemberType NoteProperty -Name "ID" -Value $Field.Id
                $Result | Add-Member -MemberType NoteProperty -Name "Group" -Value $Field.Group
                $Result | Add-Member -MemberType NoteProperty -Name "Description" -Value $Field.Description
 
                #Add the object to array
                $ResultArray += $Result
            }
            $ResultArray | Format-table -AutoSize
            # Export the results to CSV
            $ResultArray | Export-Csv $CSVPath -NoTypeInformation -force

            Write-host "Content Type Columns Exported to CSV successfully!'" -f Green
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
        }
   }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"
$ListName ="News"
$ContentTypeName="Announcement"
$CSVPath = "C:\Temp\CtypeFields.csv"

#Call the function
Get-SPOListCTypeFields -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -CSVPath $CSVPath

PnP PowerShell 从内容类型获取所有字段

让我向您展示如何使用 PowerShell 从内容类型获取字段。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID ="0x0104004A217DA260E04940AC9DB4A010797423"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeID
 
#Get All Fields from the Content Type
$ContentTypeFields = Get-PnPProperty -ClientObject $ContentType -Property Fields

#Get Field Title and ID
$ContentTypeFields | Select Title, ID

同样,要从列表内容类型获取所有字段,请使用:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ContentTypeName ="Crescent Project V2"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the List content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
 
#Get All Fields from the Content Type
$ContentTypeFields = Get-PnPProperty -ClientObject $ContentType -Property Fields

#Get Field Title, Internal Name and ID
$ContentTypeFields | Select Title, InternalName, ID

包起来

总之,我们了解了如何使用 PowerShell 检索与 SharePoint Online 中的特定内容类型关联的字段。通过使用提供的脚本,您可以连接到 SharePoint Online 环境并检索与特定内容类型关联的字段的详细信息。此信息可用于管理目的,例如管理您的 SharePoint 内容或用于报告目的。借助 PowerShell,您可以自动执行和简化任务,从而使您的 SharePoint Online 管理任务更加高效。

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

取消回复欢迎 发表评论:

关灯