PowerShell kullanarak SharePoint'te Site Sütunu Oluşturma
Site sütunları, SharePoint sitelerinize ekleyebileceğiniz yeniden kullanılabilir alanlardır ve istediğiniz sayıda liste veya kitaplıkta kullanılabilir! Site sütunlarını eklemenin PowerShell yöntemi, site sütunlarını toplu olarak eklemeniz gerektiğinde son derece faydalıdır. DEV ortamınızda oluşturulmuş belirli site sütunlarınız olabilir ve bunların STAGING veya PROD ortamında olmasını isteyebilirsiniz. Bunları SharePoint kullanıcı arayüzüyle tek tek mi oluşturacaksınız? HAYIR! Bu çok telaşlı olurdu. Bu yazıda SharePoint'te site sütunları oluşturmak için PowerShell scriptlerimi paylaşıyorum.
PowerShell ile SharePoint'e Site Sütunu Ekleme
PowerShell ile SharePoint 2016'da site sütunu oluşturmanın basit bir örneği:
#Get the Root Web
$RootWeb = Get-SPWeb "https://intranet.crescent.com"
#Create a text Field site column
$RootWeb.Fields.Add("ProjectCode","Text",$false)
$SalaryField = $RootWeb.Fields.Add("Crescent-Salary", "Currency", $false)
PowerShell kullanarak site sütunları oluşturun - Toplu olarak oluşturalım!
Şimdi site sütunlarını toplu olarak ekleyelim. Sütun adlarınızı diziye girin: $SiteColumnsToAdd ve kodu çalıştırın.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
# List of Site Column Names to be Added
$SiteColumnsToAdd = @("Branch Name", "Branch Code", "Destination")
ForEach($SiteColumn in $SiteColumnsToAdd)
{
if (!$RootWeb.Fields.ContainsField($SiteColumn))
{
#Remove Space in the Field's Internal Name
$InternalName= $SiteColumn.Replace(" ","")
#Add new site column
$RootWeb.Fields.Add($InternalName, "Text", $false)
#Get the site column
$Addedcolumn = $RootWeb.Fields.GetField($InternalName)
#Set the Column Title and Group
$Addedcolumn.Group="Crescent Columns"
$Addedcolumn.Title=$SiteColumn
$Addedcolumn.update()
Write-host "Site column $($SiteColumn) has been Added Successfully!"
}
}
$RootWeb.Dispose()
Site sütunu oluşturmak için SharePoint 2013 PowerShell - Seçim Alanı:
SharePoint'te site sütunu oluşturmak için başka bir PowerShell örneği:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
#Create a Choice Field site column
$RootWeb.Fields.Add("Departments","Choice",$false)
#Get the field by internal Name
$ChoiceField=$RootWeb.Fields.GetField("Departments")
#Set the Site column Group
$ChoiceField.Group="Crescent Columns"
#Add Choices
$ChoiceField.Choices.Add("Operations")
$ChoiceField.Choices.Add("Marketing")
$ChoiceField.Choices.Add("Sales")
$ChoiceField.Choices.Add("Finance")
#Set Title
$ChoiceField.Title="Department Selection"
#Default choice
$ChoiceField.DefaultValue="Sales"
#Allow fill in choices
$ChoiceField.FillInChoice=$true
#Apply changes to the column
$ChoiceField.Update()
Write-host "Site column $($SiteColumn) has been Added Successfully!"
$RootWeb.Dispose()
SharePoint 2016, Alan Şeması XML Yöntemini kullanarak PowerShell'i kullanarak site sütunu oluşturur
AddFieldAsXml yöntemi bize daha fazla esneklik sağlar:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
#Define the Site column XML
$FieldXML = '<Field Type="Number" DisplayName="Bpt-InvoiceNo" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Min="1" Max="10000" Group="Bpt Columns" ID="{b81c7da6-1317-46fa-a32b-9f446c30b6e9}" StaticName="Bpt-InvoiceNo" Name="Bpt-InvoiceNo"></Field>'
#Create site columns with powershell from XML
$RootWeb.Fields.AddFieldAsXml($FieldXML)
Write-host "Site column has been Added Successfully!"
Salt Okunur, Grup vb. gibi alan seçeneklerini belirtmek için Alan Öğesi XML'sine bakın.
https://msdn.microsoft.com/en-us/library/office/ms437580.aspx
SharePoint, CSV Dosyasından PowerShell ile site sütunu ekleme
Bir CSV dosyasından site sütunlarını toplu olarak eklemeye ne dersiniz?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
$CSVPath = "D:\SiteColumns.csv"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
#Import from CSV File
$SiteColumnsCSV = Import-csv -Path $CSVPath #CSV has "Title" column Header
foreach($Column in $SiteColumnsCSV)
{
#Get the field title from CSV
$FieldTitle = $Column.Title
#Check if a site column exists already
if (!$RootWeb.Fields.ContainsField($FieldTitle))
{
#Frame FieldXMLString from the column Title
$FieldXMLString = [String]::Format('<Field Type="Text" Name="{0}" DisplayName="{1}" StaticName="{0}" Required="FALSE"></Field>',$fieldTitle.Replace(" ",""),$FieldTitle)
#create site column sharepoint 2010 powershell from XML string
$RootWeb.Fields.AddFieldAsXml($FieldXMLString)
Write-host "Site column '$($FieldTitle)' has been Added Successfully!"
}
}
$RootWeb.Dispose()
İlgili Gönderiler:
- PowerShell kullanarak site sütunlarını kopyalayıp yapıştırın: PowerShell ile İçe Aktarma Sitesi Sütunlarını Dışa Aktarın
- SharePoint'te site sütunları eklemek için özellik tabanlı bir çözüm oluşturun: SharePoint Sitesi sütunlarını Dağıtmak için Özellik Tabanlı Çözüm Oluşturun
