Thursday, February 19, 2009

Copy content types Across Farms (SharePoint)

Content Types in SharePoint can be very useful, and can be created by manually by users or automatically by various products or procedures. In one case, content types may be created automatically during a migration of content from CMS to SharePoint MOSS 2007 publishing server. Regardless of where they came from, they can be copied from one site to another, even moved across farms.

Under most circumstances, Gary Lapointe's stsadm extensions can be used to help automate the process. He has operations for copying content types between sites in the same farm, for propogating content types and for exporting content types from a site. Strangely there is no import content types, which would have enabled cross-farm content type copying.

To copy content types across farms, you'll need to do the gl-exportcontenttypes and then create a SharePoint feature that adds them to your site collection, site, ... whatever. If you don't want to install Gary Lapointe's stsadm extensions, you'll need to use his source code to write a console application that does the same thing. If you have the console application, you can drop it onto your source server (no install needed) and do the export.

For the export, I used the following flags:
url = http://hostname
contentTypeGroup = "Custom Content Types"
contentTypeName = null
excludeParentFields = false
includeFieldDefinitions = true
includeListBindings = false
listName = null
removeEncodedSpaces = false
featureSafe = true
outputFile = "ctypes.xml"

Once I had my output (ctypes.xml), I had to remove any ... tag pairs. For some reason they were included in some of the field definitions (site columns) associated with the exported content types. I don't know what the ultimate effect of this is, but it didn't matter to me in this case, and it worked without it.

The structure of the ctypes.xml file should look like this (values removed):
<elements xmlns="http://schemas.microsoft.com/sharepoint/">
<field id=" Name="" sourceid=" StaticName="" group=" RowOrdinal="" type=" DisplayName="" readonly=" Sealed="" colname=" PITarget="" piattribute=" /">
[...]
<contenttype id=" Name="" group=" Version="">
<folder targetname=" /">
<fieldrefs>
<fieldref id=" Name="">
<fieldref id=" Name="">
</fieldrefs></contenttype></elements>

Create a 12 hive feature directory:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\MyCtypes

Copy the ctypes.xml file into the directory.

Add feature.xml with content like this (with a new Id GUID, and change Scope if desired, remember Scope="Site" means SiteCollection):
<?xml version="1.0" encoding="utf-8"?>
<feature id="031D991D-2559-4512-BE86-024E9CA035B3"
Title="My Imported content types"
Description="Content types imported from wherever"
Version="1.0.0.0"
Scope="Site"
ImageUrl="Health.GIF"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<elementmanifests>
<elementmanifest location="ctypes.xml">
</elementmanifests>
</feature>

Install the feature this way:
"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe" -o installfeature -name MyCtypes

(if your xml file doesn't comply to proper schema, this installfeature will fail)

Now go activeate the feature. In this case, go to "Site Collection Features" and click activate for the new feature. This will add the content types to the site collection.

Uninstall this way:
"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe" -o uninstallfeature -name MyCtypes

This uninstall will remove the content types from the site collection. I suspect this will fail if the content types are in use by the site.

No comments: