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

[玩转系统] SharePoint Online:如何使用 PowerShell 将网站栏添加到内容类型?

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

SharePoint Online:如何使用 PowerShell 将网站栏添加到内容类型?


要求:使用 PowerShell 将网站栏添加到 SharePoint Online 中的内容类型。

如何将字段添加到 SharePoint 中的现有内容类型?

网站栏是可重复使用的字段,可以添加到内容类型、列表和库中。通过将网站栏添加到内容类型,您可以更轻松地创建和管理信息。在这篇博文中,我们将引导您完成将网站栏添加到 SharePoint Online 中的内容类型的过程。我们还将为您提供一个 PowerShell 脚本,您可以使用该脚本将网站栏添加到 SharePoint Online 中的内容类型。

默认情况下,标题列会添加到内容类型中。但是,您可能想要添加其他列。

  1. 转到要向网站内容类型添加列的 SharePoint Online 网站。
  2. 单击“设置”齿轮>>单击“站点设置”。
  3. 在“站点设置”中,单击“Web 设计师图库”部分下的“站点内容类型”
  4. 选择要修改的目标内容类型。在网站内容类型页面的“栏”部分下,单击“从现有网站栏添加”链接。

    [玩转系统] SharePoint Online:如何使用 PowerShell 将网站栏添加到内容类型?

  5. 这将带您进入一个页面,您可以在其中选择现有网站栏。选择适当的组并选择要添加的网站栏,然后单击“添加”将该栏移动到“要添加的栏”列表中。

    [玩转系统] SharePoint Online:如何使用 PowerShell 将网站栏添加到内容类型?

让我们看看如何使用 PowerShell 将列添加到 SharePoint Online 中的内容类型。

PowerShell 将网站栏添加到 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 Add-SiteColumnToContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $SiteColumnName
    )

    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 content type
        $ContentTypeColl = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the site        
        $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($ContentType -eq $Null)
        { 
            Write-host "Content Type '$ContentTypeName' doesn't exists in  '$SiteURL'" -f Yellow
            Return 
        }
        #Get the site column to add
        $SiteColumnColl = $Ctx.Web.Fields
        $Ctx.Load($SiteColumnColl)
        $Ctx.ExecuteQuery()
        $SiteColumn = $SiteColumnColl | Where {$_.Title -eq $SiteColumnName}
    
        #Check if given site column exists
        if($SiteColumn -eq $Null)
        {
            Write-host "Site Column '$SiteColumnName' doesn't exists!" -f Yellow
            Return
        }
        else
        {
            #Check if site column already added to the content type
            $FieldCollection = $ContentType.Fields
            $Ctx.Load($FieldCollection)
            $Ctx.ExecuteQuery()
            $Field = $FieldCollection | Where {$_.Title -eq $SiteColumnName}
            if($Field -ne $Null)
            {
                Write-host "Site Column '$SiteColumnName' Already Exists in the content type!" -f Yellow
                Return
            }
    
            #Add a field to content type
            $FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $FieldLink.Field = $SiteColumn
            [Void]$ContentType.FieldLinks.Add($FieldLink)
            $ContentType.Update($true)
            $Ctx.ExecuteQuery() 
       
            Write-host "Site Column '$SiteColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
        }
   }
    Catch {
        write-host -f Red "Error Adding Site Column to Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="ProjectTemplate"
$SiteColumnName="Project Title"

#Call the function
Add-SiteColumnToContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName

如果您需要将同一列添加到许多内容类型,这会很有帮助。

PnP PowerShell 将网站栏添加到内容类型

以下是如何使用 PnP PowerShell 将网站栏添加到 SharePoint Online 中的内容类型:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName = "Crescent Project V2"
$SiteColumnName = "Project_x0020_Manager" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Add Field to Content type
    Add-PnPFieldToContentType -Field $SiteColumnName -ContentType $ContentTypeName
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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

取消回复欢迎 发表评论:

关灯