<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Second Life of a Hungarian SharePoint Geek</title>
	<atom:link href="http://pholpar.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pholpar.wordpress.com</link>
	<description>If your sword is too short, take one step forward</description>
	<lastBuildDate>Sat, 21 Jan 2012 12:41:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pholpar.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/5ce925449c0b4e5877ba46ccc93f3f55?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Second Life of a Hungarian SharePoint Geek</title>
		<link>http://pholpar.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pholpar.wordpress.com/osd.xml" title="Second Life of a Hungarian SharePoint Geek" />
	<atom:link rel='hub' href='http://pholpar.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Implementing double click in Silverlight</title>
		<link>http://pholpar.wordpress.com/2012/01/08/implementing-double-click-in-silverlight/</link>
		<comments>http://pholpar.wordpress.com/2012/01/08/implementing-double-click-in-silverlight/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 22:01:32 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/?p=900</guid>
		<description><![CDATA[Recently I faced with the problem, that double click is not supported by Silverlight 4. On the web I found several solutions how to implement it for individual controls (regarding Silverlight 5, read this), however I wished a more general solution. Inspired by this default button example (another missing feature in SL 4), I’ve implemented [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=900&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I faced with the problem, that double click is not supported by <em>Silverlight 4</em>. On the web I found several solutions how to implement it <a href="http://www.michaelsnow.com/2010/05/10/silverlight-tip-of-the-day-17-double-click/">for individual controls</a> (regarding <em>Silverlight 5</em>, read <a href="http://10rem.net/blog/2011/04/13/silverlight-5-supporting-double-and-even-triple-click-for-the-mouse">this</a>), however I wished a more general solution.</p>
<p>Inspired by <a href="http://stackoverflow.com/questions/2683891/silverlight-4-default-button-service">this default button example</a> (another missing feature in <em>SL 4</em>), I’ve implemented the following proof of concept (<em>PoC</em>) solution. I’ve created an attached property (called <em>DoubleClick</em>) where you can configure a <em>RoutedEventHandler</em> for your controls. We will use&#160; this property to return our <em>RoutedEventHandler</em> instance that should handle double click events.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d920d152-29a2-40cb-a2ec-1852453c52b1" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:400px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Windows;</li>
<li><span style="color:#0000ff;">using</span> System.Windows.Controls;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Collections.Generic;</li>
<li><span style="color:#0000ff;">using</span> System.Linq;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">namespace</span> DblClickTest</li>
<li style="background:#f3f3f3;">{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">DoubleClickService</span></li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// time limit between mouse up / click events to be handled as a double click</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#0000ff;">int</span> TimeLimit = 1000;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// list to track earlier mouse up / click events and related controls + handlers</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">UIElement</span>, <span style="color:#2b91af;">DateTime</span>, <span style="color:#2b91af;">RoutedEventHandler</span>&gt;&gt; dict = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">UIElement</span>, <span style="color:#2b91af;">DateTime</span>, <span style="color:#2b91af;">RoutedEventHandler</span>&gt;&gt;();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">DependencyProperty</span> DoubleClickProperty = <span style="color:#2b91af;">DependencyProperty</span>.RegisterAttached(<span style="color:#a31515;">&quot;DoubleClick&quot;</span>, <span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">RoutedEventHandler</span>), <span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">DoubleClickService</span>), <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">PropertyMetadata</span>(<span style="color:#0000ff;">null</span>, DoubleClickChanged));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> DoubleClickChanged(<span style="color:#2b91af;">DependencyObject</span> d, <span style="color:#2b91af;">DependencyPropertyChangedEventArgs</span> e)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">UIElement</span> uiElement = d <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">UIElement</span>;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">RoutedEventHandler</span> clickFunc = e.NewValue <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">RoutedEventHandler</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (uiElement != <span style="color:#0000ff;">null</span> &amp;&amp; clickFunc != <span style="color:#0000ff;">null</span>)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Button</span> button = uiElement <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">Button</span>;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (button != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;button.Click += (sender, arg) =&gt; HandleClick(sender, arg, clickFunc);&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">else</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;uiElement.MouseLeftButtonUp += (sender, arg) =&gt; HandleClick(sender, arg, clickFunc);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> HandleClick(<span style="color:#0000ff;">object</span> sender, <span style="color:#2b91af;">RoutedEventArgs</span> e, <span style="color:#2b91af;">RoutedEventHandler</span> handler)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">UIElement</span> uie = sender <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">UIElement</span>;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// just do the cleanup: remove old entries&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dict = dict.Where(p =&gt; (p.Item2 &gt; <span style="color:#2b91af;">DateTime</span>.Now.AddMilliseconds(-1 * TimeLimit))).ToList();</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">UIElement</span>, <span style="color:#2b91af;">DateTime</span>, <span style="color:#2b91af;">RoutedEventHandler</span>&gt; item = dict.FirstOrDefault(p =&gt; (p.Item1 == sender));</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (item == <span style="color:#0000ff;">null</span>)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dict.Add(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Tuple</span>&lt;<span style="color:#2b91af;">UIElement</span>, <span style="color:#2b91af;">DateTime</span>, <span style="color:#2b91af;">RoutedEventHandler</span>&gt;(uie, <span style="color:#2b91af;">DateTime</span>.Now, handler));</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">else</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;item.Item3.Invoke(sender, e);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">RoutedEventHandler</span> GetDoubleClick(<span style="color:#2b91af;">UIElement</span> obj) </li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> (<span style="color:#2b91af;">RoutedEventHandler</span>)obj.GetValue(DoubleClickProperty);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> SetDoubleClick(<span style="color:#2b91af;">DependencyObject</span> obj, <span style="color:#2b91af;">RoutedEventHandler</span> clickFunc)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;obj.SetValue(DoubleClickProperty, clickFunc); </li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>My static class is built around a generic <em>List </em>of <em>Tuple </em>objects, that is used to track <em>UIElement</em> instances and related click events and handlers.</p>
<p>In the <em>DoubleClickChanged</em> method I register my handler method. If the control is a <em>Button</em>, then I attach to the <em>Click</em> event, otherwise the <em>MouseLeftButtonUp</em> event is used. That is needed because the <em>Button</em> control handles the <em>MouseLeftButtonDown</em> /<em>MouseLeftButtonUp</em>&#160; events internally and set these methods as handled, that means it does not forward these events forward.</p>
<p>The <em>HandleClick </em>handler method first remove outdated entries from the list (you can set the value of <em>TimeLimit</em>&#160; to match your needs), then if the source control is not found in the list, it is registered with the current time and the specified event handler, otherwise the event is considered a double click, and the specified <em>RoutedEventHandler</em> is invoked, passing the original <em>sender</em> and <em>RoutedEventArgs</em> parameters.</p>
<p><strong>Note</strong>: The referenced post above uses the <em>MouseLeftButtonDown </em>event. Based on my experience this event is not triggered for example on <em>ListBox</em> control, so I switched to the <em>MouseLeftButtonUp</em> event, that is a bit different from the standard <em>Windows </em>double click behavior.</p>
<p>The <em>MainPage.xaml </em>for the test application looks like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:293d3746-a54f-44d0-b9fa-a4800f8f936e" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">UserControl</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Class</span><span style="color:#0000ff;">=&quot;DblClickTest.MainPage&quot;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;<span style="color:#ff0000;"> xmlns</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></li>
<li>&#160;&#160;&#160;<span style="color:#ff0000;"> xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">x</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;<span style="color:#ff0000;"> xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">d</span><span style="color:#0000ff;">=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span></li>
<li>&#160;&#160;&#160;<span style="color:#ff0000;"> xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">mc</span><span style="color:#0000ff;">=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;<span style="color:#ff0000;"> xmlns</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">ap</span><span style="color:#0000ff;">=&quot;clr-namespace:DblClickTest&quot;</span></li>
<li>&#160;&#160;&#160;<span style="color:#ff0000;"> mc</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Ignorable</span><span style="color:#0000ff;">=&quot;d&quot;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;<span style="color:#ff0000;"> d</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">DesignHeight</span><span style="color:#0000ff;">=&quot;300&quot;</span><span style="color:#ff0000;"> d</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">DesignWidth</span><span style="color:#0000ff;">=&quot;400&quot;</span></li>
<li>&#160;&#160;&#160;<span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;Top&quot;&gt;</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">StackPanel</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&quot;Double click me!&quot;</span> </li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000;"> ap</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">DoubleClickService.DoubleClick</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=Top,</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=DblClickHandler}&quot;/&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBox</span><span style="color:#ff0000;"> Name</span><span style="color:#0000ff;">=&quot;Output&quot; /&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&quot;MyButton&quot;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&quot;Or me!&quot;</span> </li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#ff0000;"> ap</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">DoubleClickService.DoubleClick</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=Top,</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=DblClickHandler}&quot;/&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListBox</span><span style="color:#ff0000;"> ap</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">DoubleClickService.DoubleClick</span><span style="color:#0000ff;">=&quot;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=Top,</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">=DblClickHandler}&quot;&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListBoxItem</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&quot;Item 1&quot;&gt;&lt;/</span><span style="color:#a31515;">ListBoxItem</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListBoxItem</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&quot;Item 2&quot;&gt;&lt;/</span><span style="color:#a31515;">ListBoxItem</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ListBox</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#a31515;"></span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">StackPanel</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">UserControl</span><span style="color:#0000ff;">&gt;</span></li>
</ol></div>
</p></div>
</p></div>
<p>The related code-behind:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:55a790cb-c510-4606-85ba-e4e3ad086a17" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Linq;</li>
<li><span style="color:#0000ff;">using</span> System.Net;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Windows;</li>
<li><span style="color:#0000ff;">using</span> System.Windows.Controls;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">using</span> System.ComponentModel;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">namespace</span> DblClickTest</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">MainPage</span> : <span style="color:#2b91af;">UserControl</span> </li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> MainPage()</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;InitializeComponent();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#2b91af;">RoutedEventHandler</span> DblClickHandler</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">get</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">RoutedEventHandler</span>(OnDblClick);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> OnDblClick(<span style="color:#0000ff;">object</span> sender, <span style="color:#2b91af;">RoutedEventArgs</span> e)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Output.Text = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;Double click on {0}&quot;</span>, sender);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>The sample application in action, after double-clicking on the second list box item:</p>
<p><a href="http://pholpar.files.wordpress.com/2012/01/image.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2012/01/image_thumb.png?w=393&#038;h=116" width="393" height="116" /></a> </p>
</p>
</p>
<p><strong>Note</strong>: In a real application you might have to add synchronization code for the static list object access and / or exception handling.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/900/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=900&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2012/01/08/implementing-double-click-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2012/01/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Synchronizing SharePoint tasks with Outlook including categories</title>
		<link>http://pholpar.wordpress.com/2011/12/30/synchronizing-sharepoint-tasks-with-outlook-including-categories/</link>
		<comments>http://pholpar.wordpress.com/2011/12/30/synchronizing-sharepoint-tasks-with-outlook-including-categories/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 23:30:14 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[Fiddler]]></category>
		<category><![CDATA[Outlook]]></category>
		<category><![CDATA[SP 2010]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/?p=895</guid>
		<description><![CDATA[Recently one of our customers requested a SharePoint task list that is synchronized with Outlook. That is usually quite easy, but in this case we should have provided a way to tag the tasks and group them based on this multi-value property on the Outlook side, similarly to the default Categories field feature. As you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=895&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently one of our customers requested a <em>SharePoint</em> task list that is synchronized with <em>Outlook</em>. That is usually quite easy, but in this case we should have provided a way to tag the tasks and group them based on this multi-value property on the <em>Outlook</em> side, similarly to the default <em>Categories</em> field feature.</p>
<p>As you may know, only a limited set of task properties are synchronized between <em>SharePoint</em> and <em>Outlook</em>, but details –&#160; like what exactly these properties are and how the synchronization process works – are not very well documented.</p>
<p>To investigate the behavior I’ve created a test list called <em>OLTasks </em>based on the <em>Tasks </em>list template in SharePoint.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb.png?w=1018&#038;h=638" width="1018" height="638" /></a> </p>
<p>Then connected the list to <em>Outlook</em>.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image1.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb1.png?w=992&#038;h=224" width="992" height="224" /></a> </p>
<p>I’ve started <em>Fiddler</em> and found that the <em>Lists</em> web service is used for synchronization. First, the <em>GetList </em>method is called.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image2.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb2.png?w=1042&#038;h=892" width="1042" height="892" /></a> </p>
<p>Next, a <em>GetListItemChangesSinceToken</em> request is sent. This method is suggested by Microsoft for item synchronization. As one can read on <a href="http://msdn.microsoft.com/en-us/library/cc264031.aspx">MSDN</a>:</p>
<p>“The most efficient way to synchronize third-party clients with <em>Microsoft SharePoint Foundation 2010</em> is to download only those items that have changed since the last synchronization occurred. You can do this in <em>SharePoint Foundation 2010</em> by calling the <em>GetListItemChangesSinceToken</em> Web method.”</p>
<p>The first request contains no token, and the response includes the entire list schema (not shown below). Since our task list contains no item, the <em>ItemCount </em>is zero.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image3.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb3.png?w=940&#038;h=663" width="940" height="663" /></a> </p>
<p>Note, that the request above contains the <em>Categories </em>property, however, our <em>Task </em>content type has no such field.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image4.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb4.png?w=884&#038;h=545" width="884" height="545" /></a> </p>
<p>I’ve created a new task item in the list to see how it is downloaded to <em>Outlook</em>.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image5.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb5.png?w=628&#038;h=727" width="628" height="727" /></a> </p>
</p>
</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image6.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb6.png?w=834&#038;h=85" width="834" height="85" /></a> </p>
<p>Again, the <em>GetListItemChangesSinceToken</em> method was called, in this case there was a token in the request (<em>changeToken </em>is not shown below due to lack of space) and the value of <em>ItemCount </em>was 1 in response.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image7.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb7.png?w=1046&#038;h=424" width="1046" height="424" /></a>&#160;</p>
<p>After synchronization, the item appeared in <em>Outlook</em>:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image8.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb8.png?w=1005&#038;h=43" width="1005" height="43" /></a></p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image9.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb9.png?w=869&#038;h=360" width="869" height="360" /></a> </p>
<p> I’ve modified the task description, and found that on the next synchronization the <em>UpdateListItems</em> method was called to upload changes to <em>SharePoint</em>.</p>
</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image10.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb10.png?w=942&#038;h=490" width="942" height="490" /></a> </p>
<p>Before and after calling the <em>UpdateListItems</em> method the <em>GetListItemChangesSinceToken</em> method was called to detect possible conflicts and synchronize back changes from server.</p>
<p>To provide the <em>Categories</em> field for our tasks, I’ve added the existing site column with the same name to the list.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image11.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb11.png?w=569&#038;h=424" width="569" height="424" /></a> </p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image12.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb12.png?w=497&#038;h=213" width="497" height="213" /></a>&#160;</p>
<p>Set a test value for the field at the existing task, </p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image13.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb13.png?w=656&#038;h=489" width="656" height="489" /></a> </p>
<p>and created a new one with other test values. Note, that I’ve specified two values in this case, separated by a comma.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image14.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb14.png?w=651&#038;h=487" width="651" height="487" /></a> </p>
<p>As expected the values are synchronized down to <em>Outlook</em>, as shown in this view, grouped by the <em>Categories </em>field.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image15.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb15.png?w=781&#038;h=217" width="781" height="217" /></a></p>
</p>
</p>
</p>
</p>
<p>When opening <em>Task 2</em>, we found that our category values are not in the <em>Master Category List</em>. </p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image16.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb16.png?w=813&#038;h=646" width="813" height="646" /></a> </p>
<p>We can resolve it – and add some color codes as well – by clicking <em>New…</em> on the dialog box.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image17.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb17.png?w=289&#038;h=213" width="289" height="213" /></a></p>
<p>After this configuration, <em>Outlook</em> handles our categories as known ones. Next, I’ve set a new category for the task in <em>Outlook</em>.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image18.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb18.png?w=809&#038;h=652" width="809" height="652" /></a></p>
<p>The <em>Outlook </em>view reflects the changes.</p>
</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image19.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb19.png?w=786&#038;h=265" width="786" height="265" /></a> </p>
<p>The <em>UpdateListItems</em> method uploads the changes to <em>SharePoint</em>.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image20.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb20.png?w=942&#038;h=739" width="942" height="739" /></a></p>
<p> And the updated values are displayed in our <em>SharePoint</em> list as well.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/12/image21.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/12/image_thumb21.png?w=988&#038;h=107" width="988" height="107" /></a> </p>
</p>
</p>
</p>
</p>
<p>I hope this quick guide helps you to better understand the default synchronization process, and to utilize similar techniques in your applications.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/895/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/895/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/895/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=895&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/12/30/synchronizing-sharepoint-tasks-with-outlook-including-categories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb11.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb12.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb13.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb14.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb15.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb16.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb17.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb18.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb19.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb20.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/12/image_thumb21.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating custom validation rules in our list field iterators</title>
		<link>http://pholpar.wordpress.com/2011/11/14/creating-custom-validation-rules-in-our-list-field-iterators/</link>
		<comments>http://pholpar.wordpress.com/2011/11/14/creating-custom-validation-rules-in-our-list-field-iterators/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 12:36:32 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[CAML]]></category>
		<category><![CDATA[Custom forms]]></category>
		<category><![CDATA[SP 2010]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/11/14/creating-custom-validation-rules-in-our-list-field-iterators/</guid>
		<description><![CDATA[Last week we saw how to hide specific field controls or how to display them as read-only even on the edit form. In the current post I introduce a simple technique one can apply to create custom validation for SharePoint forms without InfoPath. First, what are the main problems with the out of the box [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=848&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week we <a href="http://pholpar.wordpress.com/2011/11/09/setting-form-field-visibility-and-editability-through-custom-list-field-iterators/">saw</a> how to hide specific field controls or how to display them as read-only even on the edit form. In the current post I introduce a simple technique one can apply to create custom validation for SharePoint forms without InfoPath.</p>
<p>First, what are the main problems with the out of the box SharePoint form validation capability?</p>
<p>As written in the <a href="http://pholpar.wordpress.com/2011/10/16/customizing-sharepoint-forms-via-creating-our-own-listfielditerator/">first post</a> of this series about custom list field iterators, SharePoint 2010 made a large step to the right direction compared to its predecessor, WSS 3.0. One can define field level and form level validation rules using a syntax similar to the one used with calculated columns, based on values of other fields of the current item.</p>
<p>However, you can define only a single validation rule for your form having a single validation error message, and a single validation rule and error message for the fields. Furthermore, there is another serious limitation, namely one can validate only the basic field types (like text, date, numeric) and the same is true to the fields one can refer to in the validation rule.</p>
<p>It means, you are out of luck if you have to work with validation of lookup or user fields, managed metadata or even your custom field types.</p>
<p>In the validation rule you can refer to the fields of the current item, you cannot build validation rules based on field values of other, related items, custom user properties, not to mention accessing other systems, like <em>CRM</em>, 3rd party web services, etc.</p>
<p>Moreover, the validation error messages are static text, one can not built them up dynamically based on field values, for example.</p>
<p>I found that in real world applications customers need usually much more validation power.&#160; All of these inflexibilities listed above lead me to create some kind of custom solution for validation. You can read another approach built on custom field types <a href="http://pholpar.wordpress.com/2010/01/04/cross-field-cross-item-cross-list-or-even-more-complicated-validations-on-sharepoint-forms/">here</a>, but in this post I illustrate the list field iterator based solution.</p>
<p>As you might know the <em>BaseFieldControl</em> class, the base class of all field controls implements the <em>IValidator</em> interface. In practice it means, you can use these field controls similar to standard <em>ASP.NET</em> validator controls.</p>
<p>But before working with them, we first have to get a reference to the field controls. So we extend our <em>BaseListFieldIterator</em> class – introduced in the <a href="http://pholpar.wordpress.com/2011/11/09/setting-form-field-visibility-and-editability-through-custom-list-field-iterators/">previous part</a> – with the <em>GetFieldControlByName</em> method. There might be multiple forms on the page, so just to be sure that the field control is within our list field iterator, we call the <em>GetIteratorByFieldControl</em> method and compare the <em>ClientID</em> values.</p>
<p>The <em>SetValidationError</em> method is to set the validation error for the specified field.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d0ab9010-ae7d-4075-b2e9-2e1d19ee9c0b" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">BaseFieldControl</span> GetFieldControlByName(<span style="color:#2b91af;">String</span> fieldNameToFind)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">foreach</span> (<span style="color:#2b91af;">Control</span> control <span style="color:#0000ff;">in</span> _formContext.FieldControlCollection)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (control <span style="color:#0000ff;">is</span> <span style="color:#2b91af;">BaseFieldControl</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">BaseFieldControl</span> baseField = (<span style="color:#2b91af;">BaseFieldControl</span>)control;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> fieldName = baseField.FieldName;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((fieldName == fieldNameToFind) &amp;&amp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(GetIteratorByFieldControl(baseField).ClientID == ClientID))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> baseField;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">protected</span> Microsoft.SharePoint.WebControls.<span style="color:#2b91af;">ListFieldIterator</span> GetIteratorByFieldControl(<span style="color:#2b91af;">BaseFieldControl</span> fieldControl)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> (Microsoft.SharePoint.WebControls.<span style="color:#2b91af;">ListFieldIterator</span>)fieldControl.Parent.Parent.Parent.Parent.Parent;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> SetValidationError(<span style="color:#2b91af;">String</span> fieldName, <span style="color:#2b91af;">String</span> errorMessage)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">BaseFieldControl</span> fieldControl = GetFieldControlByName(fieldName);</li>
<li>&#160;&#160;&#160;&#160;fieldControl.ErrorMessage = errorMessage;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;fieldControl.IsValid = <span style="color:#0000ff;">false</span>;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>Next we have to add the following code to our custom <em>ListFieldIterator </em>class.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:523f82aa-cd04-4c11-8185-bcbb75ab0996" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnLoad(<span style="color:#2b91af;">EventArgs</span> e)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (Page.IsPostBack)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Page.Validate();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.Validate();</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Validate()</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">base</span>.ControlMode != <span style="color:#2b91af;">SPControlMode</span>.Display)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// here comes the validation logic</span></li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>We will extend the <em>Validate</em> method to fulfill our fictitious business needs.</p>
<p>The validation messages are stored as constants:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e18cf770-a1e2-4194-bb80-845d424f62d0" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">ValidationMessages</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> TitleNotUnique = <span style="color:#a31515;">&quot;Task title should be unique&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> UserBusy = <span style="color:#a31515;">&quot;This user already has another task for this time&quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> NoDateForAssignement = <span style="color:#a31515;">&quot;A user can be assigned only if the start and due dates are specified&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> PredecessorsNotCompleted = <span style="color:#a31515;">&quot;At least one of the predecessors is not yet completed&quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> PredecessorSelfReference = <span style="color:#a31515;">&quot;A task cannot be its own predecessor&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> DueDateEarlierThanStartDate = <span style="color:#a31515;">&quot;Due date should not be earlier than start date &quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> CompletedTaskPercentage = <span style="color:#a31515;">&quot;For a completed task the % complete should be 100&quot;</span>;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>Despite of the constant values used in the sample, it is easy to create dynamic messages as well. For example, you can define a message like <em>&quot;Task title &#8216;{0}&#8217; is not unique&quot;</em>, then substitute the current value when setting the validation error.</p>
<p>The first rule is to allow only unique task titles.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bcecfd5b-ae16-4456-8eb7-bf32d375e6e9" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">TextField</span> titleField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.Title) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">TextField</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">if</span> (titleField != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> title = titleField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">String</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!<span style="color:#2b91af;">String</span>.IsNullOrEmpty(title))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (ItemWithSameTitleExists(title))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.Title, <span style="color:#2b91af;">ValidationMessages</span>.TitleNotUnique);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>We check the uniqueness through a <em>CAML</em> query, and use the <em>GetFieldRefs </em>helper method to build the value of the <em>ViewFields </em>property of the <em>SPQuery </em>instance.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c07205b5-c811-4612-b265-03f75f1b6a52" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">bool</span> ItemWithSameTitleExists(<span style="color:#0000ff;">string</span> title)</li>
<li style="background:#f3f3f3;">{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPQuery</span> query = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPQuery</span>();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;query.ViewFields = GetFieldRefs(<span style="color:#2b91af;">SampleTaskListFields</span>.Title);</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// we should not check the ID in this case, as altering the title is not allowed</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// title will be validated only on new item creation</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// if title would be editable for existing items, then we should check </span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// whether the ID is not the one of the current (edited) item</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// note, that CAML Eq for a Text field type is case insensitive</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;query.Query = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&#039;{0}&#039;/&gt;&lt;Value Type=&#039;Text&#039;&gt;{1}&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.Title, title);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">bool</span> result = List.GetItems(query).Count &gt; 0;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">private</span> <span style="color:#2b91af;">String</span> GetFieldRefs(<span style="color:#0000ff;">params</span> <span style="color:#2b91af;">String</span>[] fieldNames)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> fieldRefs = <span style="color:#2b91af;">String</span>.Concat(fieldNames.ToList().</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ConvertAll(fieldName =&gt; <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;FieldRef Name=&#039;{0}&#039;/&gt;&quot;</span>, fieldName)));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> fieldRefs;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>The next image shows the validator in action.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image7.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb7.png?w=627&#038;h=687" width="627" height="687" /></a></p>
<p>The comparison is case insensitive.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image8.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb8.png?w=318&#038;h=48" width="318" height="48" /></a> </p>
<p>Yes, you are right it is nothing more than a custom implementation for the built-in <em>Enforce unique values </em>feature, but this one does not require indexed columns, and you have the chance to handle issues like leading / trailing spaces. For example, if you enable <em>Enforce unique values</em>, it won’t prohibit users entering &#8216;<em> Sample task 1&#160; </em>&#8216; without any difficulties. If you don’t like this, you can trim the <em>title</em> parameter value and/or use <em>Contains</em> instead of <em>Eq </em>in the <em>CAML </em>query.</p>
<p>After this basic validation, let’s see a more tricky one. This one can be solved using standard field validation, although in this case it is more complicated “thanks” to the read-only <em>Start date </em>field when the task is not in the “<em>Not Started</em>” status.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4b59058b-f483-4407-a8ea-a8b07858dcd6" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">DateTimeField</span> startDateField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.StartDate) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">DateTimeField</span>;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">DateTimeField</span> dueDateField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.DueDate) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">DateTimeField</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#008000;">// if the task is not in &#039;Not Started&#039; status, then we display the start date</span></li>
<li><span style="color:#008000;">// as read-only and value would be null</span></li>
<li style="background:#f3f3f3;"><span style="color:#008000;">// in this case we should get the real value, stored in the item</span></li>
<li><span style="color:#2b91af;">DateTime</span>? startDateValue = (startDateField.ControlMode == <span style="color:#2b91af;">SPControlMode</span>.Display) ? (<span style="color:#2b91af;">DateTime</span>?)Item[<span style="color:#2b91af;">SampleTaskListFields</span>.StartDate] : startDateField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">DateTime</span>?;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">DateTime</span>? dueDateValue = dueDateField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">DateTime</span>?;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">if</span> ((startDateValue.HasValue) &amp;&amp; (dueDateValue.HasValue))</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (dueDateValue.Value &lt; startDateValue.Value)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.DueDate, <span style="color:#2b91af;">ValidationMessages</span>.DueDateEarlierThanStartDate);</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>The illustration of the validation error:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image9.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb9.png?w=663&#038;h=742" width="663" height="742" /></a> </p>
<p>This validation is again a simple one. We require <em>100%</em> for <em>% Complete</em> when <em>Status </em>is <em>Completed</em>.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d0239199-d0f0-4aa8-aaa4-2aadea605de2" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">DropDownChoiceField</span> statusField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.Status) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">DropDownChoiceField</span>;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">NumberField</span> percComplField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.PercentComplete) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">NumberField</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">bool</span> checkForPredecessorStatus = <span style="color:#0000ff;">false</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">if</span> ((statusField != <span style="color:#0000ff;">null</span>) &amp;&amp; (percComplField != <span style="color:#0000ff;">null</span>))</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> statusFieldValue = statusField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">String</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Double</span>? percComplValue = percComplField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">Double</span>?;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (statusFieldValue == <span style="color:#2b91af;">TaskStates</span>.Completed)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;checkForPredecessorStatus = <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((!percComplValue.HasValue) || (percComplValue.Value != 1))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.PercentComplete, <span style="color:#2b91af;">ValidationMessages</span>.CompletedTaskPercentage);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
</p>
<p>The screenshot of the validation:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image10.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb10.png?w=661&#038;h=740" width="661" height="740" /></a> </p>
<p>Before you leave the post unread thinking this technique provides no additional value for validation, let’s see some more advanced example.</p>
<p>Using the standard <em>Tasks </em>list you can set an existing task as its own predecessor. That is not very nice, and in this case I found no simple OOB <em>SharePoint </em>tool to prohibit that.</p>
<p>Using this technique, you can achieve the result as shown below:</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d4696d91-41e1-4a6d-811a-24721c81cd36" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:400px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">MultipleLookupField</span> predecessorsField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.Predecessors) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">MultipleLookupField</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">if</span> (predecessorsField != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFieldLookupValueCollection</span> predecessorsValue = predecessorsField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">SPFieldLookupValueCollection</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (predecessorsValue != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// a task can reference itself only in edit mode</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (ControlMode == <span style="color:#2b91af;">SPControlMode</span>.Edit)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!predecessorsValue.TrueForAll(predecessor =&gt; predecessor.LookupId != ItemId))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.Predecessors, <span style="color:#2b91af;">ValidationMessages</span>.PredecessorSelfReference);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (checkForPredecessorStatus)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!IsAllTasksCompleted(predecessorsValue.ConvertAll(predecessor =&gt; predecessor.LookupId)))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.Status, <span style="color:#2b91af;">ValidationMessages</span>.PredecessorsNotCompleted);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>And in action:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image11.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb11.png?w=661&#038;h=742" width="661" height="742" /></a></p>
<p>The code above contains a check of predecessors when the task is to be set <em>Completed</em>. (The value of <em>checkForPredecessorStatus </em>computed earlier when validating <em>% Complete.</em>) We don’t want to allow that if there is at least on uncompleted predecessor. For the check we use the <em>IsAllTasksCompleted </em>method, passing the task <em>ID</em> of all of the predecessors as parameter.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:91279eb0-52cc-491b-b517-443ea13b8415" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">bool</span> IsAllTasksCompleted(<span style="color:#2b91af;">List</span>&lt;<span style="color:#0000ff;">int</span>&gt; taskIds)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPQuery</span> query = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPQuery</span>();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;query.ViewFields = GetFieldRefs(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.Id,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.Status);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;query.Query = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;Where&gt;&lt;Neq&gt;&lt;FieldRef Name=&#039;{0}&#039;/&gt;&lt;Value Type=&#039;Text&#039;&gt;{1}&lt;/Value&gt;&lt;/Neq&gt;&lt;/Where&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.Status, <span style="color:#2b91af;">TaskStates</span>.Completed);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">bool</span> result = <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPListItemCollection</span> tasksNotCompleted = List.GetItems(query);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">foreach</span> (<span style="color:#2b91af;">SPListItem</span> task <span style="color:#0000ff;">in</span> tasksNotCompleted)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (taskIds.Contains((<span style="color:#0000ff;">int</span>)task[<span style="color:#2b91af;">SampleTaskListFields</span>.Id]))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;result = <span style="color:#0000ff;">false</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">break</span>;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>The result of the validation is shown here:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image12.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb12.png?w=664&#038;h=743" width="664" height="743" /></a> </p>
<p>Next, we want to allow to assign a user to the task if both start and due dates are set.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:554ddfcf-d368-4905-94a6-36d849dc8e21" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">UserField</span> userField = GetFieldControlByName(<span style="color:#2b91af;">SampleTaskListFields</span>.AssignedTo) <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">UserField</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">if</span> (userField != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> userFieldValue = userField.Value <span style="color:#0000ff;">as</span> <span style="color:#2b91af;">String</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!<span style="color:#2b91af;">String</span>.IsNullOrEmpty(userFieldValue))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((startDateValue.HasValue) &amp;&amp; (dueDateValue.HasValue))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (startDateValue.Value &lt;= dueDateValue.Value)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFieldUserValue</span> userValue = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPFieldUserValue</span>(Web, userFieldValue);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">int</span>? taskId = (<span style="color:#0000ff;">base</span>.ControlMode == <span style="color:#2b91af;">SPControlMode</span>.Edit) ? ItemId : (<span style="color:#0000ff;">int</span>?)<span style="color:#0000ff;">null</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((userValue.LookupId != -1) &amp;&amp; (UserIsBusy(userValue.LookupId, taskId, startDateValue.Value, dueDateValue.Value)))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.AssignedTo, <span style="color:#2b91af;">ValidationMessages</span>.UserBusy);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">else</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetValidationError(<span style="color:#2b91af;">SampleTaskListFields</span>.AssignedTo, <span style="color:#2b91af;">ValidationMessages</span>.NoDateForAssignement);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>On the screenshot below, I “forgot” to set the start date, that is not allowed in this case.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image13.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb13.png?w=661&#038;h=741" width="661" height="741" /></a> </p>
<p>Finally, using the method described in <a href="http://pholpar.wordpress.com/2011/11/11/how-to-find-overlapping-items-for-a-date-range/">this post</a>, we allow to assign the user for the task, if (s)he has no other assignment for that time interval. In this case we should ignore the task being edited when doing the validation.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:14ffdc20-91e5-4491-a87f-c2135634d1e5" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">bool</span> UserIsBusy(<span style="color:#0000ff;">int</span> userId, <span style="color:#0000ff;">int</span>? taskId, <span style="color:#2b91af;">DateTime</span> startDate, <span style="color:#2b91af;">DateTime</span> dueDate)</li>
<li style="background:#f3f3f3;">{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPQuery</span> query = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPQuery</span>();</li>
<li>&#160;&#160;&#160;&#160;query.ViewFields = GetFieldRefs(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.Id,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.AssignedTo,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.StartDate,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.DueDate);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// NOTE: you can add filter for Status Neq &#039;Completed&#039; as well if you wish</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// I have not included that for the sake of simplicity</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (taskId.HasValue)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// it is editing an existing task, so we should exclude the task itself</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;query.Query = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;Where&gt;&lt;And&gt;&lt;And&gt;&lt;Neq&gt;&lt;FieldRef Name=&#039;{0}&#039;/&gt;&lt;Value Type=&#039;Integer&#039;&gt;{1}&lt;/Value&gt;&lt;/Neq&gt;&lt;Eq&gt;&lt;FieldRef Name=&#039;{2}&#039; LookupId=&#039;TRUE&#039; /&gt;&lt;Value Type=&#039;Lookup&#039;&gt;{3}&lt;/Value&gt;&lt;/Eq&gt;&lt;/And&gt;{4}&lt;/And&gt;&lt;/Where&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.Id, taskId.Value,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.AssignedTo, userId,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BuildDateRangeOverlapFilter(startDate, dueDate));</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">else</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// it is a new task, we don&#039;t have to check te task ID</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;query.Query = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;Where&gt;&lt;And&gt;&lt;Eq&gt;&lt;FieldRef Name=&#039;{0}&#039; LookupId=&#039;TRUE&#039; /&gt;&lt;Value Type=&#039;Lookup&#039;&gt;{1}&lt;/Value&gt;&lt;/Eq&gt;{2}&lt;/And&gt;&lt;/Where&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SampleTaskListFields</span>.AssignedTo, userId,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BuildDateRangeOverlapFilter(startDate, dueDate));</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">bool</span> result = List.GetItems(query).Count &gt; 0;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">String</span> BuildDateRangeOverlapFilter(<span style="color:#2b91af;">DateTime</span> startDate, <span style="color:#2b91af;">DateTime</span> endDate)</li>
<li>{</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">StringBuilder</span> sb = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;sb.Append(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;And&gt;{0}{1}&lt;/And&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BuildSimpleDateFilter(<span style="color:#2b91af;">SampleTaskListFields</span>.StartDate, endDate, <span style="color:#a31515;">&quot;Leq&quot;</span>),</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BuildSimpleDateFilter(<span style="color:#2b91af;">SampleTaskListFields</span>.DueDate, startDate, <span style="color:#a31515;">&quot;Geq&quot;</span>)));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> sb.ToString();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">String</span> BuildSimpleDateFilter(<span style="color:#2b91af;">String</span> dateFieldName, <span style="color:#2b91af;">DateTime</span> filterDate, <span style="color:#2b91af;">String</span> relation)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> datePattern = <span style="color:#a31515;">&quot;yyyy-MM-ddT00:00:00Z&quot;</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;{0}&gt;&lt;FieldRef Name=&#039;{1}&#039;/&gt;&lt;Value Type=&#039;DateTime&#039;&gt;{2}&lt;/Value&gt;&lt;/{0}&gt;&quot;</span>, relation, dateFieldName, filterDate.ToString(datePattern));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>And that is the outcome of the validation in this case:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image14.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb14.png?w=662&#038;h=743" width="662" height="743" /></a> </p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<p><strong>Note</strong>: Other validation possibilities for <em>Person or Group </em>field type are to check the number of users set (you can allow a single user or multiple ones using the standard tools, but you can not expect exactly three of them), or to check if the specified users are from a specific <em>Active Directory </em>group.</p>
<p>Although the technique illustrated in this post require coding and not so straightforward as specifying a simple formula on the UI, I hope you can use it&#160; effectively if the customer requirements demand something more sophisticated.</p>
<p>You can download the sample application from <a href="http://code.msdn.microsoft.com/SharePoint-list-form-ef7d732b">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/848/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/848/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/848/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/848/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/848/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/848/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/848/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/848/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=848&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/11/14/creating-custom-validation-rules-in-our-list-field-iterators/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb11.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb12.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb13.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb14.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>How to find overlapping items for a date range</title>
		<link>http://pholpar.wordpress.com/2011/11/11/how-to-find-overlapping-items-for-a-date-range/</link>
		<comments>http://pholpar.wordpress.com/2011/11/11/how-to-find-overlapping-items-for-a-date-range/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 12:33:13 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[CAML]]></category>
		<category><![CDATA[SP 2010]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/11/11/how-to-find-overlapping-items-for-a-date-range/</guid>
		<description><![CDATA[It is a common requirement to find list items (like tasks) that have an overlapping duration with a specified data range. Several years ago I wrote a post about the DateRangesOverlap CAML element and about its strange behavior returning items outside of the specified month range. As I suggested there, we can (and should) create [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=831&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It is a <a href="http://stackoverflow.com/questions/3325552/sharepoint-daterangesoverlap-find-overlaping-items-for-a-date-range">common requirement</a> to find list items (like tasks) that have an overlapping duration with a specified data range.</p>
<p>Several years ago I wrote a post about the <a href="http://pholpar.wordpress.com/2008/03/04/using-month-element-in-daterangesoverlap-can-return-items-not-in-the-specified-month/">DateRangesOverlap</a> CAML element and about its strange behavior returning items outside of the specified month range. As I suggested there, we can (and should) create the right query using basic CAML elements.</p>
<p>It is easy to create a complex CAML query to filter items for overlapping date ranges, however, there is a way to create a simple query as well. The simplest way and the best description in the topic is the one I found on <a href="http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap">stackoverflow</a>. It says, the date ranges are overlapping when it is true for both of them, that their start date is no later than the end date of the other date range. That is very clear if you think about that just a bit.</p>
<p>Based on that concept I’ve created two helper methods:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b471c7df-16de-40e7-b60b-17ab21452187" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:400px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">String</span> BuildDateRangeOverlapFilter(<span style="color:#2b91af;">DateTime</span> startDate, <span style="color:#2b91af;">DateTime</span> endDate)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">StringBuilder</span> sb = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;sb.Append(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;Where&gt;&lt;And&gt;{0}{1}&lt;/And&gt;&lt;/Where&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BuildSimpleDateFilter(<span style="color:#a31515;">&quot;StartDate&quot;</span>, endDate, <span style="color:#a31515;">&quot;Leq&quot;</span>),</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BuildSimpleDateFilter(<span style="color:#a31515;">&quot;DueDate&quot;</span>, startDate, <span style="color:#a31515;">&quot;Geq&quot;</span>)));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> sb.ToString();</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">String</span> BuildSimpleDateFilter(<span style="color:#2b91af;">String</span> dateFieldName, <span style="color:#2b91af;">DateTime</span> filterDate, <span style="color:#2b91af;">String</span> relation)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> filter = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;&lt;{0}&gt;&lt;FieldRef Name=&#039;{1}&#039;/&gt;&lt;Value Type=&#039;DateTime&#039;&gt;{2}&lt;/Value&gt;&lt;/{0}&gt;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;relation, dateFieldName, <span style="color:#2b91af;">SPUtility</span>.CreateISO8601DateTimeFromSystemDateTime(filterDate));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> filter;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>And here is an example about the usage:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:12cb9d43-3965-4149-b316-44664ff9365d" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">DateTime</span> startDate = <span style="color:#2b91af;">DateTime</span>.Today.AddDays(11);</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">DateTime</span> endDate = <span style="color:#2b91af;">DateTime</span>.Today.AddDays(15);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">SPList</span> taskList = web.Lists[<span style="color:#a31515;">&quot;Tasks&quot;</span>];</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">SPQuery</span> query = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPQuery</span>();</li>
<li>query.ViewFields = <span style="color:#a31515;">&quot;&lt;FieldRef Name=&#039;Title&#039;/&gt;&lt;FieldRef Name=&#039;StartDate&#039;/&gt;&lt;FieldRef Name=&#039;DueDate&#039;/&gt;&quot;</span>;</li>
<li style="background:#f3f3f3;">query.Query = BuildDateRangeOverlapFilter(startDate, endDate);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">SPListItemCollection</span> matches = taskList.GetItems(query);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">foreach</span> (<span style="color:#2b91af;">SPListItem</span> match <span style="color:#0000ff;">in</span> matches)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Console</span>.WriteLine(match[<span style="color:#a31515;">&quot;Title&quot;</span>]);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p><strong>Note 1:</strong> The start date of the date ranges must be less than or equal to the end date, but I think that is an acceptable restriction.</p>
<p><strong>Note 2: </strong>Before you ask it, I’ve not tested this solution with recurring events, but I assume it does not work in that case. Sorry!</p>
<p><strong>Note 3: </strong>To convert my date stored as <em>DateTime </em>to <em>String</em> I used earlier the <em>DateTime.ToString(String format) </em>method with format pattern <em>&quot;yyyy-MM-ddT00:00:00Z&quot;</em>, later I found that more-or-less matches the <em>UniversalSortableDateTimePattern</em>, so switched to parameter <em>“u”</em>. If you check the source code of the <em>SPUtility.CreateISO8601DateTimeFromSystemDateTime </em>method used in this sample (for example with <em>Reflector</em>), you can see that instead of simply calling <em>DateTime.ToString </em>with the right pattern, it takes the individual parts of the <em>DateTime </em>(like year, month, day, etc.), and creates the result by appending these component to each other using a <em>StringBuilder</em>. At first it was quite strange for me, but then thought, it must be the fastest way of conversion, as it does not have the overhead of pattern recognition and lot of conditions found in <em>DateTime.ToString</em>. However, if you should work with time zone information, then it might be not ideal for you. Although <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> supports time zones, AFAIS it is not implemented in <em>CreateISO8601DateTimeFromSystemDateTime</em>, for example, by providing an overload with a <em>DateTimeOffset </em>parameter. I don’t know if <em>CAML</em> supports at all this kind of time zone information provided in the date string. I admit it is not a crucial question most of the cases, however it might produce a surprise if not taken into account. You can read a bit more about that <a href="http://blog-sharepoint.blogspot.com/2009/07/infopath-caml-date-time-format-in-when.html">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/831/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=831&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/11/11/how-to-find-overlapping-items-for-a-date-range/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>
	</item>
		<item>
		<title>Setting form field visibility and editability through custom list field iterators</title>
		<link>http://pholpar.wordpress.com/2011/11/09/setting-form-field-visibility-and-editability-through-custom-list-field-iterators/</link>
		<comments>http://pholpar.wordpress.com/2011/11/09/setting-form-field-visibility-and-editability-through-custom-list-field-iterators/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 23:27:06 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[Custom forms]]></category>
		<category><![CDATA[Dynamic method]]></category>
		<category><![CDATA[SP 2010]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/11/09/setting-form-field-visibility-and-editability-through-custom-list-field-iterators/</guid>
		<description><![CDATA[In a recent post I showed you the basic of SharePoint list form customizations via custom list filed iterators. In this post I provide a sample that can serve as a base for your form customization needs. From the sample you will learn how to alter the default display mode of the form fields, for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=830&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://pholpar.wordpress.com/2011/10/16/customizing-sharepoint-forms-via-creating-our-own-listfielditerator/">recent post</a> I showed you the basic of SharePoint list form customizations via custom list filed iterators.</p>
<p>In this post I provide a sample that can serve as a base for your form customization needs. From the sample you will learn how to alter the default display mode of the form fields, for example, hide fields on the edit or display form, or display fields as read-only on edit form. As a bonus, I will show you how to inject your code into the saving mechanism of list items and override the default behavior.</p>
<p>Before writing our own code, let’s see how the default field rendering is working. The following code snippet is the <em>CreateChildControls </em>method of the <em>Microsoft.SharePoint.WebControls.ListFieldIterator </em>class. </p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d0be19f2-4579-4b63-b4ce-77fbbc0d68dc" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> CreateChildControls()</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.Controls.Clear();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>.ControlTemplate == <span style="color:#0000ff;">null</span>)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Could not find ListFieldIterator control template.&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i &lt; <span style="color:#0000ff;">base</span>.Fields.Count; i++)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPField</span> field = <span style="color:#0000ff;">base</span>.Fields[i];</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">this</span>.IsFieldExcluded(field))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">TemplateContainer</span> child = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">TemplateContainer</span>();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.Controls.Add(child);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;child.ControlMode = <span style="color:#0000ff;">base</span>.ControlMode;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;child.FieldName = field.InternalName;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.ControlTemplate.InstantiateIn(child);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>As you can see, hiding a field is quite straightforward, you should simply skip the field similarly to the <em>IsFieldExcluded </em>condition. In fact, the <em>IsFieldExcluded </em>property is a useful way to hide fields, but the method shown in the post hopefully provides a more flexible solution.</p>
<p>Displaying the field as read-only on the edit form would be possible through setting the <em>ControlMode</em> property of the <em>TemplateContainer</em> instance in the overriden version of <em>CreateChildControls </em>method. </p>
<p>Unfortunately, both <em>ControlMode</em> and <em>FieldName</em> properties are internal members of the <em>TemplateContainer</em> class, so there is no simple way to work with them in our custom <em>CreateChildControls </em>method.</p>
<p>That is where <em>Reflection </em>or <em>dynamic methods </em>come into the picture. In this case I chose the second one, and created the <em>ILUtils</em> helper class:</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6b1fe906-7079-40d4-a106-5c36e7ace2aa" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">delegate</span> <span style="color:#0000ff;">void</span> <span style="color:#2b91af;">GenericSetter</span>(<span style="color:#0000ff;">object</span> target, <span style="color:#0000ff;">object</span> value);</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">delegate</span> <span style="color:#0000ff;">object</span> <span style="color:#2b91af;">GenericGetter</span>(<span style="color:#0000ff;">object</span> target);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">class</span> <span style="color:#2b91af;">ILUtils</span></li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#808080;">///</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#808080;">///</span><span style="color:#008000;"> Creates a dynamic setter for the property</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#808080;">///</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">GenericSetter</span> CreateSetMethod(<span style="color:#2b91af;">Type</span> targetType, <span style="color:#2b91af;">String</span> propName)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">GenericSetter</span> result = <span style="color:#0000ff;">null</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">PropertyInfo</span> propertyInfo = targetType.GetProperty(propName,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">BindingFlags</span>.NonPublic | <span style="color:#2b91af;">BindingFlags</span>.Instance);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (propertyInfo != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">MethodInfo</span> setMethod = propertyInfo.GetSetMethod(<span style="color:#0000ff;">true</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (setMethod != <span style="color:#0000ff;">null</span>)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Type</span>[] arguments = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Type</span>[2];</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;arguments[0] = arguments[1] = <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">DynamicMethod</span> setter = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">DynamicMethod</span>(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;_Set&quot;</span>, propertyInfo.Name, <span style="color:#a31515;">&quot;_&quot;</span>),</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">void</span>), arguments, propertyInfo.DeclaringType);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">ILGenerator</span> generator = setter.GetILGenerator();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Ldarg_0);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Castclass, propertyInfo.DeclaringType);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Ldarg_1);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (propertyInfo.PropertyType.IsClass)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Castclass, propertyInfo.PropertyType);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">else</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Unbox_Any, propertyInfo.PropertyType);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.EmitCall(<span style="color:#2b91af;">OpCodes</span>.Callvirt, setMethod, <span style="color:#0000ff;">null</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Ret);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;result = (<span style="color:#2b91af;">GenericSetter</span>)setter.CreateDelegate(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">GenericSetter</span>));</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#808080;">///</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#808080;">///</span><span style="color:#008000;"> Creates a dynamic getter for the property</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#808080;">///</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">GenericGetter</span> CreateGetMethod(<span style="color:#2b91af;">Type</span> targetType, <span style="color:#2b91af;">String</span> propName)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">GenericGetter</span> result = <span style="color:#0000ff;">null</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">PropertyInfo</span> propertyInfo = targetType.GetProperty(propName,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">BindingFlags</span>.NonPublic | <span style="color:#2b91af;">BindingFlags</span>.Instance);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (propertyInfo != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">MethodInfo</span> getMethod = propertyInfo.GetGetMethod(<span style="color:#0000ff;">true</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (getMethod != <span style="color:#0000ff;">null</span>)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Type</span>[] arguments = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Type</span>[1];</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;arguments[0] = <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">DynamicMethod</span> getter = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">DynamicMethod</span>(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;_Get&quot;</span>, propertyInfo.Name, <span style="color:#a31515;">&quot;_&quot;</span>),</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>), arguments, propertyInfo.DeclaringType);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">ILGenerator</span> generator = getter.GetILGenerator();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.DeclareLocal(<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>));</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Ldarg_0);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Castclass, propertyInfo.DeclaringType);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.EmitCall(<span style="color:#2b91af;">OpCodes</span>.Callvirt, getMethod, <span style="color:#0000ff;">null</span>);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!propertyInfo.PropertyType.IsClass)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Box, propertyInfo.PropertyType);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;generator.Emit(<span style="color:#2b91af;">OpCodes</span>.Ret);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;result = (<span style="color:#2b91af;">GenericGetter</span>)getter.CreateDelegate(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">GenericGetter</span>));</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>Above idea and most of the code are borrowed from <a href="http://jachman.wordpress.com/2006/08/22/2000-faster-using-dynamic-method-calls/">this great post</a>.</p>
<p>We will store display rule exceptions using <em>FieldDisplayRuleItem </em>instances. <em>FieldDisplayRule </em>determines if we should hide the field or display it as read-only. <em>FieldNames</em> and <em>ControlModes</em> stores the field names and control modes the rule applies to. We will see this class in action soon.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3bec5361-e931-43b9-b82b-ab70408b75dd" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#008000;">// helper class to store display exception rules</span></li>
<li style="background:#f3f3f3;"><span style="color:#008000;">// you can add your own properties to extend logic</span></li>
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">FieldDisplayRuleItem</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// list of field names the rule applies to</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt; FieldNames { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// list of control modes the rule applies to</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">SPControlMode</span>&gt; ControlModes { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// the resulting display exeption rule</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#2b91af;">FieldDisplayRule</span> Rule { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> <span style="color:#2b91af;">FieldDisplayRule</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;Hidden,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;Display</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p><strong>Note</strong>: you can extend the <em>FieldDisplayRuleItem </em>class as your requirements dictate. For example if the items you work with have a <em>Status </em>field it is common to include this field in the rules. Of course, in this case you should alter the logic that selects the items to get the hidden / read-only fields as illustrated later.</p>
<p>Using the dynamic methods helper class it is easy to create a setter for the <em>ControlMode</em> and <em>FieldName</em> properties of the <em>TemplateContainer</em> class, that we can use in the overriden <em>CreateChildControls </em>method of our base class.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bc9ec85e-c8db-4c55-8ba9-b2e4266d31ea" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">BaseListFieldIterator</span> : Microsoft.SharePoint.WebControls.<span style="color:#2b91af;">ListFieldIterator</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// create dynamic setter methods that wrap the internal</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// ControlMode and FieldName properties of the TemplateContainer class&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">GenericSetter</span> set_TemplateContainer_ControlMode =</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">ILUtils</span>.CreateSetMethod(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">TemplateContainer</span>), <span style="color:#a31515;">&quot;ControlMode&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">GenericSetter</span> set_TemplateContainer_FieldName =</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">ILUtils</span>.CreateSetMethod(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">TemplateContainer</span>), <span style="color:#a31515;">&quot;FieldName&quot;</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">FieldDisplayRuleItem</span>&gt; _dynamicRules = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">FieldDisplayRuleItem</span>&gt;();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// get references for the frequently used objects</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">SPSite</span> _site = <span style="color:#2b91af;">SPContext</span>.Current.Site;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">SPWeb</span> _web = <span style="color:#2b91af;">SPContext</span>.Current.Web;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">SPContext</span> _context = <span style="color:#2b91af;">SPContext</span>.Current;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#2b91af;">SPFormContext</span> _formContext = <span style="color:#2b91af;">SPContext</span>.Current.FormContext;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> CreateChildControls()</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.Controls.Clear();</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>.ControlTemplate == <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&quot;Could not find ListFieldIterator control template.&quot;</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i &lt; <span style="color:#0000ff;">base</span>.Fields.Count; i++)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPField</span> field = <span style="color:#0000ff;">base</span>.Fields[i];</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> fieldName = field.InternalName;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// check if the current field is on the list of &quot;hidden&quot; fields in the current display mode</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// or whether there is a &quot;global&quot; rule to hide fields</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">FieldDisplayRuleItem</span> exception = _dynamicRules.FirstOrDefault(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// empty (null) value means there is no restriction for the control mode</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;e =&gt; ((e.ControlModes == <span style="color:#0000ff;">null</span>) || (e.ControlModes.Contains(ControlMode))) &amp;&amp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// empty (null) value means there is no restriction for the field name</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;((e.FieldNames == <span style="color:#0000ff;">null</span>) || (e.FieldNames.Contains(fieldName))) &amp;&amp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(e.Rule == <span style="color:#2b91af;">FieldDisplayRule</span>.Hidden));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((!<span style="color:#0000ff;">this</span>.IsFieldExcluded(field)) &amp;&amp; (exception == <span style="color:#0000ff;">null</span>))</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">TemplateContainer</span> child = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">TemplateContainer</span>();</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.Controls.Add(child);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPControlMode</span> controlMode = GetControlMode(fieldName);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// use the dynamic setter to access internal properties</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;set_TemplateContainer_ControlMode(child, controlMode);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;set_TemplateContainer_FieldName(child, fieldName);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">this</span>.ControlTemplate.InstantiateIn(child);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#2b91af;">SPControlMode</span> GetControlMode(<span style="color:#0000ff;">string</span> fieldName)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">FieldDisplayRuleItem</span> rule = _dynamicRules.FirstOrDefault(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;e =&gt; ((e.ControlModes == <span style="color:#0000ff;">null</span>) || (e.ControlModes.Contains(ControlMode))) &amp;&amp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;((e.FieldNames == <span style="color:#0000ff;">null</span>) || (e.FieldNames.Contains(fieldName))) &amp;&amp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(e.Rule == <span style="color:#2b91af;">FieldDisplayRule</span>.Display));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPControlMode</span> result = (rule == <span style="color:#0000ff;">null</span>) ? ControlMode : <span style="color:#2b91af;">SPControlMode</span>.Display;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>Instead of using string literals in my methods, I usually use static classes to store string constants like field or list names:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3fb38de4-fcf6-45f5-abae-9f97832ca4f8" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">SampleTaskListFields</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> Id = <span style="color:#a31515;">&quot;ID&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> Title = <span style="color:#a31515;">&quot;Title&quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> PercentComplete = <span style="color:#a31515;">&quot;PercentComplete&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> AssignedTo = <span style="color:#a31515;">&quot;AssignedTo&quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> Status = <span style="color:#a31515;">&quot;Status&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> Predecessors = <span style="color:#a31515;">&quot;Predecessors&quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> StartDate = <span style="color:#a31515;">&quot;StartDate&quot;</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> DueDate = <span style="color:#a31515;">&quot;DueDate&quot;</span>;</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Lists</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> SampleList = <span style="color:#a31515;">&quot;SampleTaskList&quot;</span>;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">TaskStates</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// we only declare the state names used in the sample</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> NotStarted = <span style="color:#a31515;">&quot;Not Started&quot;</span>;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">readonly</span> <span style="color:#2b91af;">String</span> Completed = <span style="color:#a31515;">&quot;Completed&quot;</span>;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>If you prefer working with <em>Guid</em>s then you may consider using the <i>SPBuiltInFieldId</i> class instead.</p>
<p>The exact implementation is derived from the <em>BaseListFieldIterator </em>class. We store static (global) rules in <em>_staticRules </em>and dynamic rules in <em>_dynamicRules</em>.</p>
<p>In the current example we are working with a task list and would like to achieve the following results:</p>
<p>Static rules:</p>
<ul>
<li>The <em>Title</em> field can be edited only when the task is created. </li>
<li><em>Status</em> and <em>% Complete</em> fields are hidden on task creation (we will set this values from our custom save handler method later). </li>
</ul>
<p>Dynamic rules:</p>
<ul>
<li><em>Predecessors</em> field is only editable by site administrators. </li>
<li><em>Start Date</em> is only editable if the item is in the <em>Not Started</em> status. </li>
</ul>
<p>The following code creates the rules for these requirements.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2c129753-54d2-4dcd-9fdd-0f6f85c9a414" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">ListFieldIterator</span> : <span style="color:#2b91af;">BaseListFieldIterator</span></li>
<li style="background:#f3f3f3;">{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">static</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">FieldDisplayRuleItem</span>&gt; _staticRules = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">FieldDisplayRuleItem</span>&gt;();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">static</span> ListFieldIterator()</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// task title can be set on item creation</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// but can&#039;t be altered later</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_staticRules.Add(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FieldDisplayRuleItem</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FieldNames = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt;() { <span style="color:#2b91af;">SampleTaskListFields</span>.Title },</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ControlModes = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">SPControlMode</span>&gt;() { <span style="color:#2b91af;">SPControlMode</span>.Edit },</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Rule = <span style="color:#2b91af;">FieldDisplayRule</span>.Display</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// we don&#039;t allow to set status and % info on new item form</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// items will be created with status &quot;Not Started&quot; and 0 % </span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// these values are set on item saving from code</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_staticRules.Add(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FieldDisplayRuleItem</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FieldNames = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt;() { <span style="color:#2b91af;">SampleTaskListFields</span>.Status, <span style="color:#2b91af;">SampleTaskListFields</span>.PercentComplete },</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ControlModes = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">SPControlMode</span>&gt;() { <span style="color:#2b91af;">SPControlMode</span>.New },</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Rule = <span style="color:#2b91af;">FieldDisplayRule</span>.Hidden</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnInit(<span style="color:#2b91af;">EventArgs</span> e)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">base</span>.OnInit(e);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// register save handler if not in display mode and form is posted back</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((Page.IsPostBack) &amp;&amp; (ControlMode != <span style="color:#2b91af;">SPControlMode</span>.Display))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_formContext.OnSaveHandler += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">EventHandler</span>(SaveHandler);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CreateDynamicExceptions();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> CreateDynamicExceptions()</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_dynamicRules.AddRange(_staticRules);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// only site admins are allowed to edit predecessor tasks</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// others can see only the predecessors regardless of the control mode</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (!_web.CurrentUser.IsSiteAdmin)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_dynamicRules.Add(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FieldDisplayRuleItem</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FieldNames = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt;() { <span style="color:#2b91af;">SampleTaskListFields</span>.Predecessors },</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Rule = <span style="color:#2b91af;">FieldDisplayRule</span>.Display</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// start date can be set only if the task is not started</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((<span style="color:#2b91af;">String</span>)Item[<span style="color:#2b91af;">SampleTaskListFields</span>.Status] != <span style="color:#2b91af;">TaskStates</span>.NotStarted)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_dynamicRules.Add(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">FieldDisplayRuleItem</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;FieldNames = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">String</span>&gt;() { <span style="color:#2b91af;">SampleTaskListFields</span>.StartDate },</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Rule = <span style="color:#2b91af;">FieldDisplayRule</span>.Display</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> SaveHandler(<span style="color:#0000ff;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Page.Validate();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (Page.IsValid)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// do custom activities, send mail, create task,</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// set permissions etc.</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// new tasks are created with status &#039;Not Started&#039; and 0% complete</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (ControlMode == <span style="color:#2b91af;">SPControlMode</span>.New)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Item[<span style="color:#2b91af;">SampleTaskListFields</span>.PercentComplete] = 0;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Item[<span style="color:#2b91af;">SampleTaskListFields</span>.Status] = <span style="color:#2b91af;">TaskStates</span>.NotStarted;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// we should save the item explicitly</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Item.Update();</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>In the <em>OnInit</em> method we register of custom save handler method. In the <em>SaveHandler</em> method we set the values for the <em>Status</em> and <em>% Complete</em> fields if the item is a new task. That is the method you can include your custom actions into, like sending mails or setting permissions on items.</p>
<p>To test the solution, we should first deploy the assembly as well as our user control. Then create a <em>Tasks</em> list called <em>SampleTaskList</em>, and register our custom <em>ListFieldIterator</em> to all of its forms as shown <a href="http://pholpar.wordpress.com/2011/10/16/customizing-sharepoint-forms-via-creating-our-own-listfielditerator/">in my former post</a>.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb.png?w=1020&#038;h=641" width="1020" height="641" /></a> </p>
<p>Logged on as a site admin, we create a task item.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image1.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb1.png?w=625&#038;h=672" width="625" height="672" /></a> </p>
</p>
<p>Although the <em>Status</em> and <em>% Complete</em> fields are not visible on the form, the values of the fields are populated.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image2.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb2.png?w=814&#038;h=53" width="814" height="53" /></a></p>
<p>Next, we create another task item, and set the first one as a predecessor. </p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image3.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb3.png?w=625&#038;h=673" width="625" height="673" /></a> </p>
<p>After saving the item, we open it again in edit mode. The <em>Title</em> field displayed as read-only.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image4.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb4.png?w=632&#038;h=730" width="632" height="730" /></a></p>
<p>We log in as a standard site member (non-admin), and open the item once again. The <em>Predecessors</em> field is also read-only in this case. We set the <em>Status </em>to <em>In Progress</em>.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image5.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb5.png?w=631&#038;h=617" width="631" height="617" /></a> </p>
<p>After re-opening the item we see that the <em>Start Date</em> field became read-only.</p>
<p><a href="http://pholpar.files.wordpress.com/2011/11/image6.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/11/image_thumb6.png?w=632&#038;h=613" width="632" height="613" /></a>&#160; </p>
<p>I hope this example helps you to create SharePoint forms that fit better to the common customer needs. In the <a href="http://pholpar.wordpress.com/2011/11/14/creating-custom-validation-rules-in-our-list-field-iterators/">following posts</a> I’m planning to build on this sample with other interesting features.</p>
<p>You can download the sample application from <a href="http://code.msdn.microsoft.com/SharePoint-list-form-ef7d732b">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/830/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=830&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/11/09/setting-form-field-visibility-and-editability-through-custom-list-field-iterators/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/11/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Using SharePoint 2010 Word Automation Services to convert document synchronously</title>
		<link>http://pholpar.wordpress.com/2011/10/27/using-sharepoint-2010-word-automation-services-to-convert-document-synchronously/</link>
		<comments>http://pholpar.wordpress.com/2011/10/27/using-sharepoint-2010-word-automation-services-to-convert-document-synchronously/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 20:43:10 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[SP 2010]]></category>
		<category><![CDATA[Word Automation Services]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/10/27/using-sharepoint-2010-word-automation-services-to-convert-document-synchronously/</guid>
		<description><![CDATA[A few months ago a fellow developer asked me how to use WAS in an application that requires synchronous document conversion. In the current post I show you a simple way for that. As you might know (if not you can read a bit more about that at the end of this post), you can [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=815&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few months ago a fellow developer asked me how to use <em>WAS </em>in an application that requires synchronous document conversion. In the current post I show you a simple way for that.</p>
<p>As you might know (if not you can read a bit more about that at the end of <a href="http://pholpar.wordpress.com/2010/04/27/a-simple-demo-application-of-office-2010-and-sharepoint-2010-development-using-visual-studio-2010/">this post</a>), you can submit <em>Word </em>documents to <em>WAS </em>and let it convert the documents to <em>PDF </em>or other formats like <em>XPS</em>. <em>WAS </em>works as a timer job, so conversion is done based on the schedule of the job that you should set based on the number of documents to be converted and the free resources of the server. As in the case of any timer job, you can start the <em>Word Automation Services Timer Job</em> immediately using the web UI and <a href="http://pholpar.wordpress.com/2010/03/17/enumerating-timer-jobs-for-service-applications-and-a-new-way-of-forcing-the-immediate-execution-of-a-timer-job-from-code/">from custom code</a> as well.</p>
<p>For the sample method I pass in the document content as a <em>byte</em> array and the converted document is returned by the method as a <em>byte</em> array as well. First I’ve implemented a <em>Stream</em>-based solution but found it is easier to work with byte arrays in this case (see reason a bit later).</p>
<p>After preparing and starting the <em>ConversionJob</em>, we start the <em>WAS</em> timer job if immediate conversion is requested, then wait until the conversion is finished either successfully or unsuccessfully or until the timeout interval elapsed. In case of timeout, we cancel the conversion process. Next we display possible conversion errors and delete the documents from the working document library if requested.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:20c86e5a-4636-4c10-93d4-a7fb2d50b0ec" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:500px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">byte</span>[] ConvertDocument(<span style="color:#2b91af;">SPWeb</span> web, <span style="color:#0000ff;">byte</span>[] docToConvert, <span style="color:#0000ff;">bool</span> isImmediate,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> conversionLibName, <span style="color:#0000ff;">int</span> timeOutSecs, <span style="color:#0000ff;">bool</span> deleteDocs)</li>
<li>{</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">byte</span>[] result = <span style="color:#0000ff;">null</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPList</span> conversionLib = web.Lists[conversionLibName];</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFolder</span> folder = conversionLib.RootFolder;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// Get the default proxy for the current Word Automation Services instance</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPServiceContext</span> serviceContext = <span style="color:#2b91af;">SPServiceContext</span>.GetContext(web.Site);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">WordServiceApplicationProxy</span> wordServiceApplicationProxy =</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(<span style="color:#2b91af;">WordServiceApplicationProxy</span>)serviceContext.GetDefaultProxy(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">WordServiceApplicationProxy</span>));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">ConversionJob</span> job = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ConversionJob</span>(wordServiceApplicationProxy);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;job.UserToken = web.CurrentUser.UserToken;</li>
<li>&#160;&#160;&#160;&#160;job.Settings.UpdateFields = <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;job.Settings.OutputSaveBehavior = <span style="color:#2b91af;">SaveBehavior</span>.AlwaysOverwrite;</li>
<li>&#160;&#160;&#160;&#160;job.Settings.OutputFormat = <span style="color:#2b91af;">SaveFormat</span>.PDF;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> docFileName = <span style="color:#2b91af;">Guid</span>.NewGuid().ToString(<span style="color:#a31515;">&quot;D&quot;</span>);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// we replace possible existing files on upload</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// although there is a minimal chance for GUID duplicates <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFile</span> docFile = folder.Files.Add(docFileName + <span style="color:#a31515;">&quot;.docx&quot;</span>, docToConvert, <span style="color:#0000ff;">true</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;conversionLib.AddItem(docFileName + <span style="color:#a31515;">&quot;.docx&quot;</span>, <span style="color:#2b91af;">SPFileSystemObjectType</span>.File);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> docFileUrl = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;{0}/{1}&quot;</span>, web.Url, docFile.Url);</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> pdfFileUrl = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;{0}/{1}.pdf&quot;</span>,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;web.Url, docFile.Url.Substring(0, docFile.Url.Length &#8211; 5));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;job.AddFile(docFileUrl, pdfFileUrl);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// let&#039;s do the job <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// Start-SPTimerJob &quot;Word Automation Services&quot;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;job.Start();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (isImmediate)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;StartServiceJob(<span style="color:#a31515;">&quot;Word Automation Services Timer Job&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">ConversionJobStatus</span> cjStatus = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ConversionJobStatus</span>(wordServiceApplicationProxy, job.JobId, <span style="color:#0000ff;">null</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// set up timeout</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">TimeSpan</span> timeSpan = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">TimeSpan</span>(0, 0, timeOutSecs);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">DateTime</span> conversionStarted = <span style="color:#2b91af;">DateTime</span>.Now;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">int</span> finishedConversionCount = cjStatus.Succeeded + cjStatus.Failed;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">while</span> ((finishedConversionCount != 1) &amp;&amp; ((<span style="color:#2b91af;">DateTime</span>.Now &#8211; conversionStarted) &lt; timeSpan))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// wait a sec.</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Thread</span>.Sleep(1000);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cjStatus = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ConversionJobStatus</span>(wordServiceApplicationProxy, job.JobId, <span style="color:#0000ff;">null</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;finishedConversionCount = cjStatus.Succeeded + cjStatus.Failed;</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// timeouted -&gt; cancel conversion</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (finishedConversionCount != 1)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;job.Cancel();</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// we can output the possible failed conversion error(s)</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">foreach</span> (<span style="color:#2b91af;">ConversionItemInfo</span> cii <span style="color:#0000ff;">in</span> cjStatus.GetItems(<span style="color:#2b91af;">ItemTypes</span>.Failed))</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Failed conversion. Input file: &#039;{0}&#039;; Output file: &#039;{1}&#039;; Error code: &#039;{2}&#039;; Error message: &#039;{3}&#039;;&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cii.InputFile, cii.OutputFile, cii.ErrorCode, cii.ErrorMessage);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFile</span> convertedFile = web.GetFile(pdfFileUrl);</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// shouldn&#039;t be null (unless there is a conversion error)</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// but we check for sure</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((convertedFile != <span style="color:#0000ff;">null</span>) &amp;&amp; (convertedFile.Exists))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">Stream</span> pdfStream = convertedFile.OpenBinaryStream();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;result = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[pdfStream.Length];</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pdfStream.Read(result, 0, result.Length);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// delete result doc if requested</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (deleteDocs)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;convertedFile.Delete();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#008000;">// delete source doc if requested</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (deleteDocs)</li>
<li>&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;docFile.Delete();</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> result;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> StartServiceJob(<span style="color:#0000ff;">string</span> serviceTypeName, <span style="color:#0000ff;">string</span> jobTypeName)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFarm</span>.Local.Services.ToList().ForEach(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;svc =&gt; svc.JobDefinitions.ToList().ForEach(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jd =&gt;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((jd.TypeName == jobTypeName) &amp;&amp; ((serviceTypeName == <span style="color:#0000ff;">null</span>) || (serviceTypeName == svc.TypeName)))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jd.RunNow();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}));</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>To start immediate conversion in the <em>ConvertDocument</em> method I used a slightly modified version of the <em>StartServiceJob</em> method already introduced in <a href="http://pholpar.wordpress.com/2010/03/17/enumerating-timer-jobs-for-service-applications-and-a-new-way-of-forcing-the-immediate-execution-of-a-timer-job-from-code/">my former post</a>.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52f9d87c-b3dc-4ccc-8375-1f5df6944040" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> StartServiceJob(<span style="color:#0000ff;">string</span> serviceTypeName, <span style="color:#0000ff;">string</span> jobTypeName)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">SPFarm</span>.Local.Services.ToList().ForEach(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;svc =&gt; svc.JobDefinitions.ToList().ForEach(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jd =&gt;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> ((jd.TypeName == jobTypeName) &amp;&amp; ((serviceTypeName == <span style="color:#0000ff;">null</span>) || (serviceTypeName == svc.TypeName)))</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jd.RunNow();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}));</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> StartServiceJob(<span style="color:#0000ff;">string</span> jobTypeName)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;StartServiceJob(<span style="color:#0000ff;">null</span>, jobTypeName);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>The following code snippet shows a sample for calling the <em>ConvertDocument</em> method. In this case we request an immediate conversion with 240 seconds timeout and use the standard <em>Shared Documents </em>document library as a working folder, deleting the temporary files.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:babcc361-21c3-43b2-94c4-551505fd4405" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">DateTime</span> startTime = <span style="color:#2b91af;">DateTime</span>.Now;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">byte</span>[] doc = <span style="color:#2b91af;">File</span>.ReadAllBytes(<span style="color:#a31515;">@&quot;C:&#092;Data&#092;HelloWorld.docx&quot;</span>);</li>
<li><span style="color:#0000ff;">byte</span>[] pdf = ConvertDocument(web, doc, <span style="color:#0000ff;">true</span>, <span style="color:#a31515;">&quot;Shared Documents&quot;</span>, 240, <span style="color:#0000ff;">true</span>);</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">if</span> (pdf != <span style="color:#0000ff;">null</span>)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">File</span>.WriteAllBytes(<span style="color:#a31515;">@&quot;C:&#092;Data&#092;HelloWorld.pdf&quot;</span>, pdf);</li>
<li>}</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Duration of conversion: {0} ms&quot;</span>, (<span style="color:#2b91af;">DateTime</span>.Now &#8211; startTime).TotalMilliseconds);</li>
</ol></div>
</p></div>
</p></div>
<p>The sample above requires further work if you would like to use it in a real application. First, you should add some extra error handling, for example check if default <em>WordServiceApplicationProxy </em>is found at all, etc.</p>
<p>Next, instead of submitting documents one by one to <em>WAS</em> it is better to create a <em>ConvertDocument</em> version that supports multiple document conversion. In this case you should use arrays of <em>byte </em>arrays that I found easier than bothering (like disposing through <em>using </em>blocks) with multiple streams simultaneously.</p>
<p>You can extend the supported conversion options to other formats as well, like <em>XPS</em>.</p>
<p>In a real life application you probably wouldn’t like to start immediate conversions on each requests because it might produce a heavy load on your servers. Instead you can create a specific queue for documents with the option for high privilege users to submit dedicated document types for immediate conversions and leave the default conversion schedule for the others.</p>
<p>Although our original goal was to create a synchronous conversion method, sometimes it is more comfortable to do the conversion asynchronously, for example to avoid locking of the UI thread. To support that in your application, you can start <em>ConvertDocument</em> in a separate thread and raise your custom .NET events to reflect the output of the conversion job.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/815/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=815&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/10/27/using-sharepoint-2010-word-automation-services-to-convert-document-synchronously/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting the process ID of the IIS / SharePoint application pool using PowerShell</title>
		<link>http://pholpar.wordpress.com/2011/10/24/getting-the-process-id-of-the-iis-sharepoint-application-pool-using-powershell/</link>
		<comments>http://pholpar.wordpress.com/2011/10/24/getting-the-process-id-of-the-iis-sharepoint-application-pool-using-powershell/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 21:28:07 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SP 2010]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/10/24/getting-the-process-id-of-the-iis-sharepoint-application-pool-using-powershell/</guid>
		<description><![CDATA[It is easy to attach your debugger to the right SharePoint application pool using Visual Studio, especially if specific extensions like CKS.Dev or my VS extension is installed. Sometimes life is not so trivial, you don’t have Visual Studio installed on the server and / or you should use other tools to debug your application. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=812&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It is easy to attach your debugger to the right SharePoint application pool using <em>Visual Studio</em>, especially if specific extensions like <a href="http://cksdev.codeplex.com/">CKS.Dev</a> or my <a href="http://pholpar.wordpress.com/2010/05/20/visual-studio-2010-extension-for-helping-sharepoint-2010-debugging/">VS extension</a> is installed.</p>
<p>Sometimes life is not so trivial, you don’t have <em>Visual Studio</em> installed on the server and / or you should use other tools to debug your application. It would be great to have the right process ID in this case as well.</p>
<p>For example, assume you have to <a href="http://windbg.info/doc/1-common-cmds.html#3_sessions">attach WinDbg</a> or other debugging tools like <a href="http://nickhoggard.wordpress.com/2010/06/17/debugging-net-il-at-runtime-using-reflector-deblector/">Deblector</a> to the process of the IIS application pool of a selected SharePoint application.</p>
<p>Two years ago I wrote a <a href="http://pholpar.wordpress.com/2009/06/12/i-got-tired-of-attaching-debugger-to-w3wp-exe-and-you/">post</a> about how to attach the <em>VS</em> debugger from a <em>VS</em> macro. In that post I’ve illustrated how to get the right process based on the application pool name (see the <em>GetProcessIdByAppPoolName</em> and <em>GetAppPoolNameFromCommandLine </em>methods in that post). In the <a href="http://pholpar.wordpress.com/2010/05/20/visual-studio-2010-extension-for-helping-sharepoint-2010-debugging/">post</a> about my <em>VS</em> extension mentioned above you can find a similar solution as well.</p>
<p>In this post I show you two <em>PowerShell</em> methods that can be used when there is no <em>Visual Studio</em> on the computer or you simply do not want to work with that.</p>
<p>You should specify the name of the application pool for the <em>Get-AppPoolProcessIdByName</em> method and it displays the process <em>ID</em> of the matching application pool: </p>
<p><em>function Get-AppPoolProcessIdByName($name)&#160; <br />{&#160; <br />&#160;&#160;&#160;&#160; Get-WmiObject Win32_Process |      <br />&#160;&#160;&#160;&#160;&#160;&#160; Where-Object { $_.CommandLine -like &quot;*w3wp.exe*&quot; } |      <br />&#160;&#160;&#160;&#160;&#160;&#160; ForEach-Object { [regex]::Matches($_.CommandLine, &quot;-ap &quot;&quot;(.+)&quot;&quot; -v&quot;) |      <br />&#160;&#160;&#160;&#160;&#160;&#160; Add-Member NoteProperty -Name ProcessId -Value $_.ProcessId -PassThru } |      <br />&#160;&#160;&#160;&#160;&#160;&#160; Where-Object { $_.Success -and $_.Groups.Count -gt 1 -and $_.Groups[1].Value -eq $name } |      <br />&#160;&#160;&#160;&#160;&#160;&#160; ForEach-Object { Write-Host $_.ProcessId }       <br />} </em></p>
<p>Usage:</p>
<p><em>Get-AppPoolProcessIdByName(&quot;SharePoint &#8211; 80&quot;) </em></p>
<p>The <em>Get-SPAppPoolProcessIdByUrl</em> method first determines the corresponding <em>SharePoint</em> web application, then calls the <em>Get-AppPoolProcessIdByName</em> method to get the process <em>ID</em>.</p>
<p><em>function Get-SPAppPoolProcessIdByUrl($url)&#160; <br />{&#160; <br />&#160;&#160;&#160;&#160; $app = Get-SPWebApplication $url      <br />&#160;&#160;&#160;&#160; Get-AppPoolProcessIdByName($app.DisplayName)       <br />} </em></p>
<p>Usage:</p>
<p><em>Get-SPAppPoolProcessIdByUrl(&quot;http://sp2010&quot;)</em></p>
<p>You can use <em>Get-AppPoolProcessIdByName</em> for arbitrary IIS application pool, but as you can see from the code (and as I intended to sign with the <em>Get-SP</em> prefix) the <em>Get-SPAppPoolProcessIdByUrl </em>method is SharePoint specific.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/812/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/812/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/812/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/812/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/812/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/812/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/812/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/812/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=812&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/10/24/getting-the-process-id-of-the-iis-sharepoint-application-pool-using-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a ToggleButton on SharePoint ribbon with alternating images</title>
		<link>http://pholpar.wordpress.com/2011/10/24/creating-a-togglebutton-on-sharepoint-ribbon-with-alternating-images/</link>
		<comments>http://pholpar.wordpress.com/2011/10/24/creating-a-togglebutton-on-sharepoint-ribbon-with-alternating-images/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 10:17:48 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Ribbon]]></category>
		<category><![CDATA[SP 2010]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/10/24/creating-a-togglebutton-on-sharepoint-ribbon-with-alternating-images/</guid>
		<description><![CDATA[Although ToggleButton is a useful control on the ribbon of SharePoint 2010 user interface, unfortunately I found no built-in way to assign separate images to the on / off states of the button. After playing awhile with the Internet Explorer Developer Tools (F12, see also the Tools menu in IE 8), I find out how [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=809&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Although <em>ToggleButton </em>is a useful control on the ribbon of <em>SharePoint 2010 </em>user interface, unfortunately I found no built-in way to assign separate images to the on / off states of the button.</p>
<p>After playing awhile with the <em>Internet Explorer Developer Tools</em> (<em>F12</em>, see also the <em>Tools </em>menu in <em>IE 8</em>), I find out how to achieve that through the <em>HTML </em>object model built up by the ribbon infrastructure.</p>
<p>The following <em>JavaScript </em>code is from the command handler method of the custom ribbon components and assumes <em>jQuery </em>is already loaded.</p>
<p>Replace the <em>commandId</em> of the <em>ToggleButton</em> and the image properties (<em>src</em> and <em>style</em> attributes) to match your needs.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8f5e7bad-b5d0-4c26-af8b-7801b7d24f2d" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">if</span> (commandId === <span style="color:#800000;">&#039;Ribbon.Custom.List.MyFeature.SwitchOnOff&#039;</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> x = jQuery(<span style="color:#800000;">&quot;a[id|=&#039;&quot;</span> + commandId + <span style="color:#800000;">&quot;&#039;]&quot;</span>).find(<span style="color:#800000;">&quot;img&quot;</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (properties.On) {</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;x.attr(<span style="color:#800000;">&quot;src&quot;</span>, <span style="color:#800000;">&quot;/_layouts/images/MyImages/On.png&quot;</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;x.attr(<span style="color:#800000;">&quot;style&quot;</span>, <span style="color:#800000;">&quot;top: 0px; left: 0px;&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">else</span> {</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;x.attr(<span style="color:#800000;">&quot;src&quot;</span>, <span style="color:#800000;">&quot;/_layouts/images/MyImages/Off.png&quot;</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;x.attr(<span style="color:#800000;">&quot;style&quot;</span>, <span style="color:#800000;">&quot;top: 0px; left: 0px;&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;}</li>
</ol></div>
</p></div>
</p></div>
<p>You can use separate image files, or a single one with different vertical / horizontal offset value.</p>
<p>I’ve defined a global <em>switchedOn</em> variable to track the actual status of the <em>ToggleButton </em>easier, and query that value from other <em>JavaScript </em>methods:</p>
<p><em>var switchedOn = false;</em></p>
<p>I hope this trick helps you to make your <em>SharePoint </em>UI a bit more dynamic.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/809/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=809&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/10/24/creating-a-togglebutton-on-sharepoint-ribbon-with-alternating-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>
	</item>
		<item>
		<title>How to deploy a custom field with custom properties from a feature &#8211; the missing second part</title>
		<link>http://pholpar.wordpress.com/2011/10/21/how-to-deploy-a-custom-field-with-custom-properties-from-a-feature-the-missing-second-part/</link>
		<comments>http://pholpar.wordpress.com/2011/10/21/how-to-deploy-a-custom-field-with-custom-properties-from-a-feature-the-missing-second-part/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 13:49:13 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[Custom fields]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Features]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SP 2010]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/10/21/how-to-deploy-a-custom-field-with-custom-properties-from-a-feature-the-missing-second-part/</guid>
		<description><![CDATA[Only readers following my blog from the beginning may remember my post I wrote about the deployment of custom fields having custom properties more than three years ago. Of course that post is about WSS 3.0. At that time I described how to include the custom properties into the field definition even if these properties [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=808&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Only readers following my blog from the beginning may remember my <a href="http://pholpar.wordpress.com/2008/08/12/how-to-deploy-a-custom-field-with-custom-properties-from-a-feature/">post</a> I wrote about the deployment of custom fields having custom properties more than three years ago. Of course that post is about <em>WSS 3.0</em>.</p>
<p>At that time I described how to include the custom properties into the field definition even if these properties are not declared by the <em>XSD </em>validation schema. However, injecting the custom properties the way I described there did not have effect on the values of the custom properties the custom field actually deployed with. I promised then that in the next part I would show you how to resolve this issue. It’s better late than never, the second part comes now, updated for <em>SharePoint 2010</em>.</p>
<p>The trick to achieve our goal seemed originally to be really simple. The base idea behind&#160; it was that an instance of the field must be created sooner or later before usage, and the values we would like to push into the custom properties were available on object creation in the field schema deployed.</p>
<p>I’ve modified the constructors of the custom field, adding a call to my custom <em>InitializeProps </em>method.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:63c90168-d2be-4849-93bd-9f3eb7a7ad98" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">public</span> SPFieldRegEx(<span style="color:#2b91af;">SPFieldCollection</span> fields, <span style="color:#0000ff;">string</span> fieldName)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;: <span style="color:#0000ff;">base</span>(fields, fieldName)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;InitializeProps();</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">public</span> SPFieldRegEx(<span style="color:#2b91af;">SPFieldCollection</span> fields, <span style="color:#0000ff;">string</span> typeName, <span style="color:#0000ff;">string</span> displayName)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;: <span style="color:#0000ff;">base</span>(fields, typeName, displayName)</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;InitializeProps();</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>The <em>InitializeProps </em>method is responsible for reading up the deployed custom property values from the field schema and setting them to the standard custom location in the schema.</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:960615ee-c137-4d26-80ec-75bd85320e56" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:400px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#008000;">// in the custom field feature definition</span></li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> <span style="color:#0000ff;">string</span> customNamespaceUri = <span style="color:#a31515;">&quot;http://schemas.grepton.com/sharepoint/&quot;</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> InitializeProps()</li>
<li>{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#2b91af;">String</span> regEx = (<span style="color:#2b91af;">String</span>)GetCustomProperty(<span style="color:#a31515;">&quot;RegEx&quot;</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// value of custom property is null if not yet set</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (regEx == <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#008000;">// load the field schema into an XML document</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">XmlDocument</span> schemaXml = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">XmlDocument</span>();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;schemaXml.LoadXml(SchemaXml);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#2b91af;">XmlNode</span> fieldNode = schemaXml.SelectSingleNode(<span style="color:#a31515;">&quot;Field&quot;</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (fieldNode != <span style="color:#0000ff;">null</span>)</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;InitCustomProperty(fieldNode, <span style="color:#a31515;">&quot;RegEx&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;InitCustomProperty(fieldNode, <span style="color:#a31515;">&quot;ErrMsg&quot;</span>);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;InitCustomProperty(fieldNode, <span style="color:#a31515;">&quot;MaxLen&quot;</span>);</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>}</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> InitCustomProperty(<span style="color:#2b91af;">XmlNode</span> fieldNode, <span style="color:#2b91af;">String</span> custPropName)</li>
<li style="background:#f3f3f3;">{</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#2b91af;">XmlAttribute</span> custPropOrigAttr = fieldNode.Attributes[custPropName, customNamespaceUri];</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#008000;">// should not be null, but we check it</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (custPropOrigAttr != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SetCustomProperty(custPropName, custPropOrigAttr.Value);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>After deploying my custom field, I’ve checked my field at the <em>Change Site Column</em> page and was happy to see my custom property values there:</p>
<p><a href="http://pholpar.files.wordpress.com/2011/10/image3.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://pholpar.files.wordpress.com/2011/10/image_thumb3.png?w=393&#038;h=449" width="393" height="449" /></a> </p>
<p>However, it turned out quickly that the schema of my field was not updated as expected:</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3d0b2ad1-7a6a-4447-927f-d8a3ab44d426" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Field</span><span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">ID</span><span style="color:#0000ff;">=</span>&quot;{<span style="color:#0000ff;">54634385-A8AC-4898-BF24-E533EB23444F}</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;<span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">DisplayName</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;<span style="color:#0000ff;"> </span></li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">StaticName</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;<span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Group</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">Grepton Fields</span>&quot;<span style="color:#0000ff;"> </span></li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">SPFieldRegEx</span>&quot;<span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Sealed</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">FALSE</span>&quot;<span style="color:#0000ff;"> </span></li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">AllowDeletion</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">TRUE</span>&quot;<span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">SourceID</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://schemas.microsoft.com/sharepoint/v3/fields</span>&quot;<span style="color:#0000ff;"> </span></li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Description</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">This is the RegEx field</span>&quot;<span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">grp:RegEx</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">[0-9]</span>&quot;<span style="color:#0000ff;"> </span></li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">grp:MaxLen</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">20</span>&quot;<span style="color:#0000ff;"> </span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">grp:ErrMsg</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">Error!</span>&quot;<span style="color:#0000ff;"> </span></li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:grp</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://schemas.grepton.com/sharepoint/</span>&quot;<span style="color:#0000ff;"> /&gt;</span></li>
</ol></div>
</p></div>
</p></div>
</p>
<p>Of course, pressing <em>OK</em> on the <em>Change Site Column</em> page updated the field schema with the custom properties:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1958147c-852d-4f5e-baaa-a38ea19a8ebf" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Field</span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">ID</span><span style="color:#0000ff;">=</span>&quot;{<span style="color:#0000ff;">54634385-A8AC-4898-BF24-E533EB23444F}</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">DisplayName</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">StaticName</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Group</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">Grepton Fields</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">SPFieldRegEx</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Sealed</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">FALSE</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">AllowDeletion</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">TRUE</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">SourceID</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://schemas.microsoft.com/sharepoint/v3/fields</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Description</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">This is the RegEx field</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">grp:RegEx</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">[0-9]</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">grp:MaxLen</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">20</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">grp:ErrMsg</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">Error!</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:grp</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://schemas.grepton.com/sharepoint/</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Required</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">FALSE</span>&quot;</li>
<li>&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">EnforceUniqueValues</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">FALSE</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Version</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">1</span>&quot;<span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Customization</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ArrayOfProperty</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span>RegEx<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Value</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:q1</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">p4:type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">q1:string</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:p4</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema-instance</span>&quot;<span style="color:#0000ff;">&gt;</span>[0-9]<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span>MaxLen<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Value</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:q2</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">p4:type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">q2:double</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:p4</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema-instance</span>&quot;<span style="color:#0000ff;">&gt;</span>20<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span>ErrMsg<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Value</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:q3</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">p4:type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">q3:string</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:p4</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema-instance</span>&quot;<span style="color:#0000ff;">&gt;</span>Error!<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ArrayOfProperty</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Customization</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Field</span><span style="color:#0000ff;">&gt;</span></li>
</ol></div>
</p></div>
</p></div>
</p>
</p>
<p>That gave me the idea that I should try to deploy my custom field using this schema. I removed my custom attributes from the <em>Field</em> element and used the following <em>XML</em> to deploy the field.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1fd17b02-0877-41ed-b6f1-3bf0c01b2bea" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">version</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">1.0</span>&quot;<span style="color:#0000ff;"> </span><span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">utf-8</span>&quot;<span style="color:#0000ff;"> ?&gt;</span></li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Elements</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://schemas.microsoft.com/sharepoint/</span>&quot;<span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Field</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">ID</span><span style="color:#0000ff;">=</span>&quot;{<span style="color:#0000ff;">54634385-A8AC-4898-BF24-E533EB23444F}</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">DisplayName</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">StaticName</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">RegExField</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Group</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">Grepton Fields</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">SPFieldRegEx</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Sealed</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">FALSE</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">AllowDeletion</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">TRUE</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">SourceID</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://schemas.microsoft.com/sharepoint/v3/fields</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Description</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">This is the RegEx field</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">Version</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">1</span>&quot;<span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Customization</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ArrayOfProperty</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span>RegEx<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Value</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:q1</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">p4:type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">q1:string</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:p4</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema-instance</span>&quot;<span style="color:#0000ff;">&gt;</span>[0-9]<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span>MaxLen<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Value</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:q2</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">p4:type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">q2:double</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:p4</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema-instance</span>&quot;<span style="color:#0000ff;">&gt;</span>20<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span>ErrMsg<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Name</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Value</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:q3</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema</span>&quot;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">p4:type</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">q3:string</span>&quot;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;"></span><span style="color:#ff0000;">xmlns:p4</span><span style="color:#0000ff;">=</span>&quot;<span style="color:#0000ff;">http://www.w3.org/2001/XMLSchema-instance</span>&quot;<span style="color:#0000ff;">&gt;</span>Error!<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Value</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Property</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ArrayOfProperty</span><span style="color:#0000ff;">&gt;</span></li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Customization</span><span style="color:#0000ff;">&gt;</span></li>
<li style="background:#f3f3f3;">&#160;&#160;<span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Field</span><span style="color:#0000ff;">&gt;</span></li>
<li><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Elements</span><span style="color:#0000ff;">&gt;</span></li>
</ol></div>
</p></div>
</p></div>
<p>I’ve removed my custom codes described above either, and tried to deploy the field.</p>
<p>Voilà! The field is deployed successfully, including the custom properties. At that point I really don’t understand if it would work for <em>WSS 3.0 </em>and if so, how I missed to find that solution three years ago. The main point it is working now and requires no hacking at all.</p>
<p>A mystical observation is that after deploying the field I was not able to get a reference for the new field using <em>PowerShell</em>:</p>
<p><em>$avFields = $web.AvailableFields     <br />$avFields[&quot;RegExField&quot;]</em></p>
<p>At the same time, the field is visible on the web UI, and the following <em>C# </em>code finds it:</p>
<p><em>SPFieldCollection avFields = web.AvailableFields;     <br />SPField field = avFields[&quot;RegExField&quot;];</em></p>
<p>If I create the field from code, <em>PowerShell</em> finds that either:</p>
<p><em>SPFieldCollection fields = web.Fields;     <br />SPField fieldCode = new SPField(fields, &quot;SPFieldRegEx&quot;, &quot;RegExCode&quot;);      <br />fields.Add(fieldCode);</em></p>
<p>If you know the reason for that, let me know, please!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/808/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/808/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/808/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/808/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/808/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/808/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/808/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/808/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=808&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/10/21/how-to-deploy-a-custom-field-with-custom-properties-from-a-feature-the-missing-second-part/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>

		<media:content url="http://pholpar.files.wordpress.com/2011/10/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating more advanced conditions for your ECB menus through jQuery and synchronous Client Object Model / WCF Data Services calls</title>
		<link>http://pholpar.wordpress.com/2011/10/16/creating-more-advanced-conditions-for-your-ecb-menus-through-jquery-and-synchronous-client-object-model-wcf-data-services-calls/</link>
		<comments>http://pholpar.wordpress.com/2011/10/16/creating-more-advanced-conditions-for-your-ecb-menus-through-jquery-and-synchronous-client-object-model-wcf-data-services-calls/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 21:00:21 +0000</pubDate>
		<dc:creator>Peter Holpar</dc:creator>
				<category><![CDATA[ECB menu]]></category>
		<category><![CDATA[ECMAScript Client Object Model]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[SP 2010]]></category>
		<category><![CDATA[WCF Data Services]]></category>

		<guid isPermaLink="false">https://pholpar.wordpress.com/2011/10/16/creating-more-advanced-conditions-for-your-ecb-menus-through-jquery-and-synchronous-client-object-model-wcf-data-services-calls/</guid>
		<description><![CDATA[In my last post I showed you how to alter the Edit Control Block (ECB) menu of SharePoint 2010 based on simple conditions. As I described, if you need to use more advanced conditions, for example based on list item field values or other information not available on the client side, you should apply a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=803&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://pholpar.wordpress.com/2011/10/16/disabling-item-deletion-at-the-sharepoint-user-interface/">my last post</a> I showed you how to alter the <em>Edit Control Block</em> (<em>ECB</em>) menu of <em>SharePoint 2010</em> based on simple conditions. As I described, if you need to use more advanced conditions, for example based on list item field values or other information not available on the client side, you should apply a few tricks or even hacks.</p>
<p>For these advanced solutions it is useful to provide the context to our <em>allowDeletion</em> method through the <em>ctx</em> parameter, so we add this one to the parameter list of the method.</p>
<p>It means that our original condition in method <em>AddListMenuItems</em> is changed from</p>
<p><em>if (allowDeletion(currentItemID))</em></p>
<p>to</p>
<p><em>if (allowDeletion(ctx, currentItemID))</em></p>
<p>In the simplest case, the information is already there at the page on client side, you simply have to find the way to get it. Checking the HTML source of the page or using the <em>Developer Tools </em>in the <em>Tools </em>menu of <em>Internet Explorer</em> (you can press <em>F12 </em>as a shortcut key) usually helps you to find the right track.</p>
<p>For example, if you need the title of the item, that is included in the <em>LinkTitle </em>field (as we have an <em>ECB</em> menu linked to the title) for each items. When looking up the right item, we should use the <em>ID</em> of the item (passed as the <em>itemID </em>parameter) and the <em>ID </em>of our list view (available in <em>ctx.ctxId</em>).</p>
<p><strong>Note:</strong> Although it should be evident, your page must load the <em>jQuery </em>library before you can use <em>jQuery </em>methods. Similarly, the <em>ECMAScript Client Object Model</em> library must be loaded before you reference its objects in methods later in the posts.</p>
<p>So if we would like to allow deletion only for items having title beginning with ‘<em>V</em>’ then we need to apply a method like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a907bed7-e359-4969-a759-229ddb86ffbe" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:200px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">function</span> allowDeletion(ctx, itemID) {</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> title = jQuery(<span style="color:#800000;">&#039;div[Field=&quot;LinkTitle&quot;][CTXName=&quot;ctx&#039;</span> + ctx.ctxId + <span style="color:#800000;">&#039;&quot;][id=&quot;&#039;</span> + itemID + <span style="color:#800000;">&#039;&quot;]&#039;</span>).text();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> (title.toUpperCase().startsWith(<span style="color:#800000;">&quot;V&quot;</span>));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>Next step is to see how to get the information if it is not available on the client side, but included in the list items on the server side. My first idea was to use the <em>ECMAScript Client Object Model</em>, however it turned out quickly that is not the best choice, as the <em>AddListMenuItems</em> method does not wait for the asynchronous reply supported by the client OM to be returned. Although I created a dirty workaround for that, I will show you that later.</p>
<p>Another alternative is to use <em>REST </em>protocol through the <em>WCF Data Services</em>.</p>
<p>As mentioned, our call must be synchronous, so we have to use <em>jQuery.ajax </em>specifying <em>async: false</em>. The next code snippet shows an example using the same condition applied above, that is title of the item must be started with ‘<em>V</em>’ to enable deletion.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9d68bff2-f857-4a55-99fb-8525d3a79687" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:300px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">function</span> allowDeletion(ctx, itemID) {</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;itemUrl = ctx.HttpRoot + <span style="color:#800000;">&quot;/_vti_bin/listdata.svc/&quot;</span> + ctx.ListTitle +<span style="color:#800000;">&quot;(&quot;</span> + itemID + <span style="color:#800000;">&quot;)?$select=Title&quot;</span>&#160;&#160;&#160;&#160;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> title = <span style="color:#800000;">&quot;&quot;</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;jQuery.ajax(</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;{</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;type: <span style="color:#800000;">&#039;GET&#039;</span>,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;url: itemUrl,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;dataType: <span style="color:#800000;">&#039;json&#039;</span>,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;success: <span style="color:#0000ff;">function</span> (result) {</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (result.isOk != <span style="color:#0000ff;">false</span>) {</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;title = result.d.Title;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;},</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;data: {}, </li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;async: <span style="color:#0000ff;">false</span></li>
<li>&#160;&#160;&#160;&#160;});</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> (title.toUpperCase().startsWith(<span style="color:#800000;">&quot;V&quot;</span>));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p><strong>Note:</strong> When working with <em>jQuery</em> and <em>WCF Data Services</em>, it is useful to know about the <em>parsererror</em> issue with field values containing apostrophe (single quote) and <a href="http://charliedigital.com/2011/07/25/jquery-parsererror-and-wcf-data-services/">how to fix it</a>.</p>
<p>It is important to note that you are not restricted to the actual list only. With a bit of additional complexity you can query related lists as well using $<em>expand</em> and the lookup field / user field IDs in the current list. If you are unsure how to compose the URL for you request, I suggest you to try to create the filter first in <em>C#</em> using the <em>LINQ</em> syntax, then use <em>Fiddler </em>to capture the request or apply <a href="http://blah.winsmarts.com/2011-10-Figuring_our_complex_REST_queries_for_SharePoint.aspx">this simple trick</a> from <em>Sahil Malik</em>.</p>
</p>
<p>If network bandwidth and speed are limited, then waiting for the synchronous result will cause issues via blocking the UI thread of the browser. If that is the case you should consider “pre-caching” the data required for checks on page load through a single request. If the item count of the list is limited, then you can cache all of the data (in our case, the IDs and the related titles of the items is needed), in the case of a larger list, you should get only the items displayed in the current page of the view. To get the IDs of the items on the page you should run a <em>jQuery</em> select similar to the one we used to get the title in the first example, and submit the <em>REST </em>request using a complex condition.</p>
<p>Last, I would like to show you my workaround for calling <em>ECMAScript Client Object Model </em>synchronously. As you might now, it is officially / theoretically not possible. I found that it can be done technologically, although I had to spend a few hours <em>Fiddler</em>ing and digging into the internals of the <em>ECMAScript Client OM</em> (<em>SP.Runtime.debug.js</em>), comparing the <em>JavaScript </em>methods to the ones in the managed <em>Client OM </em>classes using <em>Reflector</em>. If you demand more information about it I can give you more details, but now I publish it “as is”.</p>
<p>To build up the request I use the “traditional” <em>ECMAScript Client Object Model </em>(that, of course, should be already loaded before our script), but before sending it I get the built-up request in <em>XML</em> format from the internal methods of the OM, and send the request through <em>jQuery.ajax</em> in synchronous mode, just as like the case of <em>WCF Data Services</em>. The <em>JSON</em> result is “loaded” into <em>JavaScript</em> objects.</p>
<p>Be aware that the method described here don’t use the public, documented interfaces, so it is probably not supported by MS, and suggested to be used only as an experiment. There is no guarantee that it will work for you, especially if our environments are at different <em>SP </em>/ cumulative update level.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:656125e5-3fc4-4fdf-8219-6a652cfae37a" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;max-height:400px;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">function</span> allowDeletion(ctx, itemID) {</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> title = <span style="color:#800000;">&quot;&quot;</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;<span style="color:#0000ff;">try</span> {</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> context = SP.ClientContext.get_current();</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> web = context.get_web();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> selectedListId = SP.ListOperation.Selection.getSelectedList();</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> selectedListItem = web.get_lists().getById(selectedListId).getItemById(itemID);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;context.load(selectedListItem, <span style="color:#800000;">&quot;Title&quot;</span>);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#006400;">// start hacking</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> pendingRequest = context.get_pendingRequest();</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> webRequest = pendingRequest.get_webRequest();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#006400;">// get the request XML</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> body = pendingRequest.$24_0().toString();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#006400;">// get the URL of client.svc</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> url = webRequest.get_url();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#006400;">// &quot;initialize&quot; request</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SP.ClientRequest.$1T(webRequest);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#006400;">// we should add digest later to the request as an HTTP header (see below)</span></li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">var</span> digest = webRequest.get_headers()[<span style="color:#800000;">&#039;X-RequestDigest&#039;</span>];</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jQuery.ajax(</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;type: <span style="color:#800000;">&quot;POST&quot;</span>,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;data: body,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;url: ctx.HttpRoot + url,</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;success: <span style="color:#0000ff;">function</span> (result) {</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#0000ff;">if</span> (result.isOk != <span style="color:#0000ff;">false</span>) {</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;title = result[result.length - 1].Title;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;},</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;headers: {</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style="color:#800000;">&quot;x-requestdigest&quot;</span>: digest</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;},</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;contentType: <span style="color:#800000;">&quot;application/x-www-form-urlencoded&quot;</span>,</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;async: <span style="color:#0000ff;">false</span></li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&#160;&#160;&#160;&#160;}</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">catch</span> (e) {</li>
<li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;alert(<span style="color:#800000;">&quot;Error: &quot;</span> + e);</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&#160;&#160;&#160;&#160;<span style="color:#0000ff;">return</span> (title.toUpperCase().startsWith(<span style="color:#800000;">&quot;V&quot;</span>));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>BTW, the example above achieves the same, it allows deletion only for items having title beginning with ‘<em>V</em>’. Similarly to the former <em>WCF DS</em> example, the synchronous request through the network blocks the UI thread, and might require pre-caching in case of a slow network.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pholpar.wordpress.com/803/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pholpar.wordpress.com/803/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pholpar.wordpress.com/803/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pholpar.wordpress.com/803/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pholpar.wordpress.com/803/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pholpar.wordpress.com/803/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pholpar.wordpress.com/803/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pholpar.wordpress.com/803/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pholpar.wordpress.com&amp;blog=10111481&amp;post=803&amp;subd=pholpar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pholpar.wordpress.com/2011/10/16/creating-more-advanced-conditions-for-your-ecb-menus-through-jquery-and-synchronous-client-object-model-wcf-data-services-calls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/31db6921f6eddb38f5f86b0a220dbd58?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pholpar</media:title>
		</media:content>
	</item>
	</channel>
</rss>
