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

[玩转系统] SharePoint Online:使用 PowerShell 将列添加到列表内容类型

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

SharePoint Online:使用 PowerShell 将列添加到列表内容类型


要求:添加一个字段以列出 SharePoint Online 中的内容类型。

如何向 SharePoint Online 中的列表或库内容类型添加列?

PowerShell 是一种功能强大的脚本语言,可以帮助自动执行 SharePoint 中的许多任务。有时您可能需要向列表内容类型添加列。您可以手动执行此操作,但如果您有大量列表和内容类型,这可能会非常耗时。本博客文章将向您展示如何使用 PowerShell 将列添加到 SharePoint Online 列表内容类型。

如果要将字段添加到列表或库的内容类型,请按照以下步骤操作:

  1. 转到列表或库设置 >> 从列表设置页面单击相应的内容类型名称。
  2. 在“列表内容类型”页面的“栏”部分下,单击“从现有网站或列表栏添加”链接
  3. 选择适当的列组,然后选择要添加到内容类型的列,单击“添加”按钮,然后单击“确定”将该列添加到您的内容类型。

    [玩转系统] SharePoint Online:使用 PowerShell 将列添加到列表内容类型

您可以从现有列表的列或网站列中添加列。

SharePoint Online:PowerShell 将字段添加到列表内容类型

让我们使用 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 Add-ColumnToListContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $ColumnName,
        [Parameter(Mandatory=$false)] [bool] $IsSiteColumn=$True
    )

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

        #Get the content type from list
        $ContentTypeColl = $List.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the list        
        $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($ContentType -eq $Null)
        { 
            Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow
            Return 
        }

        #Get the column to add: Either site column or existing list's column
        If($IsSiteColumn)
        {
            $ColumnColl = $Ctx.Web.Fields
        }
        else #List Column
        {
            $ColumnColl = $List.Fields
        }
        $Ctx.Load($ColumnColl)
        $Ctx.ExecuteQuery()
        $Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
    
        #Check if given column exists
        if($Column -eq $Null)
        {
            Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
            Return
        }
        else
        {
            #Check if column already added to the content type
            $FieldCollection = $ContentType.Fields
            $Ctx.Load($FieldCollection)
            $Ctx.ExecuteQuery()
            $Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
            if($Field -ne $Null)
            {
                Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
                Return
            }
    
            #Add field to content type
            $FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $FieldLink.Field = $Column
            [Void]$ContentType.FieldLinks.Add($FieldLink)
            $ContentType.Update($false)
            $Ctx.ExecuteQuery() 
       
            Write-host "Column '$ColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
        }
   }
    Catch {
        write-host -f Red "Error Adding Column to Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Projects"
$ColumnName="Head Count"
$IsSiteColumn=$false

#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName 

根据“IsSiteColumn”参数值,此脚本可以将网站栏或列表栏添加到给定的内容类型。

PnP PowerShell 在 SharePoint Online 中添加列以列出内容类型


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName = "Crescent Project V2"
$FieldName = "Department" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Get the Content Type from the List
    $ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

    #Add a column list
    $Field =  Add-PnPField -List $ListName -DisplayName $FieldName -InternalName $FieldName -Type Choice -Choices "Sales","Marketing","Purchase" -AddToDefaultView

    #Add the column to Content type
    Add-PnPFieldToContentType -Field $Field -ContentType $ContentType
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

如果需要向网站内容类型添加字段,请使用:SharePoint Online:如何使用 PowerShell 将网站栏添加到内容类型?

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

取消回复欢迎 发表评论:

关灯