Tìm kiếm trang web

SharePoint Online: Loại bỏ trường khỏi loại nội dung danh sách bằng PowerShell


Yêu cầu: Xóa trường khỏi loại nội dung danh sách SharePoint trong SharePoint Online.

Làm cách nào để xóa trường khỏi loại nội dung danh sách trong SharePoint Online?

Dưới đây là các bước về cách loại bỏ trường khỏi kiểu nội dung trong danh sách SharePoint Online:

Điều hướng đến Danh sách >> Nhấp vào Cài đặt >> Cài đặt danh sách >> Chọn loại nội dung phù hợp từ các loại nội dung có sẵn >> Chọn cột bạn muốn xóa. Nhấp vào nút “Xóa” từ trang “Thay đổi cột loại nội dung”.

PowerShell để xóa cột khỏi loại nội dung danh sách trong SharePoint Online:

Trong bài đăng blog này, chúng ta sẽ khám phá cách xóa một cột khỏi loại nội dung của danh sách SharePoint Online. Có thể trường này được thêm nhầm hoặc không còn cần thiết nữa. Chúng tôi cũng sẽ chỉ cho bạn cách sử dụng PowerShell để xóa một cột khỏi loại nội dung danh sách SharePoint Online.


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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

Khi bạn loại bỏ một cột khỏi kiểu nội dung, trường đó sẽ trở thành cột cục bộ trong bất kỳ danh sách hoặc thư viện nào đang sử dụng kiểu nội dung đó!

PnP PowerShell để xóa một trường khỏi loại nội dung danh sách


#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
}

Bạn cũng có thể sử dụng lệnh ghép ngắn Remove-PnPFieldFromContentType để xóa trường khỏi loại nội dung:


Remove-PnPFieldFromContentType -Field $Field -ContentType $ContentType

Bài viết liên quan