SharePoint Online: PowerShell kullanarak İçerik Türünden Site Sütunu Kaldırma


Gereksinim: SharePoint Online'daki İçerik Türünden Site Sütunu kaldırın.

SharePoint Online'da İçerik Türünden Bir Sütun Nasıl Kaldırılır?

Bu yazıda, SharePoint Online'daki içerik türünden bir sütunun nasıl kaldırılacağını size göstereceğiz. Bir içerik türünden site sütununu kaldırdığınızda, bu sütun SharePoint Online sitesinden silinmez. Yalnızca site sütununun ilişkisini belirli içerik türünün tanımından kaldırır. İçerik türünden bir sütunu kaldırmak için şu adımları izleyin:

  1. Site Ayarları >> Site İçerik Türleri'ne gidin (Liste düzeyindeyse Liste Ayarları'na gidin).
  2. Sayfadan içerik türünü seçin, Sütunlar bölümünde içerik türünden kaldırmak için sütuna tıklayın.
  3. İçerik türünü değiştirme sayfasında, “Kaldır” düğmesine tıklayın ve içerik türünden sütunun silinmesi istemini onaylayın.

SharePoint Online: PowerShell kullanarak İçerik Türünden Bir Sütunu Kaldırma

SharePoint Online'daki içerik türünden bir sütunu kaldırmanız mı gerekiyor? PowerShell bunu kolaylaştırıyor! PowerShell kullanarak içerik türünden bir sütunun nasıl kaldırılacağı aşağıda açıklanmıştır:


#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-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'daki İçerik Türünden Sütunu Kaldıracak

SharePoint Online'daki bir içerik türünden bir sütunu kaldırmak istiyorsanız PnP PowerShell'i kullanabilirsiniz! Size bir içerik türünden bir sütunu silmek için PowerShell kullanmanın bir örneğini göstereceğim.


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

Alternatif olarak PnP PowerShell'de Remove-PnPFieldFromContentType cmdlet'ini kullanabilirsiniz. İşte nasıl:


Remove-PnPFieldFromContentType -Field $FieldName -ContentType $ContentTypeName

İlgili makaleler