Requirement: Export the content type of a web in SharePoint into an XML file so that it can be imported in other environments such as Test, Stage, and Production
Solution: Automate the export process of content type from SharePoint web using PowerShell script.
Script:
clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell
####################### ENVIRONMENT VARIABLES #######################
$contentTypeSourceWeb = "<<weburl>>" #Ex: http://webapp/sites/myproject
# This is the place where exported content type is stored the XML file
$xmlFilePathForContentType = "C:\Script-SiteContentTypes.xml"
#####################################################################
#Content Type group you are looking to export
$contentTypeGroupName = "Custom Content Types"
$sourceWeb = Get-SPWeb $contentTypeSourceWeb
#Create Export File
New-Item $xmlFilePathForContentType -type file -force
#Export Content Types to XML file
Add-Content $xmlFilePathForContentType "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $xmlFilePathForContentType "`n<ContentTypes>"
$sourceWeb.ContentTypes | ForEach-Object {
if ($_.Group -eq $contentTypeGroupName) {
write-host "Adding content type to file."
Add-Content $xmlFilePathForContentType $_.SchemaXml
}
}
Add-Content $xmlFilePathForContentType "</ContentTypes>"
$sourceWeb.Dispose()
write-host "Completed"
No comments:
Post a Comment