How To: Adding a Content Type to a List the Correct Way

Posted Monday, January 28, 2008 10:36 AM by CoreyRoth

I have been working with document libraries quite a bit lately and I ran into yet another task that seemed like it should be pretty easy but in fact there is a complexity.  I needed to add my own content type to the document library which was made up of multiple custom site columns.  This seemed pretty easy and in fact, there is actually some info in the SDK on it.

 http://msdn2.microsoft.com/en-us/library/aa543576.aspx

Adding my own content types was pretty easy.  You just add a ContentTypeRef element to the ContentTypes section which includes refences to your own content types in the schema.xml of your List Template.  The key thing to remember is to prefix the GUID (after stripping out spaces) with the base content type (i.e.: 0x0120 + 00).  Check out my previous posts for more info on that.  Here is an example.

<ContentTypes>

      <ContentTypeRef ID="0x01010058B73A6A27334D3B9355D7AAB899D332">

        <Folder TargetName="Forms/Document" />

      </ContentTypeRef>

      <ContentTypeRef ID="0x012000521AACBC415A478390B668D81308E454" />   

    </ContentTypes>

Now, after I added that, I deployed an instance of my list template, and discovered that it had my new content types included.  Awesome.  Things are looking good. Well, maybe not.  My content types were there, but the new inherited content types it created did not have any columns.  Now, I had always known that when you create a list with custom content types and site columns, it makes a copy of said items at the list level and adds them.  This is apparent when you configure one of these using the UI.  However, when you are dealing with CAML, this is just not the case.  After quite a bit of Google searching and not finding much (big surprise), I noticed in the documentation of the same page linked above, the following paragraph.

When Windows SharePoint Services provisions a list, it provisions only those columns declared in the base type schema of the list or in the list schema. If you reference a site content type in the list schema, and that content type references site columns that are not included in the base type schema of the list or in the list schema, those columns are not provisioned on the list. You must also include those columns in the list schema for Windows SharePoint Services to provision them on the list.

Ok, well that's lame.  I knew what I needed to do now.  I needed to add my site columns to the list template, but did not yet know the syntax and where to put the needed elements.  I figured it would do this automatically, but   I again tried to search Google and again came up with nothing.  Sometimes, I would just like to see a complete example of how to do something (I'll try to post a complete example of setting up a list or document library in this manner some time).  I finally figured it out.  It matches the syntax of what you use to define your fields to being with.

<Field ID="{E059529E-2D14-4e52-A853-994DA83B76FA}" Type="Text" Name="MyCustomColumn" DisplayName="My Custom Column" Group="Custom Column Group" />

This is yet another thing that I figured would be simple in SharePoint but isn't.  Hopefully, this will help someone else down the road when they are trying to do this.  I really figured, SharePoint would add the columns automatically, but it doesn't.  It just seems like this can cause a ton of issues later, because you have to remember to update each list if you ever make changes to your site columns.  I really wish it actually used the site column itself instead of making a copy of it.

Filed under:

Comments

# re: How To: Adding a Content Type to a List the Correct Way

Friday, January 29, 2010 5:51 AM by jernej

i've came to the same problem. i totally agre, this is really lame.

# Custom List Definition &laquo; Sladescross&#039;s Blog

Wednesday, October 20, 2010 3:25 AM by Custom List Definition « Sladescross's Blog

Pingback from  Custom List Definition &laquo; Sladescross&#039;s Blog

Leave a Comment

(required)
(required)
(optional)
(required)