Creating Fields and Content Types cleanly in SharePoint Creating Fields and Content Types cleanly in SharePoint
Joel Plaut

Joel Plaut

September 30, 2014

All Post
Creating-Fields-and
Share:

when creating Content Types in SharePoint, it’s nice to script the process, for planning and repeatability. Here’s a nifty routine that allows a single line creation of Site Columns and Content Types.

First, here’s the routine to create Site Columns. The here-string XML is replaced with placeholders filled by parameters. Note the convenient parameters:

function tryAdd-TextField ([Microsoft.SharePoint.SPWeb] $web, [string] $DisplayName, [string] $InternalName, [string] $Group, [Boolean] $Hidden, 
[Boolean] $Required, [Boolean] $ShowInDisplayForm, [Boolean] $ShowInEditForm, [Boolean] $ShowInNewForm )
{
$fields = $web.fields;
try
{
    $q=$fields.getFieldByInternalName($InternalName); 
} catch
{
    $q=$null;
}
 
if ($q -ne $null)
{
Write-Host "$($InternalName) already exists!, no action taken to create this site column"
}
else
{
$fieldXMLString = '
 '
 
 $FieldXMLString = $FieldXMLString.Replace("@InternalName",$InternalName.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@DisplayName",$DisplayName.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@Group",$Group.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@Hidden",$Hidden.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@Required",$Required.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@ShowInDisplayForm",$ShowInDisplayForm.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@ShowInEditForm",$ShowInEditForm.tostring())
 $FieldXMLString = $FieldXMLString.Replace("@ShowInNewForm",$ShowInNewForm.tostring())
 
 $web.Fields.AddFieldAsXml($fieldXMLString)
 }
 }
function tryAdd-CT ([Microsoft.SharePoint.SPWeb] $web, [string] $Field, [string] $Group, [string] $Parent, [string] $Name, [string] $Description )
{
    try
    {
        $ctypeParent = $web.availablecontenttypes[$Parent]
 
    }
    catch
    {
        $ctypeParent = $null;
    }
 
    if ($ctypeParent -eq $null)
    {
        write-host "Content Type $($Name) not created because there was a problem finding the Parent Content Type $($Parent)"
    }
    else
    {
    $ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $Name)
    $ctype.Description = $Description
    $ctype.group = $Group
 
    if (![string]::IsNullOrEmpty($field))
    {
    foreach ($fld in $field.split("|"))
     {
        $f=$web.fields.getFieldByInternalName($fld)  
        $link = new-object Microsoft.SharePoint.SPFieldLink $f
        $ctype.FieldLinks.Add($link)
     }
    }
 
    try
    {
        $ctype = $web.contenttypes.add($ctype)
    }
    catch
    {
        write-host "Content Type $($Name) already exists"
    }
    }
}

Let’s now create some site columns:

tryAdd-TextField -web $Web -DisplayName "Year"    -InternalName "YearACT"    -Group "Actuarial" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False
tryAdd-TextField -web $Web -DisplayName "Quarter" -InternalName "QuarterACT" -Group "Actuarial" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False
tryAdd-TextField -web $Web -DisplayName "Month"   -InternalName "MonthACT"   -Group "Actuarial" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False
tryAdd-TextField -web $Web -DisplayName "LOB"     -InternalName "LOBACT"     -Group "Actuarial" -Hidden $False -Required $False -ShowInDisplayForm $True -ShowInEditForm $False -ShowInNewForm $False

Here’s how to create a Content Type with one field:

tryAdd-CT -web $Web -Field $null -Group "Actuarial"  -Parent (select-parent)  -Name "My Core ACT" -Description "Core Actuarial Document"

Note in the comment below, the fields can be passed in pipe-delimited

tryAdd-CT -web $Web -Field "YearACT|QuarterACT|MonthACT|LOBACT" -Group "Actuarial"  -Parent "My Core ACT" -Name "General (ACT)" -Description "General Actuarial Document"

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk?

Drop us a line. We are here to answer your questions 24*7.