Create Content Type and respective fields Office 365 PowerShell script SharePoint 2013 Online

InfoPath alternatives for form designing SharePoint

Office 365 scripting is really interesting. Today I will be sharing code to create Content type and respective site columns and also add content pages library.

You may also like to see:
– Enable Site and Web Features CSOM Powershell Script Office 365 Sharepoint 2013

– Show SharePoint Survey Friendly message using jQuery

– Hover panel in SharePoint 2013 search results

Below code for PS script and XML inputs respectively.
XML Inputs –
<ContentTypes>

<ContentType ParentId=”0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39″
Name=”Contenttype1″ Group=”Custom Content Types” List=”Pages”>

<Field
RichText=”FALSE” NumLines=”50″
UnlimitedLengthInDocumentLibrary=”TRUE”
Type=”Note” DisplayName=”Test Description” Required=”False”
StaticName=”TestDescription” Name=”TestDescription” Group=”Custom Columns” />
<Field
StaticName=”TestImage”
Name=”TestImage” DisplayName=”test Image” Type=”Image” Required=”False” RichText=”TRUE” RichTextMode=”ThemeHtml” Group=”Custom Columns”/>
<Field
Name=”Choiceclm”
DisplayName=”Test Choice column”
Type=”Choice”
Required=”False” StaticName=”Choiceclm” Group=”Custom Columns”>
<CHOICES>
<CHOICE>Internal</CHOICE>
<CHOICE>External</CHOICE>
</CHOICES>
</Field>

<Field
Type=”Text” DisplayName=”Test Title”
Required=”False” MaxLength=”255″
StaticName=”TestTitle”
Name=”TestTitle” Group=”Custom Columns” />
<Field
StaticName=”TestURL” Name=”TestURL”
DisplayName=”Test URL” Type=”URL” Format=”Hyperlink” Required=”False” Group=”Custom Columns”/>

</ContentType>

</ContentTypes>

PS Script-
cls

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “samtest@microsoft.com”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}

function createContentTypes([Microsoft.SharePoint.Client.Web] $currentWeb)
{
$web = $clientContext.Site.RootWeb
$clientContext.Load($web)
$clientContext.ExecuteQuery()

$contentTypes = $web.ContentTypes;
$clientContext.Load($contentTypes);
$clientContext.ExecuteQuery();

foreach($content in $xmldata.WebSite.ContentTypes.ContentType)
{

$docCT = $contentTypes.GetById($content.ParentId)
$clientContext.Load($docCT);
$clientContext.ExecuteQuery();

#Create a Content Type Information object
$cType = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation

#Set the name for the content type
$cType.Name =$content.Name

#Inherit from oob document – 0x0101 and assign
$cType.ParentContentType = $docCT ;
$cType.Group = $content.Group

##Create the content type
$spCT = $contentTypes.Add($cType)
$clientContext.Load($spCT);
$clientContext.ExecuteQuery()

foreach ($node in $content.Field) {

$field=$web.Fields.AddFieldAsXml($node.OuterXml,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint);
# Finally, add the custom site column to our custom content typ$fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation

$fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$fieldReferenceLink.Field = $field;
$spCT.FieldLinks.Add($fieldReferenceLink)
}

# Commit changes to SharePoint
$spCT.Update($true)
$clientContext.ExecuteQuery()

$list = $currentWeb.Lists.GetByTitle($content.List)
$cts = $list.ContentTypes
$clientContext.Load($cts)

$ctReturn = $cts.AddExistingContentType($spCT)
$clientContext.Load($ctReturn)
$clientContext.ExecuteQuery()

}
}

createContentTypes $web

Thanks,
Sambita

Similar SharePoint 2013 Tutorials


About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →

Leave a Reply