SharePoint Online: PowerShell kullanarak İçerik Türünü Listelemek için Sütun Ekleme
Gereksinim: SharePoint Online'da içerik türünü listelemek için bir alan ekleyin.
SharePoint Online'da Liste veya Kitaplık İçerik türüne nasıl sütun eklenir?
PowerShell, SharePoint'teki birçok görevi otomatikleştirmeye yardımcı olabilecek güçlü bir betik dilidir. Liste içerik türüne sütun eklemeniz gereken zamanlar olabilir. Bunu manuel olarak yapabilirsiniz ancak çok sayıda listeniz ve içerik türünüz varsa bu zaman alıcı olabilir. Bu blog gönderisi, PowerShell kullanarak SharePoint Online listesi içerik türüne nasıl sütun ekleyeceğinizi gösterecektir.
Bir listenin veya kitaplığın içerik türüne alan eklemek istiyorsanız şu adımları izleyin:
- Liste veya kitaplık ayarlarına gidin >> Liste ayarları sayfasından uygun içerik türü adına tıklayın.
- İçerik Türünü Listele sayfasında, sütunlar bölümünün altında “Mevcut siteden veya liste sütunlarından ekle” bağlantısını tıklayın
Uygun sütun grubunu seçin ve ardından içerik türüne eklemek istediğiniz sütunu seçin, “Ekle” düğmesine tıklayın ve ardından bu sütunu içerik türünüze eklemek için Tamam'a tıklayın.
Mevcut listenin sütunlarından veya site sütunlarından sütun ekleyebilirsiniz.
SharePoint Online: İçerik Türünü Listelemek İçin PowerShell Alanı Eklemek
PowerShell kullanarak içerik türlerini listelemek için alan ekleyelim. Liste içerik türüne sütun eklemek için PowerShell'i nasıl kullanacağınız 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 Add-ColumnToListContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $ContentTypeName,
[Parameter(Mandatory=$true)] [string] $ColumnName,
[Parameter(Mandatory=$false)] [bool] $IsSiteColumn=$True
)
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)
$Ctx.ExecuteQuery()
#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 add: Either site column or existing list's column
If($IsSiteColumn)
{
$ColumnColl = $Ctx.Web.Fields
}
else #List Column
{
$ColumnColl = $List.Fields
}
$Ctx.Load($ColumnColl)
$Ctx.ExecuteQuery()
$Column = $ColumnColl | Where {$_.Title -eq $ColumnName}
#Check if given column exists
if($Column -eq $Null)
{
Write-host "Column '$ColumnName' doesn't exists!" -f Yellow
Return
}
else
{
#Check if column already added to the content type
$FieldCollection = $ContentType.Fields
$Ctx.Load($FieldCollection)
$Ctx.ExecuteQuery()
$Field = $FieldCollection | Where {$_.Title -eq $ColumnName}
if($Field -ne $Null)
{
Write-host "Column '$ColumnName' Already Exists in the content type!" -f Yellow
Return
}
#Add field to content type
$FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$FieldLink.Field = $Column
[Void]$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update($false)
$Ctx.ExecuteQuery()
Write-host "Column '$ColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to Content Type!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ContentTypeName="Projects"
$ColumnName="Head Count"
$IsSiteColumn=$false
#Call the function
Add-ColumnToListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName
“IsSiteColumn” parametre değerine bağlı olarak bu komut dosyası, belirli bir içerik türüne bir site sütunu veya liste sütunu ekleyebilir.
PnP PowerShell, SharePoint Online'da İçerik Türünü Listelemek için Sütun Ekleyecek
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ContentTypeName = "Crescent Project V2"
$FieldName = "Department"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Content Type from the List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
#Add a column list
$Field = Add-PnPField -List $ListName -DisplayName $FieldName -InternalName $FieldName -Type Choice -Choices "Sales","Marketing","Purchase" -AddToDefaultView
#Add the column to Content type
Add-PnPFieldToContentType -Field $Field -ContentType $ContentType
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Site içerik türüne alan eklemeniz gerekiyorsa şunu kullanın: SharePoint Online: PowerShell Kullanarak İçerik Türüne Site Sütunu Nasıl Eklenir?
