kwizcom banner advertisement

How to Create a Content Type in SharePoint using PowerShell?

A Content Type in SharePoint is a fundamental framework for any item, such as Task, Document, Event, etc. Every content type has its own set of metadata columns and templates, information management policies, workflows. In SharePoint, we can create our own custom content types from existing content types, such as "Invoice" content type based on a document content type and add custom columns to it say "Invoice Date".

How to Create a Content Type in SharePoint?
To add a content type in SharePoint, 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.
    create content type in sharepoint using powershell
  • Provide a name to your custom content type. Optionally you can enter the description for your new content type to make it purpose clear.
  • Select the parent content type such as "Item" from which your content type is based on. You can either create a new content type group or select any existing.
    sharepoint powershell create new content type
  • Click OK to complete creating the 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.

Content types are hierarchical - So, when you create a content type at root site level, all sub-sites underneath it inherits site columns and content types from the parent site!

Create Content Type in SharePoint using PowerShell
Lets create a new content type in SharePoint 2013 using PowerShell
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"

#Get the Root web
$web = Get-SPWeb $SiteURL

#Get parent content type - "Item"
$ParentContentType = $web.AvailableContentTypes["Item"]

#Check if the content type already exists
$ContentType = $web.ContentTypes[$ContentTypeName]
If($ContentType -eq $NULL)
{
    #Create new content type from  "item" Content Type
    $ContentType = New-Object Microsoft.SharePoint.SPContentType($ParentContentType, $web.ContentTypes, $ContentTypeName)
    $ContentType.Description ="Crescent Projects Content Type"
    $ContentType.Group = "Crescent Content Types"
    $web.ContentTypes.Add($ContentType)
    $web.Update()
    Write-Host "Content Type '$ContentTypeName' Created Successfully!" -ForegroundColor Green
}
else
{
    Write-Host "Content Type '$ContentTypeName' Already Exists!" -ForegroundColor Red
}

This PowerShell creates new content type in SharePoint. Now the next step is to add some columns to it: Lets add a "Project Name" column to content type.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"

#Get the Root web
$web = Get-SPWeb $SiteURL

#Get the Content Type
$ContentType = $web.ContentTypes[$ContentTypeName]

#Check if the content type exists
If($ContentType -ne $NULL)
{
    #Create new Site column
    $FieldName="ProjectName"

    #Check if field exists already
    $Field = $web.Fields | where {$_.InternalName -eq $FieldName}
    If($Field -eq $null)
    {
        #Add a Site Column
        $Field = $Web.Fields.Add($FieldName, [Microsoft.SharePoint.SPFieldType]::Text, $false)
    }
    
    #Get a Field Link to Add the site column to content type
    $Field=$web.Fields[$FieldName]
    $FieldLink = new-object Microsoft.SharePoint.SPFieldLink($Field)
    #Add Site column to content type
    $contentType.Fieldlinks.add($FieldLink)
    $contentType.Update()

    Write-Host "Fied Added to Content Type '$ContentTypeName' Successfully!" -ForegroundColor Green
}
else
{
    Write-Host "Content Type '$ContentTypeName' Doesn't Exist!" -ForegroundColor Red
}

To create a content type in SharePoint Online using PowerShell, refer: How to Create a Content Type in SharePoint Online using PowerShell
How to Create a Content Type in SharePoint using PowerShell? How to Create a Content Type in SharePoint using PowerShell? Reviewed by Salaudeen Rajack on January 24, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.