Create Site Columns, Content Type and Add to Library using PowerShell

A good script to have for testing.

Add-PSSnapIn "Microsoft.SharePoint.Powershell"
#Get the site collection and web object
$siteColl = Get-SPSite -Identity "https://portal.contoso.com"
function Create-SiteColumnText ($siteColl, $CustomColumn)
{
    $rootWeb = $siteColl.RootWeb
    #Assign fieldXMLString variable with field XML for site column
    $fieldXMLString = "<Field Type='Text' Name='$CustomColumn'
    Description='Indicate date of last and next meeting and names of current members.'
    DisplayName='$CustomColumn'
    StaticName='$CustomColumn'
    Group='Custom Columns'
    Hidden='FALSE'
    Required='FALSE'
    Sealed='FALSE'
    ShowInDisplayForm='TRUE'
    ShowInEditForm='TRUE'
    ShowInListSettings='TRUE'
    ShowInNewForm='TRUE'></Field>"
    #See field XML on console
    #write-host $fieldXMLString
    #Create site column from XML string
    $rootWeb.Fields.AddFieldAsXml($fieldXMLString) 
    Write-Host "Column Added"
}

function Create-SiteColumnChoice ($siteColl, $CustomColumn)
{
    $rootWeb = $siteColl.RootWeb
    #Assign fieldXMLString variable with field XML for site column
    $fieldXMLString = "<Field Type='Choice' 
    Name='$CustomColumn'
    Description='Choice.'
    Format='Dropdown'
    DisplayName='$CustomColumn'
    StaticName='$CustomColumn'
    Group='Custom Columns'
    Hidden='FALSE'
    Required='FALSE'
    Sealed='FALSE'
    ShowInDisplayForm='TRUE'
    ShowInEditForm='TRUE'
    ShowInListSettings='TRUE'
    ShowInNewForm='TRUE'>
    <Default>Specification</Default>
                <CHOICES>
                  <CHOICE>Specification</CHOICE>
                  <CHOICE>Development</CHOICE>
                  <CHOICE>Test</CHOICE>
                  <CHOICE>Documentation</CHOICE>
                </CHOICES>
    </Field>"
    #See field XML on console
    #write-host $fieldXMLString
    #Create site column from XML string
    $rootWeb.Fields.AddFieldAsXml($fieldXMLString) 
    Write-Host "Column Added"
}


Create-SiteColumnText -siteColl $siteColl -CustomColumn "MyCustomColumn1"
Create-SiteColumnText -siteColl $siteColl -CustomColumn "MyCustomColumn2"
Create-SiteColumnText -siteColl $siteColl -CustomColumn "MyCustomColumn3"
Create-SiteColumnChoice -siteColl $siteColl -CustomColumn "MyCustomColumn4"

$web = $siteColl.RootWeb
$ctypeName = "MyCustomContentType"
$ctypeParent = $web.availablecontenttypes["Document"]
$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)
$web.contenttypes.add($ctype)
$field = $web.fields.getfield("MyCustomColumn1")
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)

$field = $web.fields.getfield("MyCustomColumn2")
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)

$field = $web.fields.getfield("MyCustomColumn3")
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)

$field = $web.fields.getfield("MyCustomColumn4")
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.Update()

$Library =$Web.Lists["Documents"]
$Library.ContentTypesEnabled = $true

$Library.ContentTypes.Add($ctype);
$Library.Update()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s