Steps To Create Content Type Using Visual Studio And Powershell Programmatically

In this article, you will learn about the steps to create Content Type using Visual Studio and PowerShell programmatically for Online and On-Prem 2019.

Introduction

 
Content Types are set of columns grouped together to serve a specific purpose.
 
A content type is a group of reusable settings of shared attributes and behaviours for a specific kind of content. It mainly used for defining item type, documents, list items, media files & folders.
 
While creating a custom content type child content type must be derived from the one of existing parent content type such as Image, Task, Document or Item. Once it is created in a site, a custom content type is available in that site and in all sites nested under that site.
 
To make a content type most widely available throughout a site collection, define it in the content type gallery of the top-level site. In the On-Premise environment of Share Point a custom content type can be also created inside a content type hub that is defined in a managed metadata service instance.
 
In this article, you will learn how we can create content type using Visual Studio and PowerShell for On-line and On-Premise environment.
 
Prerequisites
  • Share-Point 2019 On-Premise Environment
  • Share-Point Office 365 Tenant
  • Visual Studio
  • Share-Point Management PowerShell

Content Type Creation for On-Prem Environment Using Visual Studio

 
Open Visual Studio and create a new project.
 
Steps To Create Content Type Using Visual Studio 
 
Select SharePoint 2019 Empty Project - C# (Language).
 
Steps To Create Content Type Using Visual Studio 
 
Enter the Project Name. In our case, I created the Project Name as CustomContentType.
 
Steps To Create Content Type Using Visual Studio 
 
In the Next Screen, we need to establish the connection between CustomContentType project with our SharePoint On-Premise environment.
 
Steps To Create Content Type Using Visual Studio 
 
Note
SharePoint 2019 environment doesn’t support the sandboxed solution, we need to deploy our application at the Farm Level as a Farm Solution.
 
Once the project is created, we will have the project solution ready. During this creation, our SharePoint Application comprises of 2 key things or also known as special node.
  • Feature – It’s a container of extension of one or more SharePoint features.
  • Package – Single File which can be deployed and reusable.
Steps To Create Content Type Using Visual Studio 
 
Add New Item to the SharePoint Project (Ctrl+Shift+A) Select Content Type.
 
Steps To Create Content Type Using Visual Studio 
 

Parent Inheritance of Content Type

 
Even with the custom content type creation, it needs to be inherit from one of the available existing content types that is available in our site collection.
 
Steps To Create Content Type Using Visual Studio 
 
Note
In our application we have selected the Document as Parent Content Type and finish.
 
Steps To Create Content Type Using Visual Studio 
 
Once the Item is created it will create a Feature in the Feature Folder explicitly by default and our created content type will be placed in the Package Folder.
 
Steps To Create Content Type Using Visual Studio 
 
Content Type Name
It displays the Name of our Content type.
Parent Content Type
It displays which parent content type we inherited to derive this new custom content type.
Group Name
To which parent group this content type needs to be added there are many group names available such as Document Content Types, List Content Types, Folder Content Types, Custom Content Types etc.
Configuration Settings
Inherit the columns from Parent Content Type
If you want to inherit the same column from the content type, we need to select this option.
Set to read-only
If the content type needs to be read-only this option needs to be enabled. If this enabled, we cannot able to modify the columns that available with this content type.
Hide from the new buttons in list views
 

Configuring Columns

  • Display Name:  Which is nothing but the Display Name of the Column which we going to Create.
  • Type: Column Type information whether it is Single Line of Text, Number, Lookup or etc.,
  • Required: Whether the column is required or not while use submitting a record using this custom content type.
Steps To Create Content Type Using Visual Studio 
 
Build and deploy the SharePoint solution.
 
Open our SharePoint site. Go to Site Settings à Site Content Types under Web Designer Galleries. Our Custom Created Content Type will be visible.
 
Steps To Create Content Type Using Visual Studio 
 

Content Type Creation for Online Environment Using Powershell

 
While executing the Script if we ended up with the below exception it is stating that the PnP Powershell module is not available in our system hence it couldn’t able to recognize the Powershell commands.
 
Download Link
 
 
Steps To Create Content Type Using Visual Studio 
  1. $SiteCollectionUrl = https://bigdata15.sharepoint.com #Url of the Site Collection  
  2. $CustomContentType ="Sai" #Name of our Content Type  
  3. #Description to our Content Type  
  4. $CustomContentTypeDescription ="Custom Content Type Demo"  
  5. #Group Name of the Content Type  
  6. $CustomContentTypeGroupName = "Custom Content Type"  
  7. #Connecting to the SharePoint Site & Adding the Content Type.  
  8. Connect-PnPOnline -Url $SiteCollectionUrl -Credentials (Get-Credential)  
  9. Add-PnPContentType -Name $CustomContentType -Description $CustomContentTypeDescription -Group $CustomContentTypeGroupName  

Code Snippet Execution

 
Steps To Create Content Type Using Visual Studio
 
Steps To Create Content Type Using Visual Studio