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

[玩转系统] SharePoint Online:使用 PowerShell 从内容类型中删除网站列

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

SharePoint Online:使用 PowerShell 从内容类型中删除网站列


要求:从 SharePoint Online 的内容类型中删除网站栏。

如何从 SharePoint Online 中的内容类型中删除列?

在这篇文章中,我们将向您展示如何从 SharePoint Online 的内容类型中删除列。当您从内容类型中删除网站栏时,不会将其从 SharePoint Online 网站中删除。它只是从特定内容类型的定义中删除网站栏的关联。要从内容类型中删除列,请执行以下步骤:

  1. 转到站点设置>>站点内容类型(如果是列表级别,请转到列表设置)。
  2. 从页面中选择内容类型,在列部分中,单击要从内容类型中删除的列。
  3. 在更改内容类型页面中,单击“删除”按钮并确认提示以从内容类型中删除该列。

    [玩转系统] SharePoint Online:使用 PowerShell 从内容类型中删除网站列

SharePoint Online:使用 PowerShell 从内容类型中删除列

您是否需要从 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 Remove-ColumnFromContentType()
{ 
    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 from web
        $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 column to delete from content type
        $ContentTypeFieldColl =  $ContentType.Fields
        $Ctx.Load($ContentTypeFieldColl)
        $Ctx.ExecuteQuery()

        $SiteColumn = $ContentTypeFieldColl | Where {$_.Title -eq $SiteColumnName }
        if($SiteColumn -eq $null)
        {
            Write-host "Site Column '$SiteColumnName' doesn't exists in content Type '$ContentTypeName'" -f Yellow
        }
        else
        {
            #Get the field link from content type
            $FieldLinkColl = $ContentType.FieldLinks
            $Ctx.Load($FieldLinkColl)
            
            #Remove field from content type
            $FieldLink = $FieldLinkColl.GetById($SiteColumn.Id)
            $FieldLink.DeleteObject()
            $ContentType.Update($true)
            $Ctx.ExecuteQuery() 

            Write-host "Site Column '$SiteColumnName' deleted from '$ContentTypeName' successfully!" -ForegroundColor Green
        }
  }
    Catch {
        write-host -f Red "Error Deleting Site Column from Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="Project Template"
$SiteColumnName="Project Department" #Display Name of the Field

#Call the function
Remove-ColumnFromContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName

PnP PowerShell 从 SharePoint Online 的内容类型中删除列

如果要从 SharePoint Online 中的内容类型中删除列,可以使用 PnP PowerShell!我将向您展示使用 PowerShell 从内容类型中删除列的示例。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Invoice Template V2"
$FieldName ="Project_x0020_Manager"

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

#Get the Client Context
$Context = Get-PnPContext
 
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName

If($ContentType)
{
    #Get the Field from Content type
    $Field = $ContentType.Fields.GetByInternalNameOrTitle($FieldName)
    $Context.Load($Field) 
    $Context.ExecuteQuery() 

    #Remove the Field from content type
    $ContentType.FieldLinks.GetById($Field.Id).DeleteObject() 
    $ContentType.Update($False) #Update children
    $Context.ExecuteQuery()

    Write-host -f Green "Field '$FieldName' has been removed from Content Type!"
}

或者,您可以在 PnP PowerShell 中使用 Remove-PnPFieldFromContentType cmdlet。具体方法如下:


Remove-PnPFieldFromContentType -Field $FieldName -ContentType $ContentTypeName

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

取消回复欢迎 发表评论:

关灯