<?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; Development</title>
	<atom:link href="http://www.calvinirwin.net/tag/development/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>SQL Server Recovery Model &#8211; Modification Script</title>
		<link>http://www.calvinirwin.net/2011/10/20/sql-server-recovery-model-modification-script/</link>
		<comments>http://www.calvinirwin.net/2011/10/20/sql-server-recovery-model-modification-script/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 15:59:41 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[moss 2007]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=298</guid>
		<description><![CDATA[I often find in my sharepoint development environments that I have issues with transaction logs filling up.  This gives me the "Error" page with no indication of what the problem is...yes I can go and change the &#60;customErrors&#62; tag to set the customer errors mode to off and add the callstack to the page [...]]]></description>
			<content:encoded><![CDATA[<p>I often find in my sharepoint development environments that I have issues with transaction logs filling up.  This gives me the "Error" page with no indication of what the problem is...yes I can go and change the &lt;customErrors&gt; tag to set the customer errors mode to off and add the callstack to the page but it still doesn't address the problem.  I whipped this script up because the dev sql server we are using has well over a hundred db's on it and I didn't fell like manually typing the ALTER DATABASE [xxx] SET RECOVERY SIMPLE for each one or doing the clickity click thing either.  Now the solution below probably breaks lots of best practices and such but it works for what I wanted it to do....which is modify all existing db's recovery mode.  To modify new db's recovery mode, just set the model db's option to simple and voila..you are ready to go.
</p>
<p>
Quick note: <strong>YOU PROBABLY DO NOT WANT TO EVER...I MEAN EVER...USE THIS ON A PRODUCTION SYSTEM.</strong>
</p>
<p><code language='sql'><br />
*/</p>
<p>	Modify th recovery model for all databases on your server</p>
<p>*/</p>
<p>	DECLARE @lasterror int</p>
<p>	DECLARE @sql nvarchar(max)</p>
<p>	DECLARE @dbname nvarchar(100)</p>
<p>	DECLARE @dbid int</p>
<p>	DECLARE @recModel int</p>
<p>	-- cycle the log</p>
<p>	exec sp_cycle_errorlog </p>
<p>	CREATE Table #dbInfo</p>
<p>	(</p>
<p>		database_id int NOT NULL,</p>
<p>		dbName nvarchar(100) NOT NULL,</p>
<p>		recoveryModel int</p>
<p>	)</p>
<p>	--SELECT database_id, [name], recovery_model FROM sys.databases order by database_id</p>
<p>	SELECT TOP 1 @dbid = database_id FROM sys.databases where database_id > 4  ORDER BY database_id asc;</p>
<p>	SELECT @dbname = [name] FROM sys.databases where database_id = @dbid;</p>
<p>	SELECT @recModel = recovery_model FROM sys.databases where database_id = @dbid;</p>
<p>	-- begin backup process</p>
<p>	WHILE @dbid > 0</p>
<p>	BEGIN</p>
<p>		INSERT INTO #dbInfo (database_id, dbName, recoveryModel) VALUES (@dbid, @dbname, @recModel);</p>
<p>		IF @recModel = 1</p>
<p>		BEGIN</p>
<p>			SELECT @sql = 'ALTER DATABASE [' + @dbname + '] SET RECOVERY SIMPLE'</p>
<p>			EXEC sp_executesql @sql</p>
<p>			SELECT @lasterror = @@ERROR</p>
<p>			IF @lasterror <> 0</p>
<p>				-- log the error to the message console</p>
<p>				PRINT 'ERROR: Changing the recovery mode for database : ' + @dbname + ' with error number ' + CAST(@lasterror as nvarchar(25))</p>
<p>			ELSE</p>
<p>				PRINT 'Recovery mode changed to simple (3) for database : ' + @dbname</p>
<p>		END</p>
<p>		-- get a new record</p>
<p>		SELECT @dbid = 0;</p>
<p>		SELECT TOP 1 @dbid =  database_id FROM sys.databases where database_id > 4 and</p>
<p>			database_id NOT IN (SELECT DISTINCT x.database_id FROM #dbInfo x);</p>
<p>		SELECT @dbname = [name] FROM sys.databases where database_id = @dbid;</p>
<p>		SELECT @recModel = recovery_model FROM sys.databases where database_id = @dbid;</p>
<p>	END	</p>
<p>	DROP Table #dbInfo</p>
<p>	SELECT database_id, [name], recovery_model FROM sys.databases order by database_id</p>
<p>	GO</p>
<p></code></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/Development' rel='tag' target='_self'>Development</a>, <a class='technorati-link' href='http://technorati.com/tag/moss+2007' rel='tag' target='_self'>moss 2007</a>, <a class='technorati-link' href='http://technorati.com/tag/SharePoint' rel='tag' target='_self'>SharePoint</a>, <a class='technorati-link' href='http://technorati.com/tag/SQL+Server' rel='tag' target='_self'>SQL Server</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2011/10/20/sql-server-recovery-model-modification-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Execute Commands in Windows via .NET Application</title>
		<link>http://www.calvinirwin.net/2011/05/26/execute-commands-in-windows-via-net-application/</link>
		<comments>http://www.calvinirwin.net/2011/05/26/execute-commands-in-windows-via-net-application/#comments</comments>
		<pubDate>Thu, 26 May 2011 15:44:50 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[cmd]]></category>
		<category><![CDATA[command]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=281</guid>
		<description><![CDATA[Heres a neat little snippit that comes in handy for running commands against the operating system.
Sourced directly from here: http://stackoverflow.com/questions/691716/running-cmd-commands-via-net ...but I like having the code here as well.
&#160;
// Kills a process
&#60;span&#62; &#60;/span&#62; private static void ExecuteCommand&#40;string command&#41;
&#123;
try
&#123;
// create the ProcessStartInfo using &#34;cmd&#34; as the program to be run,
// and &#34;/c &#34; as the parameters. [...]]]></description>
			<content:encoded><![CDATA[<p>Heres a neat little snippit that comes in handy for running commands against the operating system.</p>
<p>Sourced directly from here: <a href="http://stackoverflow.com/questions/691716/running-cmd-commands-via-net" target="_blank">http://stackoverflow.com/questions/691716/running-cmd-commands-via-net</a> ...but I like having the code here as well.</p>
<pre class="csharp">&nbsp;
<span style="color: #008080; font-style: italic;">// Kills a process</span>
&lt;span&gt; &lt;/span&gt; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExecuteCommand<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> command<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">try</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #008080; font-style: italic;">// create the ProcessStartInfo using &quot;cmd&quot; as the program to be run,</span>
<span style="color: #008080; font-style: italic;">// and &quot;/c &quot; as the parameters. &amp;gt; tell the command to execute the command that follows</span>
<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Diagnostics</span>.<span style="color: #0000FF;">ProcessStartInfo</span> procStartUpInfo =
<a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Diagnostics</span>.<span style="color: #0000FF;">ProcessStartInfo</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;cmd&quot;</span>, <span style="color: #808080;">&quot;/c &quot;</span> + command<span style="color: #000000;">&#41;</span>;
<span style="color: #008080; font-style: italic;">// The following commands are needed to redirect the standard output.</span>
<span style="color: #008080; font-style: italic;">// This means that it will be redirected to the Process.StandardOutput StreamReader.</span>
procStartUpInfo.<span style="color: #0000FF;">RedirectStandardOutput</span> = <span style="color: #0600FF;">true</span>;
procStartUpInfo.<span style="color: #0000FF;">UseShellExecute</span> = <span style="color: #0600FF;">false</span>;
<span style="color: #008080; font-style: italic;">// Do not create a window.</span>
procStartUpInfo.<span style="color: #0000FF;">CreateNoWindow</span> = <span style="color: #0600FF;">true</span>;
<span style="color: #008080; font-style: italic;">// Now we create a process, assign its ProcessStartInfo and start it</span>
<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Diagnostics</span>.<span style="color: #0000FF;">Process</span> proc = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Diagnostics</span>.<span style="color: #0000FF;">Process</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
proc.<span style="color: #0000FF;">StartInfo</span> = procStartUpInfo;
proc.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #008080; font-style: italic;">// Get the output into a string</span>
<span style="color: #FF0000;">string</span> result = proc.<span style="color: #0000FF;">StandardOutput</span>.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #008080; font-style: italic;">// Display the command output.</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>result<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception objException<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>objException<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>

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

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/.net' rel='tag' target='_self'>.net</a>, <a class='technorati-link' href='http://technorati.com/tag/cmd' rel='tag' target='_self'>cmd</a>, <a class='technorati-link' href='http://technorati.com/tag/command' rel='tag' target='_self'>command</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/Windows' rel='tag' target='_self'>Windows</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2011/05/26/execute-commands-in-windows-via-net-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing and Debugging a windows Service</title>
		<link>http://www.calvinirwin.net/2011/05/04/installing-and-debugging-a-windows-service/</link>
		<comments>http://www.calvinirwin.net/2011/05/04/installing-and-debugging-a-windows-service/#comments</comments>
		<pubDate>Wed, 04 May 2011 13:04:42 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=275</guid>
		<description><![CDATA[Here is some useful information that I always seem to forget when developing windows services.

To install/uninstall manually, which i find helpful to do from the bin folder of your project use the following commands:

INSTALL: %windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe [the name of the executable]
UNINSTALL: %windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u [the name of the executable]


To Debug a windows service add the following code [...]]]></description>
			<content:encoded><![CDATA[<p>Here is some useful information that I always seem to forget when developing windows services.</p>
<ul>
<li>To install/uninstall manually, which i find helpful to do from the bin folder of your project use the following commands:
<ul>
<li>INSTALL: %windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe [the name of the executable]</li>
<li>UNINSTALL: %windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u [the name of the executable]</li>
</ul>
</li>
<li>To Debug a windows service add the following code to your Main method<br />
<code><br />
static void Main(string[] args)<br />
{<br />
#if DEBUG<br />
System.Diagnostics.Debugger.Break();<br />
#endif</code></p>
<p><code> </code></p>
<p style="padding-left: 30px;"><code> ServiceBase.Run(new Service());</code></p>
<p style="padding-left: 30px;"><code>}<br />
</code>
</li>
<li>To create an installation package for a service: ....I will come back to that - no time to put it together today <img src='http://www.calvinirwin.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</li>
</ul>

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

<p class='technorati-tags'>Technorati Tags: <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/services' rel='tag' target='_self'>services</a>, <a class='technorati-link' href='http://technorati.com/tag/visual+studio' rel='tag' target='_self'>visual studio</a>, <a class='technorati-link' href='http://technorati.com/tag/Windows' rel='tag' target='_self'>Windows</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2011/05/04/installing-and-debugging-a-windows-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XmlSerializer and XmlRootAttribute Performance Issue</title>
		<link>http://www.calvinirwin.net/2011/02/10/xmlserializer-and-xmlrootattribute-performance-issue/</link>
		<comments>http://www.calvinirwin.net/2011/02/10/xmlserializer-and-xmlrootattribute-performance-issue/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 03:28:52 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[XmlSerializer]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=262</guid>
		<description><![CDATA[I have been working with xml serialization over the last week or so and ran into a strange issue today.  It only came to light when I decided to implement IXmlSerializable and write my own methods for reading and writing the xml.  I posted recently about using he XmlRootAttribute as a fix for [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working with xml serialization over the last week or so and ran into a strange issue today.  It only came to light when I decided to implement IXmlSerializable and write my own methods for reading and writing the xml.  I <a href="http://www.calvinirwin.net/2011/02/10/xmlserialization-deserialize-causes-xmlns-was-not-expected/">posted</a> recently about using he XmlRootAttribute as a fix for the <a href="http://www.calvinirwin.net/2011/02/10/xmlserialization-deserialize-causes-xmlns-was-not-expected/">xmlns='' &gt; was not expected</a> error I was seeing.  This all worked fine and dandy until I really started to utilize that method over and over again in my code.  I found the performance of this to be horrible...so horrible in fact it was taking 2-3 minutes to deserialize a moderately complex 1MB xml file.  After doing some digging (on "The Google") I discovered that microsoft decided to only implement caching on the Xml serializer when you use one of the two "more common" constructors (see <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx">documentation</a>).  Of course it would seem the one I need would not be one of them.</p>
<p>I was thinking of somehow adding the XmlSerializers for each type/root pair that I was using to the application level cache or something like that but thought that might bring with it its own set of challenges.  I found a <a href="http://stackoverflow.com/questions/1534810/xmlserializer-performance-issue-when-specifying-xmlrootattribute">great article here on stack overflow</a> that shows a possible workaround for the problem using a static class to act as a cache or you.</p>
<p>Please note this code is taken directly from the article above:</p>
<p><code><br />
public static class XmlSerializerCache<br />
{<br />
     private static readonly Dictionary cache =<br />
     new Dictionary();</code></p>
<p><code>     public static XmlSerializer Create(Type type, XmlRootAttribute root)<br />
     {<br />
          var key = String.Format( CultureInfo.InvariantCulture, "{0}:{1}", type, root.ElementName);</p>
<p>          if (!cache.ContainsKey(key))<br />
          {<br />
               cache.Add(key, new XmlSerializer(type, root));<br />
          }<br />
</code><br />
<code>           return cache[key];<br />
     }<br />
}<br />
</code></p>
<p>Here is how you use it to create an XmlSerializer object:</p>
<p><code><br />
var xmlRootAttribute = new XmlRootAttribute("ExampleElement");<br />
var serializer = XmlSerializerCache.Create(target.GetType(), xmlRootAttribute);<br />
</code></p>
<p>After implementing that I was still experiencing the 2-3 minute deserialization one the first call but all subsequent calls to that code with similar xml took only 2 seconds.</p>
<p><strong>NOTE:</strong><br />
<em>Seems microsoft is in the habit of only half implementing caching in their code.  This problem is almost identical to the problems with the CrossListQueryCache object in SharePoint....where they again only implemented caching for two of the four available method signatures.  Here is an <a href="http://connect.microsoft.com/VisualStudio/feedback/details/98384/xmlserializer-memory-leak">interesting article</a> I found in the nether-regions of cyberspace where microsoft state this half implementation is by design...</em></p>

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

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/.net' rel='tag' target='_self'>.net</a>, <a class='technorati-link' href='http://technorati.com/tag/ASP.NET' rel='tag' target='_self'>ASP.NET</a>, <a class='technorati-link' href='http://technorati.com/tag/c+sharp' rel='tag' target='_self'>c sharp</a>, <a class='technorati-link' href='http://technorati.com/tag/c%23' rel='tag' target='_self'>c#</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/serialization' rel='tag' target='_self'>serialization</a>, <a class='technorati-link' href='http://technorati.com/tag/xml' rel='tag' target='_self'>xml</a>, <a class='technorati-link' href='http://technorati.com/tag/XmlSerializer' rel='tag' target='_self'>XmlSerializer</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2011/02/10/xmlserializer-and-xmlrootattribute-performance-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Site Collection Images and ImageFieldValue</title>
		<link>http://www.calvinirwin.net/2009/11/26/site-collection-images-and-imagefieldvalue/</link>
		<comments>http://www.calvinirwin.net/2009/11/26/site-collection-images-and-imagefieldvalue/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 03:33:46 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[moss 2007]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=237</guid>
		<description><![CDATA[One of the things I was working on today was trying to get an actual image from the library was that initially I was expecting to get a PublishingImage.  All the samples on MSDN and the ones strewn about the web refer to using the ImageFieldValue and getting it from a field in the SPListItem.  [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I was working on today was trying to get an actual image from the library was that initially I was expecting to get a PublishingImage.  All the samples on MSDN and the ones strewn about the web refer to using the ImageFieldValue and getting it from a field in the SPListItem.  Like outlined on <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.fields.imagefieldvalue.aspx">MSDN </a>:</p>
<pre class="csharp">  <span style="color: #008080; font-style: italic;">// Retrieve the current value from an SPListItem with a</span>
  <span style="color: #008080; font-style: italic;">// column of the ImageField type with the name imageFieldName</span>
  ImageFieldValue currentFieldValue =
        listItemWithImageField<span style="color: #000000;">&#91;</span>imageFieldName<span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">as</span> ImageFieldValue;</pre>
<p>The problem is this only works for images that are in a list, like the PublishingRollupImage of a page that links to an image in the Site Collection Images library.  The actual images in the library are stored as documents (Referenced using SPFile) so in order utilize them I need to extract the information out and create an image tag for it...like so:</p>
<pre class="csharp">&nbsp;
     ImageFieldValue imageValue = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ImageFieldValue<span style="color: #000000;">&#40;</span>
          <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;img src=<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> alt=<span style="color: #008080; font-weight: bold;">\&quot;</span>{1}<span style="color: #008080; font-weight: bold;">\&quot;</span> /&gt;&quot;</span>,
          file.<span style="color: #0000FF;">Url</span>, file.<span style="color: #0000FF;">Title</span> == <span style="color: #0600FF;">null</span> ? <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span> : file.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;</pre>
<p>Now that I had the ImageFieldValue it was easy to work with.  I could manipulate many aspects of the image using this object and then save it for rendering using ToString().  Funny thing is for my purposes (ImageGallery xml generation) I didn't even end up needing this code after all...frustrating but a good learning experience.  </p>
<p>Props to <a href="http://vlifeblog.com">Phanat Chan</a> as he did the initial development on this.</p>

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

<p class='technorati-tags'>Technorati Tags: <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/moss+2007' rel='tag' target='_self'>moss 2007</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/11/26/site-collection-images-and-imagefieldvalue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ERROR: Incompatible Web Part markup detected. Use &#8220;.dwp web part XML instead of *.webpart web part xml</title>
		<link>http://www.calvinirwin.net/2009/10/09/error-incompatible-web-part-markup-detected-use-dwp-web-part-xml-instead-of-webpart-web-part-xml/</link>
		<comments>http://www.calvinirwin.net/2009/10/09/error-incompatible-web-part-markup-detected-use-dwp-web-part-xml-instead-of-webpart-web-part-xml/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 01:18:15 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[web parts]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=212</guid>
		<description><![CDATA[I came across this problem recently. Its a little strange and only happened once I changed my base webpart class to inherit from Microsoft.SharePoint.WebPartPages.WebPart. There seems to be two fixes for this problem that I implemented:

remove the XMlNamespace from class declaration
Use a .dwp file instead of a .webpart file in your feature folder




Technorati Tags: Development, [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem recently. Its a little strange and only happened once I changed my base webpart class to inherit from Microsoft.SharePoint.WebPartPages.WebPart. There seems to be two fixes for this problem that I implemented:</p>
<ul>
<li>remove the XMlNamespace from class declaration</li>
<li>Use a .dwp file instead of a .webpart file in your feature folder</li>
</ul>

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

<p class='technorati-tags'>Technorati Tags: <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/SharePoint' rel='tag' target='_self'>SharePoint</a>, <a class='technorati-link' href='http://technorati.com/tag/web+parts' rel='tag' target='_self'>web parts</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2009/10/09/error-incompatible-web-part-markup-detected-use-dwp-web-part-xml-instead-of-webpart-web-part-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
		<item>
		<title>Custom Field Types&#8230;Building a base XML Field Type for SharePoint</title>
		<link>http://www.calvinirwin.net/2009/08/12/custom-field-types-seriously/</link>
		<comments>http://www.calvinirwin.net/2009/08/12/custom-field-types-seriously/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 23:47:10 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[custom field type]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=124</guid>
		<description><![CDATA[Sometimes when I am working with SharePoint I really feel like I got it.  You now that feeling you get when you know exactly what you are doing and have no problems implementing it...then there are times when I really just want to smash something.  And it really doesn't matter what...it could be anything!  This [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when I am working with SharePoint I really feel like I got it.  You now that feeling you get when you know exactly what you are doing and have no problems implementing it...then there are times when I really just want to smash something.  And it really doesn't matter what...it could be anything!  This is the feeling I got in the last couple of days working with custom field types in SharePoint.</p>
<p>It seems there are lots of blogs and articles out there talking about how to do it, but from what I could see (and to be fair I did not read them all so if you know of some good posts that would be great) it felt like most of the time I was only getting half the story.  I just finished putting together a very basic Custom Field Type that will display a multi line textbox in edit/new mode and simply display the value in a literal control in display mode.  In the grand scheme of things I am building a base XML Field Type to use for a series of additional Custom field types so essentially I want to store a string (yes I could use the XmlDocument)...but alas thats a story for another day.</p>
<p>So a bit of a primer, or rehash so I don't forget, when developing custom field types there are many pieces to it....not all of them required.  Here is a list of the items and what they do:</p>
<ul>
<li><strong>Field Type class </strong>- this class acts as the controller and hooks the rendering control, field type xml definition and the value class together.  It also provides validation at the field level via the GetValidatedString() method of the SPField object</li>
<li> <strong>Field Control class</strong> - this is the rendering engine and gives you the power to display your control in any manner you wish as well as perform validation on the data being saved via the UI.</li>
<li><strong>Field Value class</strong> - allows for custom logic to be put around mutiple column fields and different data structures that will be storing data for your field type.</li>
<li><strong>.ascx file in CONTROLTEMPLATES directory</strong> - this supplies rendering markup via a user control and hooks into the field control class</li>
<li><strong>fldtypes_*</strong> - this file that gets installed into the 12/TEMPLATES/XML directory and contains the information that SharePoint requires in order to load your field type into the system.  Such as the assembly to use, the parent type and other information.  This is required if the type is going to be applied to content types and become a field on a list.</li>
</ul>
<p>So for my requirements I only needed the field type, the field control and the fldtypes_xxx.xml file.  Lets look at the field type class I built</p>
<pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlFieldType : SPField
   <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> XmlFieldType<span style="color: #000000;">&#40;</span>SPFieldCollection fields, <span style="color: #FF0000;">string</span> fieldName<span style="color: #000000;">&#41;</span> : <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>fields, fieldName<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> XmlFieldType<span style="color: #000000;">&#40;</span>SPFieldCollection fields, <span style="color: #FF0000;">string</span> fieldName, <span style="color: #FF0000;">string</span> displayName<span style="color: #000000;">&#41;</span> : <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>fields, fieldName, displayName<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> BaseFieldControl FieldRenderingControl
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                BaseFieldControl fldControl = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlFieldControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                fldControl.<span style="color: #0000FF;">FieldName</span> = InternalName;
                <span style="color: #0600FF;">return</span> fldControl;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetFieldValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> value<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;
            <span style="color: #0600FF;">return</span> value;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> Type FieldValueType
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> DefaultValue
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                XmlDocument doc = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                doc.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SchemaXml</span><span style="color: #000000;">&#41;</span>;
                XmlNode nodeInFieldSchema = doc.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Field/Default&quot;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>nodeInFieldSchema != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">return</span> nodeInFieldSchema.<span style="color: #0000FF;">InnerXml</span>;
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">DefaultValue</span> = value; <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> GetValidatedString<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> value<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">GetValidatedString</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value == <span style="color: #0600FF;">null</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><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Required</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPFieldValidationException<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Invalid value for required field.&quot;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> value.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre>
<p>Lets review</p>
<ul>
<li>The two constructors are required...don't even bother</li>
<li><strong>FieldRenderingControl()</strong> method tells the field type which control to use</li>
<li><strong>GetFieldValue()</strong> method returns the value of the field....if we were using a custom type you would cast that to the appropriate type here</li>
<li><strong>FieldValueType()</strong> - get the type of the Value class</li>
<li><strong>DefautValue()</strong> - this gets the default value from the field if one is defined</li>
<li><strong>GetValidatedString()</strong> - perform field level validation</li>
</ul>
<p>The Field Control Class is fairly simple as well:</p>
<pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlFieldControl : BaseFieldControl
<span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> Literal _values;
        <span style="color: #0600FF;">protected</span> TextBox _editor;
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnInit<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnInit</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span>;
            EnsureChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Field</span> == <span style="color: #0600FF;">null</span> || <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ControlMode</span> == SPControlMode.<span style="color: #0000FF;">Invalid</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span>;
&nbsp;
            Controls.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">CreateChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            _editor = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TextBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            _editor.<span style="color: #0000FF;">TextMode</span> = TextBoxMode.<span style="color: #0000FF;">MultiLine</span>;
            _editor.<span style="color: #0000FF;">Height</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Unit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">300</span><span style="color: #000000;">&#41;</span>;
            _editor.<span style="color: #0000FF;">Width</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Unit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #000000;">&#41;</span>;
            Controls.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_editor<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ControlMode == SPControlMode.<span style="color: #0000FF;">Display</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                _values = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Literal<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                _values.<span style="color: #0000FF;">Text</span> = Convert.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span>ItemFieldValue<span style="color: #000000;">&#41;</span>;
                Controls.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_values<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Render<span style="color: #000000;">&#40;</span>HtmlTextWriter output<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            EnsureChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ControlMode == SPControlMode.<span style="color: #0000FF;">Display</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>_values != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    _values.<span style="color: #0000FF;">RenderControl</span><span style="color: #000000;">&#40;</span>output<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_editor != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                    _editor.<span style="color: #0000FF;">RenderControl</span><span style="color: #000000;">&#40;</span>output<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">string</span> Text
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">EnsureChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>._editor == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>._editor.<span style="color: #0000FF;">Text</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            set
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">EnsureChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>._editor != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">this</span>._editor.<span style="color: #0000FF;">Text</span> = value;
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> Value
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Text</span>; <span style="color: #000000;">&#125;</span>
            set
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Field</span> != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Text</span> = <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Field</span>.<span style="color: #0000FF;">GetFieldValueForEdit</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span>;
                    <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Value</span> = <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>value;
                    <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">UpdateFieldValueInItem</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre>
<p>Lets take a look at what this class is doing:</p>
<ul>
<li><strong>OnInit()</strong> - come on, do I have to explain?</li>
<li><strong>CreateChildControls() </strong>- initialize your controls and add to the Controls collection</li>
<li><strong>Render()</strong> - writes the controls to the HtmlTextWriter</li>
<li><strong>Text</strong> - This sets/gets the value in the controls</li>
<li><strong>Value</strong> - this sets the value in the Text property and notice the <strong>base.UpdateFieldValueInItem();</strong> statement...this oushes the value from Value to ItemFieldValue and allows you to access this value in display mode because Value returns null (which i did not find the documentation on M$ thank you very much).</li>
</ul>
<p>So that leaves the last piece of the pie for my simple implementation the fldtypes file:</p>
<pre class="xml"><span style="color: #ddbb00;">&amp;lt;</span>FieldTypes<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>FieldType<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;TypeName&quot;<span style="color: #ddbb00;">&amp;gt;</span>XmlFieldType<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;ParentType&quot;<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;InternalType&quot;<span style="color: #ddbb00;">&amp;gt;</span>Note<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;SQLType&quot;<span style="color: #ddbb00;">&amp;gt;</span>ntext<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;TypeDisplayName&quot;<span style="color: #ddbb00;">&amp;gt;</span>Xml Field<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;TypeShortDescription&quot;<span style="color: #ddbb00;">&amp;gt;</span>Xml controls<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;UserCreatable&quot;<span style="color: #ddbb00;">&amp;gt;</span>TRUE<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;ShowOnListCreate&quot;<span style="color: #ddbb00;">&amp;gt;</span>TRUE<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;Sortable&quot;<span style="color: #ddbb00;">&amp;gt;</span>FALSE<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;AllowBaseTypeRendering&quot;<span style="color: #ddbb00;">&amp;gt;</span>FALSE<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;Filterable&quot;<span style="color: #ddbb00;">&amp;gt;</span>FALSE<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Field Name=&quot;FieldTypeClass&quot;<span style="color: #ddbb00;">&amp;gt;</span>Website.CustomFields.XmlFieldType, Website.CustomFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=346fcdd567259ee6<span style="color: #ddbb00;">&amp;lt;</span>/Field<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>RenderPattern Name=&quot;HeaderPattern&quot;<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Property Select=&quot;DisplayName&quot; HTMLEncode=&quot;TRUE&quot;/<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>/RenderPattern<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>RenderPattern Name=&quot;DisplayPattern&quot;<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>Column/<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>/RenderPattern<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>/FieldType<span style="color: #ddbb00;">&amp;gt;</span>
<span style="color: #ddbb00;">&amp;lt;</span>/FieldTypes<span style="color: #ddbb00;">&amp;gt;</span></pre>
<p>So there you have it.  All I need to do in order to inherit from this is to remove the rendering (which i dont need anyway  as this is going to be a base type) and it should be good to go.  Those three pieces of code can be installed into your SharePoint environment and should help you get started to understanding custom field types.</p>
<p><span style="color: #800000;"><em>Don't forget to add the SafeControl entry, Adjust your trust level and move the .dll into your bin folder as well.</em></span></p>
<p><a href="/files/FieldType.zip">Download the code here</a></p>
<p>Resources I found useful:</p>
<ul>
<li>I did work through the example in <a href="http://www.andrewconnell.com/blog/">Andrew Connell</a>'s book (Professional SP 2007 WCM development) and that worked fine and he actually does a great job of explaining most of whats going on here.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/ms446361.aspx">MSDN</a>...where else</li>
<li><a href="http://www.chaholl.com/archive/2009/07/18/custom-fieldtypes-ndash-part-1.aspx">Charlie Holland </a>has a pretty good article series on this stuff but unfortunately I couldn't get it working.  I also wanted a simple bare bones implementation.</li>
</ul>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 2968px; width: 1px; height: 1px;">Sometimes when I am working with SharePoint I really feel like I got it.  You now that feeling you get when you know exactly what you are doing and have no problems implementing it...then there are times when I really just want to smash something.  And it really doesn't matter what...it could be anything!  This is the feeling I got in the last couple of days working with custom field types in SharePoint.  It seems there are lots of blogs and articles out there talking about how to do it, but from what I could see (and to be fair I did not read them all so if you know of some good posts that would be great) it felt like most of the time I was only getting half the story.  I just finished putting together a very basic Custom Field Type that will display a multiline textbox in edit/new mode and simply display the value in a literal control in display mode.  In the grand scheme of things I am building a base XML Field Type to use for a series of additional Custom field types so essentially I want to store a string (yes I could use the XmlDocument)...but alas thats a story for another day.</p>
<p>So a bit of a primer, or rehash so I don't forget, when developing custom field types there are many pieces to it....not all of them required.  Here is a lit of the items and what they do:</p>
<p>* Field Type class - this class acts as the controller and hooks the rendering control, field type xml definition and the value class together.  It also provides validation at the field level via the GetValidatedString() method of the SPField object<br />
* Field Control class - this is the rendering engine and gives you the power to display your control in any manner you wish as well as perform validation on the data being saved via the UI.<br />
* Field Value class - allows for custom logic to be put around mutiple column fields and different data structures that will be storing data for your field type.<br />
* .ascx file in CONTROLTEMPALTES directory - this supplies rendering markup via a usercontrol and hooks into the field control class<br />
* fldtypes_* - this file that gets installed into the 12/TEMPLATES/XML directory and contrains the information that SharePoint requires in order to load your field type into the system.  Such as the assembly to use, the parent type and other information.  This is required if the type is going to be applied to content types and become a field on a list.</p>
<p>So for my requirements I only needed the field type, the field control and the fldtypes_xxx.xml file.  Lets look at the field type class I built</p>
<pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlFieldType : SPField
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">public</span> XmlFieldType<span style="color: #000000;">&#40;</span>SPFieldCollection fields, <span style="color: #FF0000;">string</span> fieldName<span style="color: #000000;">&#41;</span> : <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>fields, fieldName<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> XmlFieldType<span style="color: #000000;">&#40;</span>SPFieldCollection fields, <span style="color: #FF0000;">string</span> fieldName, <span style="color: #FF0000;">string</span> displayName<span style="color: #000000;">&#41;</span> : <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>fields, fieldName, displayName<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> BaseFieldControl FieldRenderingControl
<span style="color: #000000;">&#123;</span>
get
<span style="color: #000000;">&#123;</span>
BaseFieldControl fldControl = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlFieldControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
fldControl.<span style="color: #0000FF;">FieldName</span> = InternalName;
<span style="color: #0600FF;">return</span> fldControl;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetFieldValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> value<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;
<span style="color: #0600FF;">return</span> value;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> Type FieldValueType
<span style="color: #000000;">&#123;</span>
get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> DefaultValue
<span style="color: #000000;">&#123;</span>
get
<span style="color: #000000;">&#123;</span>
XmlDocument doc = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
doc.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SchemaXml</span><span style="color: #000000;">&#41;</span>;
XmlNode nodeInFieldSchema = doc.<span style="color: #0000FF;">SelectSingleNode</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Field/Default&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>nodeInFieldSchema != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">return</span> nodeInFieldSchema.<span style="color: #0000FF;">InnerXml</span>;
<span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;
<span style="color: #000000;">&#125;</span>
set <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">DefaultValue</span> = value; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> GetValidatedString<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> value<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">GetValidatedString</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value == <span style="color: #0600FF;">null</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><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Required</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPFieldValidationException<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Invalid value for required field.&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">else</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">return</span> value.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre>
<p>Lets review</p>
<p>* The two constructors are required<br />
* FieldRenderingControl() method tells the field type which control to use<br />
* GetFieldValue() method returns the value of the field....if we were using a custom type you would cast that to the appropriate type here<br />
* FieldValueType() - get the type of the Value class<br />
* DefautValue() - this gets the default value from the field if one is defined<br />
* GetValidatedString() - perform field level validation</p>
<p>The Field Control Class is fairly simple as well:</p>
<pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlFieldControl : BaseFieldControl
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">protected</span> Literal _values;
<span style="color: #0600FF;">protected</span> TextBox _editor;
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnInit<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnInit</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span>;
EnsureChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> CreateChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Field</span> == <span style="color: #0600FF;">null</span> || <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ControlMode</span> == SPControlMode.<span style="color: #0000FF;">Invalid</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">return</span>;
&nbsp;
Controls.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">CreateChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
_editor = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TextBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
_editor.<span style="color: #0000FF;">TextMode</span> = TextBoxMode.<span style="color: #0000FF;">MultiLine</span>;
_editor.<span style="color: #0000FF;">Height</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Unit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">300</span><span style="color: #000000;">&#41;</span>;
_editor.<span style="color: #0000FF;">Width</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Unit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #000000;">&#41;</span>;
Controls.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_editor<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ControlMode == SPControlMode.<span style="color: #0000FF;">Display</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
_values = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Literal<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
_values.<span style="color: #0000FF;">Text</span> = Convert.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span>ItemFieldValue<span style="color: #000000;">&#41;</span>;
Controls.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_values<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Render<span style="color: #000000;">&#40;</span>HtmlTextWriter output<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
EnsureChildControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ControlMode == SPControlMode.<span style="color: #0000FF;">Display</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>_values != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
_values.<span style="color: #0000FF;">RenderControl</span><span style="color: #000000;">&#40;</span>output<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">else</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_editor != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
_editor.<span style="color: #0000FF;">RenderControl</span><span style="color: #000000;">&#40;</span>output<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">string</span> Text
<span style="color: #000000;">&#123;</span>
get
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">EnsureChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>._editor == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>._editor.<span style="color: #0000FF;">Text</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
set
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">EnsureChildControls</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>._editor != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">this</span>._editor.<span style="color: #0000FF;">Text</span> = value;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> Value
<span style="color: #000000;">&#123;</span>
get
<span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Text</span>; <span style="color: #000000;">&#125;</span>
set
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Field</span> != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Text</span> = <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Field</span>.<span style="color: #0000FF;">GetFieldValueForEdit</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span>;
<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Value</span> = <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>value;
<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">UpdateFieldValueInItem</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre>
<p>sdfsdf</p>
<pre class="xml">    XmlFieldType
&nbsp;
    Note
    ntext
    Xml Field
    Xml controls
    TRUE
    TRUE
    FALSE
    FALSE
    FALSE
    Website.CustomFields.XmlFieldType, Website.CustomFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=346fcdd567259ee6</pre>
<p>I did work through the example in Andrew Connell's book (Professional SP 2007 WCM development) and that worked fine and he actually does a great job of explaining most of whats going on here.</p>
<p>http://msdn.microsoft.com/en-us/library/ms446361.aspx</p></div>

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

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/controls' rel='tag' target='_self'>controls</a>, <a class='technorati-link' href='http://technorati.com/tag/custom+field+type' rel='tag' target='_self'>custom field type</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/SharePoint' rel='tag' target='_self'>SharePoint</a>, <a class='technorati-link' href='http://technorati.com/tag/xml' rel='tag' target='_self'>xml</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2009/08/12/custom-field-types-seriously/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>SPException: SharePoint cannot find the user &#8211; SharePoint SPWeb.EnsureUser() and a Custom MembershipProvider</title>
		<link>http://www.calvinirwin.net/2009/08/05/sharepoint-spweb-ensureuser-and-thecustom-membership-provider/</link>
		<comments>http://www.calvinirwin.net/2009/08/05/sharepoint-spweb-ensureuser-and-thecustom-membership-provider/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 22:22:28 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[MembershipProvider]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=90</guid>
		<description><![CDATA[Its seems that SharePoint has a knack for surprising me with seriously strange errors sometimes.  Usually at the core of the error there is a sound explanation as to why this is happening but nonetheless I am not pleased with the obscurity of them sometimes.  Case in point, while working on a Console [...]]]></description>
			<content:encoded><![CDATA[<p>Its seems that SharePoint has a knack for surprising me with seriously strange errors sometimes.  Usually at the core of the error there is a sound explanation as to why this is happening but nonetheless I am not pleased with the obscurity of them sometimes.  Case in point, while working on a Console application that would load FBA (Forms Based Authentication) users into a sharepoint sie collection and then create a site for each one (granting permissions and so on) I came across a very strange error - <strong>SPException: SharePoint cannot find the user.</strong></p>
<p>This kind of surprised me at first and I was wondering if somehow my <strong>CustomMembershipProvider</strong> setup stopped working on my portal site, but that was fine. So after hammering away at it for a while I came across a <a href="http://blog.mastykarz.nl/inconvenient-programmatically-sharepoint-users-spweb-ensureuser/">great article written by Waldek Mastykarz</a> on this exact problem.  After reading through his investigation it makes sense why it doesn't work in a console application but it works under the context of a SharePoint website. Essentially the web application has access to a context object and this has access to the providers node in your web.config file,  BUT the console application does not have this information available.  So when the SharePoint assemblies attempt to access this information they cannot because it does not exist.</p>
<p>The way around this is to create an HttpContext in your application before attempting to run <code>SPWeb.EnsureUser("blah")</code> like so:</p>
<pre>
<div class="code">
if (HttpContext.Current == null)
{
     HttpRequest request = new HttpRequest("", web.Url, "");
     HttpContext.Current = new HttpContext(request,
     new HttpResponse(new StringWriter()));
     HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
}</div>
</pre>
<p>Then add the system.web/membership/providers node to your app.config file (which you may have to create in the project)..  Mine looks like so:</p>
<pre>
<div class="code">&lt;system.web&gt;
     &lt;membership&gt;
          &lt;providers&gt;
          &lt;add name="CustomSqlProvider" applicationName="/Portal"
            connectionStringName="sqlData"
            type="CustomProviders.CustomSqlMembershipProvider,
            CustomProviders, Version=1.0.0.0, Culture=neutral,
            PublicKeyToken=91a4fcd60b73a0e8" /&gt;
          &lt;/providers&gt;
     &lt;/membership&gt;
&lt;/system.web&gt;
&lt;connectionStrings&gt;
     &lt;add name="sqlData" connectionString="Data Source=sqlpd;
       Initial Catalog=PUsers; Integrated Security=True;
       MultipleActiveResultSets=True "
       providerName="System.Data.SqlClient" /&gt;
&lt;/connectionStrings&gt;</div>
</pre>
<p>After adding this information and running a few tests it started to work.  In fact it worked really well on my dev box...the only problem was it was a little intermittent on my production machine.  Which is funny because the provider on the site itself works just fine all the time but in order to get my console application working (the EnsureUser() portion) the site would require an IIS reset.  This is the one MAJOR stumbling block that I have yet to overcome...the only saving grace I have is that I can import the users manually into the system first using the UI and then run my console application that will load the sites and grant permissions to sites based on an external configuration file.</p>
<p>Many thanks to <a href="http://blog.mastykarz.nl"><cite>Waldek Mastykarz</cite></a> for all his help on this one so far...I know I will be coming back to this one in the near future but my head hurts a little and I need a beer.</p>
<p>Canadian SharePint event anyone?</p>

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

<p class='technorati-tags'>Technorati Tags: <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/IIS' rel='tag' target='_self'>IIS</a>, <a class='technorati-link' href='http://technorati.com/tag/MembershipProvider' rel='tag' target='_self'>MembershipProvider</a>, <a class='technorati-link' href='http://technorati.com/tag/Security' rel='tag' target='_self'>Security</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/08/05/sharepoint-spweb-ensureuser-and-thecustom-membership-provider/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SharePoint Permissions</title>
		<link>http://www.calvinirwin.net/2009/07/29/sharepoint-permissions/</link>
		<comments>http://www.calvinirwin.net/2009/07/29/sharepoint-permissions/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 20:00:32 +0000</pubDate>
		<dc:creator>Calvin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint Designer]]></category>
		<category><![CDATA[web parts]]></category>

		<guid isPermaLink="false">http://www.calvinirwin.net/?p=65</guid>
		<description><![CDATA[Inside the Microsoft.SharePoint namespace there is the SPBasePermissions enumeration.  It specifies the built-in permissions available in Windows SharePoint Services / MOSS 2007.  This was taken from the MSDN site, but I am posting it here because sometimes they move content around and I can't find it...ha ha you can't fool me Microsoft!!!
Using the enumerations [...]]]></description>
			<content:encoded><![CDATA[<p>Inside the Microsoft.SharePoint namespace there is the SPBasePermissions enumeration.  It specifies the built-in permissions available in Windows SharePoint Services / MOSS 2007.  This was taken from the MSDN site, but I am posting it here because sometimes they move content around and I can't find it...ha ha you can't fool me Microsoft!!!</p>
<p>Using the enumerations below you can wrap some markup on your page in an SPSecurityTrimmedControl like so: </p>
<div style="padding-left:15px; padding-top: 10px;  padding-bottom: 10px; font-family: courier new; color: #000099;">
&lt;Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="CreateGroups"&gt;</div>
<p>and only allow users with the defined permission to see the contents of the security trimming control.</p>
<table border="1">
<tbody>
<tr style="background-color:#3366CC;">
<th style="color: white;">Member name</th>
<th style="color: white;">Description</th>
</tr>
<tr>
<td><strong>AddAndCustomizePages</strong></td>
<td>Add, change, or delete HTML pages or Web Part Pages, and edit the Web site using a Windows SharePoint Services–compatible editor.</td>
</tr>
<tr>
<td><strong>AddDelPrivateWebParts</strong></td>
<td>Add or remove personal Web Parts on a Web Part Page.</td>
</tr>
<tr>
<td><strong>AddListItems</strong></td>
<td>Add items to lists, add documents to document libraries, and add Web discussion comments.</td>
</tr>
<tr>
<td><strong>ApplyStyleSheets</strong></td>
<td>Apply a style sheet (.css file) to the Web site.</td>
</tr>
<tr>
<td><strong>ApplyThemeAndBorder</strong></td>
<td>Apply a theme or borders to the entire Web site.</td>
</tr>
<tr>
<td><strong>ApproveItems</strong></td>
<td>Approve a minor version of a list item or document.</td>
</tr>
<tr>
<td><strong>BrowseDirectories</strong></td>
<td>Enumerate files and folders in a Web site using Microsoft Office SharePoint Designer 2007 and WebDAV interfaces.</td>
</tr>
<tr>
<td><strong>BrowseUserInfo</strong></td>
<td>View information about users of the Web site.</td>
</tr>
<tr>
<td><strong>CancelCheckout</strong></td>
<td>Discard or check in a document which is checked out to another user.</td>
</tr>
<tr>
<td><strong>CreateAlerts</strong></td>
<td>Create e-mail alerts.</td>
</tr>
<tr>
<td><strong>CreateGroups</strong></td>
<td>Create a group of users that can be used anywhere within the site collection.</td>
</tr>
<tr>
<td><strong>CreateSSCSite</strong></td>
<td>Create a Web site using Self-Service Site Creation.</td>
</tr>
<tr>
<td><strong>DeleteListItems</strong></td>
<td>Delete items from a list, documents from a document library, and Web discussion comments in documents.</td>
</tr>
<tr>
<td><strong>DeleteVersions</strong></td>
<td>Delete past versions of a list item or document.</td>
</tr>
<tr>
<td><strong>EditListItems</strong></td>
<td>Edit items in lists, edit documents in document libraries, edit Web discussion comments in documents, and customize Web Part Pages in document libraries.</td>
</tr>
<tr>
<td><strong>EditMyUserInfo</strong></td>
<td>Allows a user to change his or her user information, such as adding a picture.</td>
</tr>
<tr>
<td><strong>EmptyMask</strong></td>
<td>Has no permissions on the Web site. Not available through the user interface.</td>
</tr>
<tr>
<td><strong>EnumeratePermissions</strong></td>
<td>Enumerate permissions on the Web site, list, folder, document, or list item.</td>
</tr>
<tr>
<td><strong>FullMask</strong></td>
<td>Has all permissions on the Web site. Not available through the user interface.</td>
</tr>
<tr>
<td><strong>ManageAlerts</strong></td>
<td>Manage alerts for all users of the Web site.</td>
</tr>
<tr>
<td><strong>ManageLists</strong></td>
<td>Create and delete lists, add or remove columns in a list, and add or remove public views of a list.</td>
</tr>
<tr>
<td><strong>ManagePermissions</strong></td>
<td>Create and change permission levels on the Web site and assign permissions to users and groups.</td>
</tr>
<tr>
<td><strong>ManagePersonalViews</strong></td>
<td>Create, change, and delete personal views of lists.</td>
</tr>
<tr>
<td><strong>ManageSubwebs</strong></td>
<td>Create subsites such as team sites, Meeting Workspace sites, and Document Workspace sites.</td>
</tr>
<tr>
<td><strong>ManageWeb</strong></td>
<td>Grant the ability to perform all administration tasks for the Web site as well as manage content. Activate, deactivate, or edit properties of Web site scoped Features through the object model or through the user interface (UI). When granted on the root Web site of a site collection, activate, deactivate, or edit properties of site collection scoped Features through the object model. To browse to the <strong>Site Collection Features</strong> page and activate or deactivate site collection scoped Features through the UI, you must be a site collection administrator.</td>
</tr>
<tr>
<td><strong>Open</strong></td>
<td>Allow users to open a Web site, list, or folder to access items inside that container.</td>
</tr>
<tr>
<td><strong>OpenItems</strong></td>
<td>View the source of documents with server-side file handlers.</td>
</tr>
<tr>
<td><strong>UpdatePersonalWebParts</strong></td>
<td>Update Web Parts to display personalized information.</td>
</tr>
<tr>
<td><strong>UseClientIntegration</strong></td>
<td>Use features that launch client applications; otherwise, users must work on documents locally and upload changes.</td>
</tr>
<tr>
<td><strong>UseRemoteAPIs</strong></td>
<td>Use SOAP, WebDAV, or Microsoft Office SharePoint Designer 2007 interfaces to access the Web site.</td>
</tr>
<tr>
<td><strong>ViewFormPages</strong></td>
<td>View forms, views, and application pages, and enumerate lists.</td>
</tr>
<tr>
<td><strong>ViewListItems</strong></td>
<td>View items in lists, documents in document libraries, and view Web discussion comments.</td>
</tr>
<tr>
<td><strong>ViewPages</strong></td>
<td>View pages in a Web site.</td>
</tr>
<tr>
<td><strong>ViewUsageData</strong></td>
<td>View reports on Web site usage.</td>
</tr>
<tr>
<td><strong>ViewVersions</strong></td>
<td>View past versions of a list item or document.</td>
</tr>
</tbody>
</table>

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

<p class='technorati-tags'>Technorati Tags: <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/SharePoint' rel='tag' target='_self'>SharePoint</a>, <a class='technorati-link' href='http://technorati.com/tag/SharePoint+Designer' rel='tag' target='_self'>SharePoint Designer</a>, <a class='technorati-link' href='http://technorati.com/tag/web+parts' rel='tag' target='_self'>web parts</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.calvinirwin.net/2009/07/29/sharepoint-permissions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

