<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dotnetmafia.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Corey Roth [MVP] : Content Type, CAML, SharePoint</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Content+Type/CAML/SharePoint/default.aspx</link><description>Tags: Content Type, CAML, SharePoint</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Using CAML to deploy a lookup column via Feature</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/09/02/using-caml-to-deploy-a-lookup-column-via-feature.aspx</link><pubDate>Wed, 02 Sep 2009 21:47:49 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:962</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=962</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/09/02/using-caml-to-deploy-a-lookup-column-via-feature.aspx#comments</comments><description>&lt;p&gt;This week, I needed to deploy lookup columns to some of my lists and as usual I wanted to avoid writing code at all costs.&amp;#160; As some of you may know, &lt;a href="http://www.dotnetmafia.com/blogs/kylekelin/default.aspx"&gt;Kyle Kelin&lt;/a&gt; and I &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/05/12/code-samples-from-our-caml-vs-api-talk-at-tulsa-sharepoint-interest-group.aspx"&gt;debate&lt;/a&gt; this topic often as he prefers a code approach.&amp;#160; I figured it had to be possible with CAML, but many claimed it was not even possible.&amp;#160; A few approaches showed up out there involving using code to modify the elements.xml file with your GUID, but that just wasn’t going to cut it for me.&amp;#160; One popular post on the topic by &lt;a href="http://blogs.msdn.com/joshuag/archive/2008/03/14/add-sharepoint-lookup-column-declaratively-through-caml-xml.aspx"&gt;Josh Gaffey&lt;/a&gt;, started me in the right direction, but there were a few hurdles I ran into as I was trying to implement it.&amp;#160; It would create the list, show the content type, and site columns, but when I tried to create a new item, the lookup column was not there.&amp;#160; The basic technique is that you specify the path to the list in the form of (Lists/MyListName) in the &lt;em&gt;List &lt;/em&gt;attribute of the &lt;em&gt;Field&lt;/em&gt; element in both your schema.xml file of your document library template as well as the definition of the site column.&amp;#160; The first thing I learned here is that you cannot simply omit declaring site columns and a content type out and go with list level columns.&amp;#160; It simply will not work (no idea why).&lt;/p&gt;  &lt;p&gt;   &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt; Here is how I ended up getting everything to work.&amp;#160; I created my initial lists, content types, and site columns through the SharePoint UI.&amp;#160; I then used &lt;a href="http://spsource.codeplex.com/"&gt;SPSource&lt;/a&gt; (seriously a great tool) to export the content type, site columns, and lists (both the source list and the one that contained the lookup column).&amp;#160; My goal was to deploy these items to another site collection.&amp;#160;&amp;#160; I recommend creating separate features for your content types and the list definition.&amp;#160; This way you can ensure the content type is deployed first.&amp;#160; To export your content type / site columns, create a new stub feature.xml file as described on their site and then create an .spsource file with just the content type in it like this (no need to specify columns).&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&amp;lt;&lt;span style="color:#cc7832;"&gt;Elements&lt;/span&gt; &lt;span style="color:silver;"&gt;xmlns&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;http://schemas.microsoft.com/sharepoint/&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;ContentType&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Test&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;lt;/&lt;span style="color:#cc7832;"&gt;Elements&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;In my example, my content type was called &lt;em&gt;Test&lt;/em&gt;.&amp;#160; Create a separate feature folder for your list template and instance.&amp;#160; Create another feature.xml and an .spsource file for the list which looks something like this.&amp;#160; My list is also named &lt;em&gt;Test&lt;/em&gt; in this case.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&amp;lt;&lt;span style="color:#cc7832;"&gt;Elements&lt;/span&gt; &lt;span style="color:silver;"&gt;xmlns&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;http://schemas.microsoft.com/sharepoint/&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;ListTemplate&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Test&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;lt;/&lt;span style="color:#cc7832;"&gt;Elements&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;SPSource does a good job when it exports my list definition and properly specifies the list using a path instead of a guid in my lookup column as shown below (from schema.xml). &lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&amp;lt;&lt;span style="color:#cc7832;"&gt;Field&lt;/span&gt; &lt;span style="color:silver;"&gt;Type&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Lookup&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;DisplayName&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;DocumentCategory&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Required&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;FALSE&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;List&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Lists/Categories&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;ShowField&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Title&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;UnlimitedLengthInDocumentLibrary&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;FALSE&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Group&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;My Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;ID&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;{9266e0fa-ae49-438c-acdc-73063684ac8f}&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;SourceID&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;{50b253e5-e90e-4781-8fce-cece417b185e}&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;StaticName&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;DocumentCategory&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;DocumentCategory&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Customization&lt;/span&gt;=&amp;quot;&amp;quot; &lt;span style="color:silver;"&gt;ColName&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;int1&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;RowOrdinal&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;0&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We don’t actually have to change a thing in this file.&amp;#160; What I did have to change is the List attribute of the file it generated for my site columns.&amp;#160; As Josh’s post above stated, we have to change the &lt;em&gt;List&lt;/em&gt; attribute from a GUID to the path of the list.&amp;#160; His post also mentioned you might want to set the PrependId attribute but I have since learned that this is only used when you are using the &lt;em&gt;LookupMulti &lt;/em&gt;type (although I can’t remember the source I got this from).&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&amp;lt;&lt;span style="color:#cc7832;"&gt;Field&lt;/span&gt; &lt;span style="color:silver;"&gt;Type&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Lookup&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;DisplayName&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;DocumentCategory&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Required&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;TRUE&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;List&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Lists/Categories&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;ShowField&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Title&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;UnlimitedLengthInDocumentLibrary&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;FALSE&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Group&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;My Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;ID&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;{9266e0fa-ae49-438c-acdc-73063684ac8f}&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;DocumentCategory&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;At this point you can deploy your site columns, content type, and then custom list (in that order) and if all goes well you will be able to create a new item of that content type with the lookup column functioning.&amp;#160; One thing to note, if you get this wrong, your list will behave oddly.&amp;#160; One thing I noticed is that it won’t turn on content types for the list when that happens.&amp;#160; If this is happening go back and check your work.&amp;#160; Another thing people mentioned is that the source list for the lookup column has to exist before you create the lookup column.&amp;#160; This in fact is not true.&amp;#160; It will work and it will display a drop down list for the lookup column, but it will be empty of course since the list does not exist.&lt;/p&gt;  &lt;p&gt;Along the way I figured out a few other things.&amp;#160; When I was trying to figure this out, I was trying to just create the content type and then manually add it to the list via the UI.&amp;#160; Doesn’t work.&amp;#160; If you try to add a content type with a lookup column defined in this way to an existing list, you will get the following error.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Exception from HRESULT: 0x80040E07 at Microsoft.SharePoint.Library.SPRequestInternalClass.AddField(String bstrUrl, String bstrListName, String bstrSchemaXml, Int32 grfAdd)      &lt;br /&gt;at Microsoft.SharePoint.Library.SPRequest.AddField(String bstrUrl, String bstrListName, String bstrSchemaXml, Int32 grfAdd) &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The only way to get the content type associated with the list is via CAML when it is created.&amp;#160; Another odd thing is that the lookup column never shows it is bound to the other list correctly.&amp;#160; Here is what the source lookup column looked like.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/SharePointLookupColumnSource_13F82363.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="SharePointLookupColumnSource" border="0" alt="SharePointLookupColumnSource" src="http://www.dotnetmafia.com/blogs/dotnettipoftheday/SharePointLookupColumnSource_thumb_659E9DB5.png" width="261" height="189" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice where it says &lt;em&gt;Get information from, &lt;/em&gt;the name of the list is present.&amp;#160; Here is what my copy of the list looks like.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/SharePointLookupColumnCopy_65326AC0.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="SharePointLookupColumnCopy" border="0" alt="SharePointLookupColumnCopy" src="http://www.dotnetmafia.com/blogs/dotnettipoftheday/SharePointLookupColumnCopy_thumb_04E14489.png" width="265" height="183" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice that the list name is not present.&amp;#160; Strangely enough though, everything works fine in the copy.&amp;#160; This one through me for a loop for a while because it didn’t seem like things weren’t working and as I mentioned above I couldn’t add the content type to a list (nor could I add the column to a content type).&amp;#160; &lt;/p&gt;  &lt;p&gt;As you can see there are few oddities about deploying lookup columns in this manner, but it does work.&amp;#160; &lt;a href="http://twitter.com/spkyle"&gt;@SPKyle&lt;/a&gt; informed me he could have written code to do this hours ago, but I am quite happy that I can deploy things in this manner now.&lt;/p&gt;  &lt;p&gt;   &lt;p&gt;Twitter: &lt;a href="http://twitter.com/coreyroth"&gt;@coreyroth&lt;/a&gt;&lt;/p&gt;&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=962" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Content+Type/default.aspx">Content Type</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Feature/default.aspx">Feature</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/CAML/default.aspx">CAML</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SPSource/default.aspx">SPSource</category></item></channel></rss>