PowerShell kullanarak SharePoint Listesine Sütun Nasıl Eklenir?


Gereksinim: PowerShell'i kullanarak SharePoint listesine sütun ekleyin.

SharePoint Listesine Sütun Eklemek için PowerShell Komut Dosyası:

SharePoint listesine sütun eklemeniz gerekiyor ancak bunu manuel olarak yapmak istemiyor musunuz? Bu blog yazısında size PowerShell kullanarak bir listeye sütun ekleme adımlarında yol göstereceğim.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Add-FieldToList($SiteURL,$ListName, $FieldName, $FieldType, $IsRequired)
{
    #Set the Error Action
    $ErrorActionPreference = "Stop"

    Try{
        #Get the List
        $List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
        
        #Check if List with specific name exists
        if($List -ne $null)
        {
            if(!$List.Fields.ContainsField($FieldName))
            {      
                #Add columns to the List 
                $List.Fields.Add($FieldName,$FieldType,$IsRequired)

                #Update the List
                $List.Update()

                #Update the default view to include the new column
                $View = $List.DefaultView # OR $List.Views["All Items"]
                $View.ViewFields.Add($FieldName)
                $View.Update()

                write-host "New Column '$FieldName' Added to the List!" -ForegroundColor Green
            }
            else
            {
                write-host "Field '$FieldName' Already Exists in the List" -ForegroundColor Red
            }
        }
        else
        {
            write-host "List '$ListName' doesn't exists!" -ForegroundColor Red
        }        
    }
     catch {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    finally {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    }
}

#Parameters 
$SiteURL="https://intranet.crescent.com/"
$ListName = "Customer Directory"

#Add 'Name' - Single Line of Text Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Text
$FieldName="Name"
$IsRequired = $False
#Call the function to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

#Add Phone Number - Number Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Number
$FieldName="Phone Number"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

#Date of Joing - Date Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::DateTime
$FieldName="Date of Join"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

Alan şemalarından da SharePoint listesine alanlar ekleyebilirsiniz. İşte nasıl:


#Field Schemas
$NameFldSchema="<Field Type='Text' DisplayName='Name' Required='False' MaxLength='255' StaticName='Name' Name='Name' />"
$PhoneNoFldSchema="<Field Type='Number' DisplayName='Phone Number' Required='False' MaxLength='255' StaticName='PhoneNumber' Name='PhoneNumber' />"
$DOBFldSchema="<Field Type='DateTime' DisplayName='Date of Birth' Required='False' MaxLength='255' StaticName='DateOfBirth' Name='DateOfBirth' /> "
#For Field schemas, Refer: https://msdn.microsoft.com/en-us/library/office/aa979575(v=office.15).aspx
  
#Add Columns to the List
$List.Fields.AddFieldAsXml($NameFldSchema, $True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Yukarıdaki komut dosyası oldukça basit olsa da, seçim alanları, aramalar vb. gibi diğer alan türlerini eklemek için aşağıdaki gönderilere bakın:

  • PowerShell kullanarak SharePoint listesine Seçim Alanı Nasıl Eklenir?
  • PowerShell, SharePoint Listesine Yeni Tek-Çok Satırlı Metin Alanı Oluşturacak
  • PowerShell kullanarak SharePoint Listesine Tarih Saat sütunu ekleme
  • PowerShell ile SharePoint Listesine arama alanı nasıl eklenir?
  • PowerShell kullanarak SharePoint Listesine Yönetilen Meta Veri Sütunu Nasıl Eklenir?

İlgili makaleler