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

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

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

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


要求:从 SharePoint Online 的 SharePoint 列表内容类型中删除字段。

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

以下是如何从 SharePoint Online 列表中的内容类型中删除字段的步骤:

导航到列表 >> 单击设置 >> 列表设置 >> 从可用内容类型中选择相关内容类型 >> 选择您要删除的列。单击“更改内容类型列”页面中的“删除”按钮。

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

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

在这篇博文中,我们将探讨如何从 SharePoint Online 列表的内容类型中删除列。也许该字段是错误添加的或者不再需要。我们还将向您展示如何使用 PowerShell 从 SharePoint Online 列表内容类型中删除列。


#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-ColumnFromListContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $ColumnName
    )

    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)

        #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 delete from content type
        $ContentTypeFieldColl =  $ContentType.Fields
        $Ctx.Load($ContentTypeFieldColl)
        $Ctx.ExecuteQuery()

        $Field = $ContentTypeFieldColl | Where {$_.Title -eq $ColumnName }
        if($Field -eq $null)
        {
            Write-host "Column '$ColumnName' 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($Field.Id)
            $FieldLink.DeleteObject()
            $ContentType.Update($false)
            $Ctx.ExecuteQuery() 

            Write-host "Column '$ColumnName' Deleted from '$ContentTypeName' Successfully!" -ForegroundColor Green 
        }
  }
    Catch {
        write-host -f Red "Error Deleting Column from Content Type!" $_.Exception.Message
    } 
}

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

#Call the function
Remove-ColumnFromListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName

当您从内容类型中删除列时,该字段将成为使用该内容类型的任何列表或库中的本地列!

PnP PowerShell 从列表内容类型中删除字段


#Parameters
$SiteURL = "https://Crescentintranet.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName = ""Crescent Project V2"
$FieldName = "Department0" #@"Project_x0020_Manager" #Internal Name

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Get the Client Context
    $Ctx = Get-PnPContext

    #Get the Content Type from the List
    $ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

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

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

    Write-host -f Green "Field '$FieldName' has been removed from Content Type!"
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

您还可以使用 cmdlet Remove-PnPFieldFromContentType 从内容类型中删除字段:


Remove-PnPFieldFromContentType -Field $Field -ContentType $ContentType

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

取消回复欢迎 发表评论:

关灯