In SharePoint, we have lists and libraries for collaboration purposes. The lists and libraries contain a huge number of columns.
For better understanding and reusability, these are grouped in content types.
Content types are, hence, reusable collections of metadata which can be attached to any list or library. They help us to organize and handle the data in a better and more efficient way.
There are 2 types of content types.
- Site Content types
This is present in the root level of the site which can be used by subsequent subsites and lists/ libraries.
- List Content types
These are associated with lists and libraries and are a subset of site content types.
Let's take an example where content types are required.
Suppose, we have an Employee site which contains list like EmployeeData, HREmployeeData, FinanceEmployeeData, EmployeeDocuments, HRDocuments.
In this case, as we can see for EmployeeData and HR EmployeeData, we can have similar columns related to employees (For example, Firstname, Lastname, Ph Number, Address). So, here there are 2 possibilities.
- We can either create a parent content type as Employee(which contains basic Employee details). Then, create HREMPContenttype and EMPContentype which will inherit from Employee.
- Or, we can create a content type Employee and add that in the 2 lists. Then, add additional columns for the respective list.
Same goes for documents as well.
Now comes a situation where we need the same content types or a few of them in a separate site.
In on-premise, we have a content type hub for publishing the content type to be used across multiple locations. But, in SP Online we do not have the feature.
There is an option to export and import content type.
In the below PowerShell CSOM code, we can export the content type in XML file and use in the respective sites as required.
-
- 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"
- $URL = Read - Host - Prompt "Enter URL"– - URL of your online site.
- $User = Read - Host - Prompt "Enter User"– - YOUR inline username
- $siteURL = $URL
- $userId = $User
- $pwd = Read - Host - Prompt "Enter password" - AsSecureString--password
- $creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
-
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- try {
- $OutPutLocation = "C:\Powershell"–
- Give your respective file location to store xml files
- $web = $ctx.Web
- $contenttypes = $web.ContentTypes;
- --This line gives the content types presemt in the site.
- $fileName = [System.IO.Path]::Combine($OutPutLocation, "content-types.xml")
- Write - Host "Starting content type export to $fileName" - ForegroundColor Green
- $ctx.load($contenttypes)
- $ctx.executeQuery()
- $GroupName = "Discussion"
-
- $contenttypes | ForEach - Object {
- #if ($_.Group - eq $GroupName) {
- Write - Host - ForegroundColor Blue $_.Name
- Add - Content $fileName $_.SchemaXml
- #
- }
- }
- Add - Content $fileName "</ContentTypes>"
- } catch {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
Please store the DLLs in respective ISAPI folder before proceeding with the code.
Similarly, we can import the respective content types to a destination site collection using the below script.
- 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"
- $xmlFilePath = "C:\powershell\content-types.xml"
- $URL = Read - Host - Prompt "Enter URL"
- $User = Read - Host - Prompt "Enter User"
- $siteURL = $URL
- $userId = $User
- $pwd = Read - Host - Prompt "Enter password" - AsSecureString
- $creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- $destWeb = $ctx.Web
- $ctx.executeQuery()
- $ctsXML = [xml](Get - Content($xmlFilePath))
- # get content types from xml path
- $ctsXML.ContentTypes.ContentType | ForEach - Object {
- $SPContentType = New - Object Microsoft.SharePoint.SPContentType($_.ID, $destWeb.ContentTypes, $_.Name)
- $SPContentType.Group = $_.Group
- # loop through each content type and add them.
- $_.Fields.Field | ForEach - Object {
- if (!$spContentType.FieldLinks[$_.DisplayName]) {
- #Create a field link
- for the Content Type by getting an existing column
- $spFieldLink = New - Object Microsoft.SharePoint.SPFieldLink($destWeb.Fields[$_.DisplayName])
- # Check to see
- if column should be Optional, Required or Hidden
- if ($_.Required - eq "TRUE") {
- $spFieldLink.Required = $true
- }
- if ($_.Hidden - eq "TRUE") {
- $spFieldLink.Hidden = $true
- }
- #Add column to Content Type
- $spContentType.FieldLinks.Add($spFieldLink)
- }
- }
- #Create Content Type on the site and update Content Type object
- $ct = $destWeb.ContentTypes.Add($spContentType)
- $spContentType.Update()
- write - host "Content type"
- $ct.Name "has been created"
- }
- $destWeb.Dispose()