Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

SharePoint Online: Create Content Type using PowerShell

$
0
0
What are content types in SharePoint Online?
Content types are set of columns grouped together to serve a specific purpose. Think about a Invoice template! Instead of you and your users end up creating different templates each time from the scratch, you can define an Invoice template once and your users can start consuming it at any number of lists or document libraries! By this way, you force standardization in your collaboration environment. Or think of "Project Tracking" list with columns required to track projects for your organization. Instead creating and adding each column for Project tracking list every time, you'll be able to simply utilize project tracking content type template in any number of lists in few clicks.

SharePoint Online lists or document libraries can contain multiple content types. E.g. You can add multiple content types such as "Invoice", "Proposal", "Purchase Order", etc to a library to organize everything related to a project as a single entity. Once you associate a content type with SharePoint online list or library, SharePoint lets you to create new items of that content type from New Item command in that list or library. In the case of a document content type, you can also define a document template that will be the base for all the documents created from this particular content type.

As a best practice, content types should be ideally defined at the top site, independent of any list or library, So that it can be reused at any SharePoint site underneath or even across SharePoint site collections with content type hub.

How to Create Content Type in SharePoint Online?
To add a content typein SharePoint online, follow these steps:
  • Go to Site Settings >> Click on "Site Content Types" under "Web Designer Galleries" group.
  • In Site content types page, You''ll see the list of default content types like Item, Tasks, Document, etc grouped by sections. Click on "Create" link at the top.
  • Provide a name to your custom content type. Optionally you can enter the description for your new content type to make it clear.
  • Select the parent content type such as "Item" to which your content type is based on. You can either create a new content type group or select any existing.
    sharepoint online powershell create content type
  • Click OK to complete adding content type. Once the content type is created, The next step is to add required Site columns to the content type to make metadata available from the content type.

SharePoint online Create Content Type using PowerShell
Here is the PowerShell script to create content type programmatically in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#function to turn ON Content Type in SharePoint Online list or library
Function Create-SPOContentType()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $CTypeName,
[Parameter(Mandatory=$true)] [string] $CTypeDesc,
[Parameter(Mandatory=$true)] [string] $ParentCTypeName,
[Parameter(Mandatory=$true)] [string] $CTypeGroup
)

Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get all content types from the site
$ContentTypeColl = $Ctx.web.ContentTypes
$Ctx.Load($ContentTypeColl)
$Ctx.ExecuteQuery()

#Get the parent content type
$ParentCType = $ContentTypeColl| Where {$_.Name -eq $ParentCTypeName}

#Check if content type exists already
$ContentType = $ContentTypeColl| Where {$_.Name -eq $CTypeName}


If($ContentType -ne $Null)
{
Write-host "Content type '$CTypeName' already exists!" -ForegroundColor Yellow
}
else
{
#Specify properties for the new content type
$CTypeCreationInfo=New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
$CTypeCreationInfo.Name=$CTypeName
$CTypeCreationInfo.Description=$CTypeDesc
$CTypeCreationInfo.Group=$CTypeGroup
$CTypeCreationInfo.ParentContentType=$ParentCType

# Add the new content type to the collection
$ContentType=$ContentTypeColl.Add($CTypeCreationInfo)
$Ctx.ExecuteQuery()

Write-host "Content Type '$CTypeName' Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Content Type!" $_.Exception.Message
}
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$CTypeName="Projects"
$CTypeDesc="Contnet type for Project template"
$ParentCTypeName="Item"
$CTypeGroup="Crescent Projects"

#Call the function
Create-SPOContentType -SiteURL $SiteURL -CTypeName $CTypeName -CTypeDesc $CTypeDesc -ParentCTypeName $ParentCTypeName -CTypeGroup $CTypeGroup
This script creates content type in sharepoint online from the specified parent content type. Here in this example, I've created "Project" content type from "Item" content type which has "Title" as its default field. You may want to add additional fields tot the content type and you can add columns to content type in SharePoint online with PowerShell.

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>