SharePoint: How to create a Choice Site Column with PowerShell!

This time I am sharing a small script that allows to create a choice site column through PowerShell. As you can see, the way to create this type of column is the same as if you use the SharePoint server API in a Console Application program in Visual Studio. You can download the script from the following link: How to create a Choice Site Column in a SharePoint Site!

############################################################################################################################################

# This script allows to easily create a new Site Column in a SharePoint Site

# Required parameters:

#   ->$sSiteCollectionUrl: Site Collection Url.

#   ->$sFieldDisplayName: Display name for the New Column to be added.

#   ->$sFieldInternalName: Internal name for the New Column to be added.

#   ->$sFieldType: Field type (Text, Choice, ...)

############################################################################################################################################

 

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 

{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }

 

$host.Runspace.ThreadOptions = "ReuseThread"

 

 

#Definition of the function that creates a New Site Column in a SharePoint Site.

function Create-SiteColumn

{

    param ($sSiteCollectionUrl,$sFieldDisplayName, $sFieldInternalName, $sFieldType)   

    try

    {

        $spSite = Get-SPSite -Identity $sSiteCollectionUrl

         $spWeb = $spSite.OpenWeb()    

 

    

        #We check the field type is not null

        if($sFieldType -ne '')

        {

            write-Host "Adding the field $sFieldDisplayName to the Site" -foregroundcolor blue            

            $spWeb.Fields.Add($sFieldInternalName,$sFieldType,$false)

            $spChoiceField=$spWeb.Fields.GetField($sFieldInternalName)

            $spChoiceField.Group="MVP Columns"

            $spChoiceField.Choices.Add("Developer")

            $spChoiceField.Choices.Add("IT Pro")

            $spChoiceField.Choices.Add("Architect")

            $spChoiceField.Title=$sFieldDisplayName

            $spChoiceField.DefaultValue="Developer"

            $spChoiceField.FillInChoice=$true

            $spChoiceField.Update()

        } 

        

        #Disposing SPSite and SPWeb objects

        $spWeb.Dispose()   

        $spSite.Dispose()   

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()

    }

} 

 

#Calling the function

Start-SPAssignment –Global

$sSiteCollectionUrl = "http://winsrv2012:80/sitios/compartimoss"

$sFieldDisplayName="MVP Type"

$sFieldInternalName="MVPType"

$sFieldType="Choice"

Create-SiteColumn -sSiteCollectionUrl $sSiteCollectionUrl -sFieldDisplayName $sFieldDisplayName -sFieldInternalName $sFieldInternalName -sFieldType $sFieldType

Stop-SPAssignment –Global

 

Remove-PsSnapin Microsoft.SharePoint.PowerShell

And bellow you can find the result you get when executing the Script:

image

Un pensamiento en “SharePoint: How to create a Choice Site Column with PowerShell!

  1. Pingback: SharePoint 2013: Resumen de posts (LXII)! | Pasión por la tecnología...

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s