SharePoint Content Types are essential for managing structured content across sites. They define metadata, workflows, and templates for items such as documents, list items, and pages.
With PnP PowerShell, we can automate the creation, modification, and management of Content Types, ensuring consistency and efficiency in SharePoint Online.
This guide provides a step-by-step approach to managing SharePoint Content Types using PnP PowerShell.
Prerequisites
PnP PowerShell Installed
Install it using:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
Administrator Permissions
- You must be a SharePoint Admin or Site Collection Admin.
Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive
Step 1: Retrieve Existing Content Types
Before creating a new Content Type, check the existing ones:
# Get all Content Types in the Site
Get-PnPContentType
To check a specific Content Type:
powershellCopyEditGet-PnPContentType -Identity "0x010100D5F4A3B2483A4B8E947FBEA3E4B4A609"
🔹 The ID helps ensure uniqueness when creating new Content Types.
Step 2: Create a New Content Type
A Content Type requires a Name, ID, and Parent Content Type.
New-PnPContentType -Name "Project Document" -Group "Custom Content Types" -Description "Project-related documents" -ParentContentType "0x0101"
Creates a Project Document Content Type under Custom Content Types.
0x0101 is the ID for the Document Content Type.
Step 3: Add Site Columns to the Content Type
To store metadata, add columns to the Content Type:
# Create a new column
Add-PnPField -DisplayName "Project Name" -InternalName "ProjectName" -Type Text -Group "Custom Columns"
# Add the column to the Content Type
Add-PnPFieldToContentType -Field "ProjectName" -ContentType "Project Document"
Adds a “Project Name” column to “Project Document” Content Type.
Step 4: Assign Content Type to a Library or List
Now, apply the Content Type to a Document Library:
# Enable content types in the library
Set-PnPList -Identity "Project Documents" -ContentTypesEnabled $true
# Add the content type to the library
Add-PnPContentTypeToList -List "Project Documents" -ContentType "Project Document"
Enables Content Types in the Project Documents library.
Assigns the Project Document Content Type to the library.
Step 5: Update an Existing Content Type
To rename or update a Content Type:
Set-PnPContentType -Identity "Project Document" -Name "Updated Project Document"
Renames Project Document to Updated Project Document.
Step 6: Remove a Content Type from a Library
To unassign a Content Type from a list:
Remove-PnPContentTypeFromList -List "Project Documents" -ContentType "Updated Project Document" -Confirm:$false
Removes Updated Project Document from Project Documents library.
Step 7: Delete a Content Type
To delete an unused Content Type:
Remove-PnPContentType -Identity "Updated Project Document" -Confirm:$false
Warning: This action cannot be undone. Ensure no dependencies exist before deleting.
Step 8: Export Content Types to CSV
To back up all Content Types and their columns:
$contentTypes = Get-PnPContentType
$contentTypes | Select Name, Id, Group, Description | Export-Csv -Path "C:\SharePoint_ContentTypes.csv" -NoTypeInformation
Saves all Content Types and metadata to a CSV file.
Step 9: Import Content Types from CSV (Bulk Creation)
Prepare a CSV file with the format:
Name | Group | ParentContentType | Description |
---|---|---|---|
Invoice Document | Custom Content Types | 0x0101 | Stores invoice documents |
HR Policies | Custom Content Types | 0x0101 | Stores HR policy documents |
Run the script to create them in bulk:
$csvData = Import-Csv -Path "C:\SharePoint_ContentTypes.csv"
foreach ($row in $csvData) {
New-PnPContentType -Name $row.Name -Group $row.Group -ParentContentType $row.ParentContentType -Description $row.Description
}
Creates multiple Content Types in SharePoint from the CSV file.
Step 10: Schedule the Script for Automation
1️⃣ Open Task Scheduler
2️⃣ Click Create Basic Task
3️⃣ Set Trigger: Daily/Weekly
4️⃣ Set Action: Start a Program
5️⃣ In Program/script, enter:
-ExecutionPolicy Bypass -File "C:\YourScriptPath\ManageContentTypes.ps1"
6️⃣ Click Finish
🔹 The script now runs automatically based on schedule.
Troubleshooting Common Issues
Issue 1: PnP PowerShell Not Recognized
Run:
Update-Module -Name PnP.PowerShell
Issue 2: Connection Errors
Ensure you are connected as Admin:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive
Issue 3: Content Type Not Appearing in Library
- Ensure Content Types are enabled in the Library.
- Check if the Content Type was correctly assigned.