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

SharePoint: Como crear una Columna de Sitio de tipo Choice con PowerShell!

En esta ocasión os dejo un pequeño script para crear una Columna de Sitio de tipo elección por medio de PowerShell. Como podéis ver, la forma de crear este tipo de Columna es la misma que si hacemos uso de la API en un programa de Aplicación de Consola de Visual Studio. Podéis descargaros el script desde el siguiente enlace: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

Y el resultado que se obtiene tras ejecutar el Script es el siguiente:

image