<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dev Team Assemble &#187; content types</title>
	<atom:link href="http://www.calvinirwin.net/tag/content-types/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.calvinirwin.net</link>
	<description>Evil beware!</description>
	<lastBuildDate>Mon, 06 Feb 2012 18:42:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Lookup Fields and Content Types</title>
		<link>http://www.calvinirwin.net/2009/09/09/lookup-fields-and-content-types/</link>
		<comments>http://www.calvinirwin.net/2009/09/09/lookup-fields-and-content-types/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 04:43:35 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[content types]]></category>
		<category><![CDATA[lookup fields]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=201</guid>
		<description><![CDATA[For the last little bit I have been working on learning how to package up all site infrastructure into a single feature.  All of this is pretty straight forward and I didn't really have any issues.....UNTIL  I tried to get lookup columns working.
Using CAML
I did some reading on the subject and apparently you can do all [...]]]></description>
			<content:encoded><![CDATA[<p>For the last little bit I have been working on learning how to package up all site infrastructure into a single feature.  All of this is pretty straight forward and I didn't really have any issues.....UNTIL  I tried to get lookup columns working.</p>
<h3>Using CAML</h3>
<p>I did some reading on the subject and apparently you can do all of this using CAML alone.  I came across a<a href="http://blogs.msdn.com/joshuag/archive/2008/03/14/add-sharepoint-lookup-column-declaratively-through-caml-xml.aspx"> post from Josh Gaffery</a> supporting this claim, but I simply could not get this working...so i gave up after spinning my wheels on it for longer than I wanted to.  Josh's approach is to use the list URL as opposed to the GUID that links the column to the source list and he has put up an update explaining it further.  Nonetheless....didn't work for me.</p>
<h3>Using Feature Receiver</h3>
<p>I knew that at this point I would have to take the feature reciever approach and modify the fields in place or create them.  I found two sources that both take different approachs to this problem.</p>
<ul>
<li>Chris O'Brien has put together the a project on <a href="http://www.codeplex.com/SP2007LookupFields">CodePlex </a>that will create the lookup columns at activation time.  This actully sounds like a pretty good approach but unfortunately it didn't work for me.  I dont know if there is something wrong with my environment but I encountered a few errors doing this...things like the fields not rendering on the page layouts and getting the "<strong>The local device name is already in use. (Exception from HRESULT: 0x80070055)</strong>" error.</li>
<li>Waldek Mastykarz has a great post on creating the columns via code <a href="http://blog.mastykarz.nl/sharepoint-programmatically-provisioning-lookup-fields/">here</a>.</li>
</ul>
<p>Basically I took a hybrid approach to doing this by mixing the two approaches mentioned above.  I created a custom XML file that I deploy into the layouts directory and then use a feature reciever to read the xml content and create lookup columns based on this.  I also added a deactiving event to remove the fields when the feature is deactivated.  Heres the feature reciever code:</p>
<pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> FeatureActivated<span style="color: #000000;">&#40;</span>SPFeatureReceiverProperties properties<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SPSite site = properties.<span style="color: #0000FF;">Feature</span>.<span style="color: #0000FF;">Parent</span> <span style="color: #0600FF;">as</span> SPSite<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> contentTypes = <span style="color: #0600FF;">null</span>;
                <span style="color: #FF0000;">string</span> listName = <span style="color: #0600FF;">null</span>;
                <span style="color: #FF0000;">string</span> fieldName = <span style="color: #0600FF;">null</span>;
                <span style="color: #FF0000;">string</span> groupName = <span style="color: #0600FF;">null</span>;
                <span style="color: #FF0000;">string</span> staticName = <span style="color: #0600FF;">null</span>;
                <span style="color: #FF0000;">string</span> lookupFieldName = <span style="color: #808080;">&quot;Title&quot;</span>;
                <span style="color: #FF0000;">bool</span> mult = <span style="color: #0600FF;">false</span>;
                <span style="color: #FF0000;">bool</span> required = <span style="color: #0600FF;">false</span>;
                <span style="color: #FF0000;">string</span> filePath = properties.<span style="color: #0000FF;">Feature</span>.<span style="color: #0000FF;">Properties</span><span style="color: #000000;">&#91;</span>
                   <span style="color: #808080;">&quot;ColumnDefinitionPath&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span>;
&nbsp;
                XmlTextReader xReader = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlTextReader<span style="color: #000000;">&#40;</span>
                  HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Server</span>.<span style="color: #0000FF;">MapPath</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">@"~\_layouts\&quot;</span> + filePath<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">LocalName</span> == <span style="color: #808080;">&quot;Field&quot;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        <span style="color: #008080;">#region Get values from attributes</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;List&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            listName = xReader.<span style="color: #0000FF;">Value</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            fieldName = xReader.<span style="color: #0000FF;">Value</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;StaticName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            staticName = xReader.<span style="color: #0000FF;">Value</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Group&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            groupName = xReader.<span style="color: #0000FF;">Value</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;LookUpField&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            lookupFieldName = xReader.<span style="color: #0000FF;">Value</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;ExistInContentTypes&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            contentTypes = xReader.<span style="color: #0000FF;">Value</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Mult&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            <span style="color: #FF0000;">bool</span>.<span style="color: #0000FF;">TryParse</span><span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">Value</span>, <span style="color: #0600FF;">out</span> mult<span style="color: #000000;">&#41;</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">MoveToAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Required&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            <span style="color: #FF0000;">bool</span>.<span style="color: #0000FF;">TryParse</span><span style="color: #000000;">&#40;</span>xReader.<span style="color: #0000FF;">Value</span>, <span style="color: #0600FF;">out</span> required<span style="color: #000000;">&#41;</span>;
                            xReader.<span style="color: #0000FF;">MoveToElement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
&nbsp;
                        <span style="color: #008080;">#endregion</span>
&nbsp;
                        SPFieldLookup lookup = CreateLookupField<span style="color: #000000;">&#40;</span>
                          fieldName, groupName, required, mult, site.<span style="color: #0000FF;">RootWeb</span>,
                          site.<span style="color: #0000FF;">RootWeb</span>.<span style="color: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span>listName<span style="color: #000000;">&#93;</span>, lookupFieldName, staticName<span style="color: #000000;">&#41;</span>;
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>contentTypes != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> s <span style="color: #0600FF;">in</span> contentTypes.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">','</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                            <span style="color: #000000;">&#123;</span>
                                LinkFieldToContentType<span style="color: #000000;">&#40;</span>s.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">&#40;</span>SPField<span style="color: #000000;">&#41;</span>lookup<span style="color: #000000;">&#41;</span>;
                            <span style="color: #000000;">&#125;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                xReader.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SPFieldLookup CreateLookupField<span style="color: #000000;">&#40;</span>
          <span style="color: #FF0000;">string</span> fieldName, <span style="color: #FF0000;">string</span> group, <span style="color: #FF0000;">bool</span> required, <span style="color: #FF0000;">bool</span> allowMultipleValues,
          SPWeb w, SPList lookupList, <span style="color: #FF0000;">string</span> lookupField, <span style="color: #FF0000;">string</span> staticName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            w.<span style="color: #0000FF;">Fields</span>.<span style="color: #0000FF;">AddLookup</span><span style="color: #000000;">&#40;</span>fieldName, lookupList.<span style="color: #0000FF;">ID</span>,
               lookupList.<span style="color: #0000FF;">ParentWeb</span>.<span style="color: #0000FF;">ID</span>, required<span style="color: #000000;">&#41;</span>;
            SPFieldLookup lookup = <span style="color: #000000;">&#40;</span>SPFieldLookup<span style="color: #000000;">&#41;</span>w.<span style="color: #0000FF;">Fields</span><span style="color: #000000;">&#91;</span>fieldName<span style="color: #000000;">&#93;</span>;
            lookup.<span style="color: #0000FF;">AllowMultipleValues</span> = allowMultipleValues;
            lookup.<span style="color: #0000FF;">LookupField</span> = lookupField;
            lookup.<span style="color: #0000FF;">StaticName</span> = staticName;
            lookup.<span style="color: #0000FF;">Group</span> = group;
            lookup.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">return</span> lookup;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> LinkFieldToContentType<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> contentType, SPField field<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SPSite site = SPContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Site</span> <span style="color: #0600FF;">as</span> SPSite<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                SPContentType ct = site.<span style="color: #0000FF;">RootWeb</span>.<span style="color: #0000FF;">ContentTypes</span><span style="color: #000000;">&#91;</span>contentType<span style="color: #000000;">&#93;</span>;
                ct.<span style="color: #0000FF;">FieldLinks</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPFieldLink<span style="color: #000000;">&#40;</span>field<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
                ct.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>; <span style="color: #008080; font-style: italic;">// will update children</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre>
<p>As you can read from the above code the xml file would need to have a node like below for each lookup column:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Field</span>
         <span style="color: #000066;">Type</span>=<span style="color: #ff0000;">&quot;Lookup&quot;</span>
         <span style="color: #000066;">List</span>=<span style="color: #ff0000;">&quot;Access Type&quot;</span>
         <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;AccessTypeColumn&quot;</span>
         <span style="color: #000066;">StaticName</span>=<span style="color: #ff0000;">&quot;Access_x0020_Type&quot;</span>
         <span style="color: #000066;">Group</span>=<span style="color: #ff0000;">&quot;Infrastructure&quot;</span>
         <span style="color: #000066;">ExistInContentTypes</span>=<span style="color: #ff0000;">&quot;THIS IS A COMMA DELIMITED LIST OF CONTENT NAMES&quot;</span>
         <span style="color: #000066;">LookUpField</span>=<span style="color: #ff0000;">&quot;Title&quot;</span>
         <span style="color: #000066;">Mult</span>=<span style="color: #ff0000;">&quot;TRUE&quot;</span>
         <span style="color: #000066;">Required</span>=<span style="color: #ff0000;">&quot;FALSE&quot;</span>
        <span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;</pre>
<h3>The Final Project</h3>
<p>So here are all the pieces of my infrastructure project. Notice the placement of the lookupfields xml file...this is because the layouts directory is setup as a virtual directory for every sharepoint site and we can read files from there without a permissions problem.</p>
<table>
<tr>
<td><em>12/TEMPLATES/FEATURES/myfeature/lists.xml</em></td>
<td>this contains the source lists for the lookup fields</td>
</tr>
<tr>
<td><em>12/TEMPLATES/FEATURES/myfeature/contenttypes.xml</em></td>
<td>this contains the content type definitions MINUS the lookup fields</td>
</tr>
<tr>
<td><em>12/TEMPLATES/FEATURES/myfeature/sitecolumns.xml</em></td>
<td>this contains all the other fields included in the content types</td>
</tr>
<tr>
<td><em>12/TEMPLATES/FEATURES/myfeature/feature.xml</em></td>
<td>the feature def</td>
</tr>
<tr>
<td><em>12/TEMPLATES/LAYOUTS/myfeature/lookupfields.xml</em></td>
<td>this contains all the lookup fields that need to be provisioned</td>
</tr>
</table>
<p>Hopefully this helps anyone who's been having problems getting this going.  And a big thanks to Waldek, Chris and Josh for their posts.</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Configuration' rel='tag' target='_self'>Configuration</a>, <a class='technorati-link' href='http://technorati.com/tag/content+types' rel='tag' target='_self'>content types</a>, <a class='technorati-link' href='http://technorati.com/tag/Development' rel='tag' target='_self'>Development</a>, <a class='technorati-link' href='http://technorati.com/tag/lookup+fields' rel='tag' target='_self'>lookup fields</a>, <a class='technorati-link' href='http://technorati.com/tag/SharePoint' rel='tag' target='_self'>SharePoint</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2009/09/09/lookup-fields-and-content-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

