SharePoint'te İçerik Türüne Alan Eklemek-Kaldırmak için PowerShell
İçerik türüne alan ekleme veya mevcut içerik türünden alan kaldırma gereksiniminiz varsa bu PowerShell betiğini kullanın:
İçerik türüne alan eklemek için PowerShell betiği:
#Get Web Object
$web = Get-SPWeb "https://sharepoint.company.com"
#Get Content Type and Field
$ContentType=$web.ContentTypes["Content-Type-Name"]
$FieldToAdd=$web.Fields["Field-Name"]
#Add Field to Content type
$FieldLink=New-Object Microsoft.SharePoint.SPFieldLink($FieldToAdd)
$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update()
PowerShell kullanarak içerik türüne site sütunu eklemek için biraz hata işleme ekleyelim ve yeniden kullanılabilir bir işlev yapalım!
SharePoint'teki İçerik türüne Site Sütunu Eklemek için PowerShell
#Add SharePoint Snap-in
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Add-FieldToContentType([Microsoft.SharePoint.SPWeb] $web, [string]$ContentTypeName, [string]$SiteColumnName)
{
#Get Content Type and Field (Site column) objects
$ContentType=$web.ContentTypes[$ContentTypeName]
#Check if the content type exists
if($ContentType -ne $null)
{
#Check if the content type has the Field already
if(!$ContentType.Fields.ContainsField($SiteColumnName))
{
#Check if the site column exists
if($web.Fields.ContainsField($SiteColumnName))
{
$FieldToAdd=$web.Fields.GetField($SiteColumnName)#[$SiteColumnName]
#Add Site column to the content type
$FieldLink= New-Object Microsoft.SharePoint.SPFieldLink($FieldToAdd)
$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update($true)
Write-Host "Site Column Field Added to the Content Type!" -ForegroundColor Green
}
else
{
Write-Host "Site Column Not Found!" -ForegroundColor Red
}
}
else
{
Write-Host "Field Exists Already!" -ForegroundColor Red
}
}
else
{
Write-Host "Content type not found!" -ForegroundColor Red
}
}
PowerShell kullanarak alanı içerik türünden kaldırın:
Bir site sütununu içerik türünden programlı olarak nasıl silebileceğiniz aşağıda açıklanmıştır
Function Remove-FieldFromContentType([Microsoft.SharePoint.SPWeb] $web, [string]$ContentTypeName, [string]$FieldNameToRemove)
{
#Get Content Type and Field (Site column) objects
$ContentType=$web.ContentTypes[$ContentTypeName]
#Check if the content type exists
if($ContentType -ne $null)
{
#Check if the content type has the Field
if($ContentType.Fields.ContainsField($FieldNameToRemove))
{
#Rempve the Field from the content type
$ContentType.FieldLinks.Delete($FieldNameToRemove)
$ContentType.Update($true)
Write-Host "Field removed from the Content Type!" -ForegroundColor Green
}
else
{
Write-Host "Field Doesn't Exists in the Content Type!" -ForegroundColor Red
}
}
else
{
Write-Host "Content type not found!" -ForegroundColor Red
}
}
Şimdi PowerShell kullanarak site sütununu içerik türüne eklemek veya içerik türünden kaldırmak için ilgili fonksiyonu çağıralım:
#Configuration parameters
$WebURL="https://portal.crescent.com"
$ContentTypeName="CrescentInvestments"
$FieldName="FullName" # Internal Name of the field
#Get the Web
$Web = Get-SPWeb -Identity $WebURL
#Call the method to Add field to content type
Add-FieldToContentType $Web $ContentTypeName $FieldName
#Remove-FieldFromContentType $Web $ContentTypeName $FieldName
ve sonuç buraya geliyor:
Bu, verilen İçerik Türünü Programlı Olarak Günceller. İşte SharePoint'e site sütunu eklemek için PowerShell betiğiyle ilgili başka bir yazım: PowerShell kullanarak SharePoint'te Site Sütunu Oluşturma
