Friday, August 16, 2013

Create SharePoint Choice Field (Site Column) using PowerShell

Start-SPAssignment -Global
$url = "siteurl"
$web = get-spweb $url

$fieldName = "Surya"
$field = $web.Fields[$fieldName]

 if($field -ne $nul)
 {
      Write-Host $field.Title "Field already avilable in the site"
 }
 else
 {

#There are 2 ways to create a field.
#First Apporach
 <#
      Write-Host "no fields exist with this name. creating..."
      $web.fields.add($fieldName, "Choice", $true)
      $field = $web.fields.getfield($fieldName)
      $field.Group = "Custom Columns"
      $field.Choices.Add("Yes")
      $field.Choices.Add("No")
      $field.Update()
     Write-Host "Field Created"
#>

#Second Apporach

      $suryafield = "<Field Type='Choice' DisplayName='Surya' Required='TRUE' Format='Dropdown' Group='Custom Columns'
      FillInChoice='FALSE' StaticName='Surya' Name='Surya'>
            <CHOICES>
                <CHOICE>YES</CHOICE>
                <CHOICE>NO</CHOICE>
            </CHOICES>
           </Field>"
      $web.Fields.AddFieldAsXml($suryafield)
  Write-Host "Field Created"
 }

 Stop-SPAssignment -Global


No comments:

Post a Comment

Followers