<?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>Raymond Little</title>
	<atom:link href="http://raymondlittle.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://raymondlittle.wordpress.com</link>
	<description></description>
	<lastBuildDate>Mon, 28 May 2012 08:06:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='raymondlittle.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Raymond Little</title>
		<link>http://raymondlittle.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://raymondlittle.wordpress.com/osd.xml" title="Raymond Little" />
	<atom:link rel='hub' href='http://raymondlittle.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Find Items in External List Programmatically</title>
		<link>http://raymondlittle.wordpress.com/2012/05/15/find-items-in-external-list-programmatically/</link>
		<comments>http://raymondlittle.wordpress.com/2012/05/15/find-items-in-external-list-programmatically/#comments</comments>
		<pubDate>Tue, 15 May 2012 21:16:48 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[BCS]]></category>
		<category><![CDATA[CAML]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=205</guid>
		<description><![CDATA[While working on a recent project I had to connect to an external DB to pull in some data and then use this in other lists in a SharePoint site. The obvious solution for this was to use BCS and set up an external content type to get the data from my DB. I had [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=205&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While working on a recent project I had to connect to an external DB to pull in some data and then use this in other lists in a SharePoint site. The obvious solution for this was to use BCS and set up an external content type to get the data from my DB. I had never setup BCS before so decided to use SharePoint Designer to set it up as this seemed the easiest way. It took a little trial and error, especially around permissions, but eventually I managed to get my list up and running displaying data. In addition I added a filter to my external content type to allow users to search on the data. I won’t go over how to add a filter as there are numerous articles on the web about how to do this, see examples below.</p>
<p><a title="http://msdn.microsoft.com/en-us/library/ff798274.aspx" href="http://msdn.microsoft.com/en-us/library/ff798274.aspx">http://msdn.microsoft.com/en-us/library/ff798274.aspx</a>     <br /><a title="http://www.lightningtools.com/blog/archive/2010/01/14/creating-comparison-and-wildcard-filters-for-bcs-in-sharepoint-designer.aspx" href="http://www.lightningtools.com/blog/archive/2010/01/14/creating-comparison-and-wildcard-filters-for-bcs-in-sharepoint-designer.aspx">http://www.lightningtools.com/blog/archive/2010/01/14/creating-comparison-and-wildcard-filters-for-bcs-in-sharepoint-designer.aspx</a></p>
<p>With the external list working I then created another list and within this I created a column which was an lookup to my external list. I think SharePoint must realise this is a special case as even though you add a column as a lookup to another list it changes the type to an External Data column. I was then able to add new items to my custom SharePoint list and associate them with data from my external DB.</p>
<p>This was all fine but I then need to migrate some of the customers existing data from an Excel SpreadSheet into SharePoint. Adding items into SharePoint is a fairly easy task and something I have done numerous times over the years but the one new section to this was how I would query the external list and then set the value of the external data column in my custom list.</p>
<p>After some research I found a lot of people suggesting that you could simply use CAML in the normal manner, however during my testing the external list always had 0 items. I then found an MSDN article on <a href="http://msdn.microsoft.com/en-us/library/ff798510.aspx">Using the BDC Object Model</a> so I decided to try this.</p>
<p>I copied the code, see full code below, from the Using Filters section of this article and added a reference for the BCS code, Microsoft.BusinessData.dll located in the ISAPI folder in the SharePoint root.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4001f9a1-1280-44cf-9472-cac73d715adb" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">MSDN Example Code</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#0000ff;">const</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;"> entityName = </span><span style="background:#ffffff;color:#800000;">&quot;Machines&quot;</span><span style="background:#ffffff;color:#000000;">;</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        </span><span style="background:#ffffff;color:#0000ff;">const</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;"> systemName = </span><span style="background:#ffffff;color:#800000;">&quot;PartsManagement&quot;</span><span style="background:#ffffff;color:#000000;">;</span></li>
<li><span style="background:#ffffff;color:#000000;">        </span><span style="background:#ffffff;color:#0000ff;">const</span><span style="background:#ffffff;color:#000000;"> </span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;"> nameSpace = </span><span style="background:#ffffff;color:#800000;">&quot;DataModels.ExternalData.PartsManagement&quot;</span><span style="background:#ffffff;color:#000000;">;</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        BdcService bdcService = SPFarm.Local.Services.GetValue&lt;BdcService&gt;();</span></li>
<li><span style="background:#ffffff;color:#000000;">        IMetadataCatalog catalog =</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">          bdcService.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);</span></li>
<li><span style="background:#ffffff;color:#000000;">        ILobSystemInstance lobSystemInstance =</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">          catalog.GetLobSystem(systemName).GetLobSystemInstances()[systemName];</span></li>
<li><span style="background:#ffffff;color:#000000;">        IEntity entity = catalog.GetEntity(nameSpace, entityName);</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        IFilterCollection filters = entity.GetDefaultFinderFilters();</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        </span><span style="background:#ffffff;color:#0000ff;">if</span><span style="background:#ffffff;color:#000000;"> (!</span><span style="background:#ffffff;color:#0000ff;">string</span><span style="background:#ffffff;color:#000000;">.IsNullOrEmpty(modelNumber))</span></li>
<li><span style="background:#ffffff;color:#000000;">        {</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">            WildcardFilter filter = (WildcardFilter)filters[0];</span></li>
<li><span style="background:#ffffff;color:#000000;">            filter.Value = modelNumber;</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        }</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        IEntityInstanceEnumerator enumerator =</span></li>
<li><span style="background:#ffffff;color:#000000;">          entity.FindFiltered(filters, lobSystemInstance);</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li><span style="background:#ffffff;color:#000000;">        entity.Catalog.Helper.CreateDataTable(enumerator);</span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>My first issue was I had no idea what the string variables at the top of the code should be set as. I tried debugging the code and I was able to find out the system name but it took some time. It turns out all the required details are in SharePoint Designer, see below. </p>
<ul>
<li>Entity name should be the name at the top of the external content type information </li>
<li>System name should be the external system at the bottom of the external content type information </li>
<li>nameSpace should be the Namespace in the middle of the external content type information </li>
</ul>
<p><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="SharePointDesignerExternalContentTypeDetails" border="0" alt="SharePointDesignerExternalContentTypeDetails" src="http://raymondlittle.files.wordpress.com/2012/05/sharepointdesignerexternalcontenttypedetails_thumb.png?w=509&h=160" width="509" height="160" /></p>
<p>Similarly to the example MSDN code I had setup one filter and this was a wildcard filter so I didn’t have to change that section of the code. All I changed was I set the filter value to be a value I was interested. When I ran the code everything seemed to be working as expected but I noticed if I was searching for ‘Test123’ and there was an item in the external list which matched exactly then it would return my result, however if I changed the code to search for ‘Test’ it didn’t find anything. I tried adding ‘*’ in various places to act as a wildcard but it made no difference to the results.</p>
<p>I expected since the filter was a wildcard filter I could search on only a part of the phrase and it would return what I was interested in but it wasn’t working. I checked the filtering functionality by adding a new item in my custom list and using the searching functionality for BCS and it worked in that I could search for only part of a phrase such as ‘Test’ and it would find partial matches like ‘Test123’. After confirming the out of the box functionality was working I rechecked my code but it seemed to match the example provided above. I then check and rechecked my settings in SharePoint Designer. but everything was as I would expect.</p>
<p>I then turned to a colleague <a href="http://mackenzie-sharepoint.blogspot.co.uk/">Ross MacKenzie</a> and we both went through the code together but even then we were unable to establish what was going wrong. He then suggested if I have tried ‘*’ around my search criteria why not try ‘%’ as this is the wildcard in SQL. After spending a good couple of hours looking about it finally started working with the code below. </p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:65d54e89-855a-49fb-8fcf-d671e7b772e2" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Correct Wildcard  Syntax</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;">WildcardFilter companyReferenceFilter = (WildcardFilter)filters[0];</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        companyReferenceFilter.Value = </span><span style="background:#ffffff;color:#2b91af;">String</span><span style="background:#ffffff;color:#000000;">.Format(</span><span style="background:#ffffff;color:#800000;">&quot;%{0}%&quot;</span><span style="background:#ffffff;color:#000000;">, siteRefCode.ToString());</span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h2>Conclusion</h2>
<p>I hope this helps others and stops them encountering the same issues as me as while the change to the example code was small it made a massive impact and from what I could see it wasn’t documented very well.</p>
<p>Happy SharePointing <img style="border-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://raymondlittle.files.wordpress.com/2012/05/wlemoticon-smile1.png?w=450" /></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/205/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=205&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/05/15/find-items-in-external-list-programmatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/05/sharepointdesignerexternalcontenttypedetails_thumb.png" medium="image">
			<media:title type="html">SharePointDesignerExternalContentTypeDetails</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/05/wlemoticon-smile1.png" medium="image">
			<media:title type="html">Smile</media:title>
		</media:content>
	</item>
		<item>
		<title>CAML Query Join Lists</title>
		<link>http://raymondlittle.wordpress.com/2012/05/11/caml-query-join-lists/</link>
		<comments>http://raymondlittle.wordpress.com/2012/05/11/caml-query-join-lists/#comments</comments>
		<pubDate>Fri, 11 May 2012 15:21:30 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[CAML]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=200</guid>
		<description><![CDATA[I was in a situation where I had a parent list with some details and then a separate list for child items. The child list had a lookup to the parent list to make the association between the two. I needed a query which allowed me to search on some fields in the parent list [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=200&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was in a situation where I had a parent list with some details and then a separate list for child items. The child list had a lookup to the parent list to make the association between the two. I needed a query which allowed me to search on some fields in the parent list and some fields in the child list.</p>
<h2>Approach</h2>
<p>When I was thinking about this I came up with a few different ideas which are each discussed below:</p>
<ol>
<li>Multiple Caml Queries. One option was I could run a Caml query against the parent list to get the items which meet the parent item criteria and then do another Caml query looking for items in the child list which are associated to the items returned in the parent Caml query but also meet the filtering on the child list. Obviously this is not idea as it would involve a decent amount of code to do but would also be fairly inefficient as well. </li>
<li>Linq to SharePoint. While this would probably have been the easiest approach as you can very quickly join two lists using Linq, once you have your entry classes generated,&#160; but I decided against this approach. My reasoning was based around a solution design perspective. I decided that while Linq to SharePoint would prove beneficial in this situation it would require adding and maintain another element in the solution thus increasing the complexity.</li>
<li>Join Caml Query. This was something I had never done before but I was aware it was one of the new features in SharePoint. I done some research and it seemed like a fairly straightforward approach so I decided to go with this. </li>
</ol>
<p>&#160;</p>
<h2>Method</h2>
<p>There are various articles out there on how to join lists via a Caml query so I won’t go into a great deal of detail only the points I found interesting.</p>
<p>The first thing is the Caml query will be created as you normally would be it needs to be run against the child list not the parent list.</p>
<p>In order to map the lists together you use the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.joins">Joins</a> property of the SPQuery object. As I mentioned there is a lot of information out there for more complicated joins but in my situation it was simply joining parent and child lists together, see example below.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bbcd0749-eb18-4bc5-82ce-43d5ad195679" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Join Lists</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;">StringBuilder joinDetails = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> StringBuilder();</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        joinDetails.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Join Type=&#039;INNER&#039; ListAlias=&#039;ParentListName&#039;&gt;&lt;Eq&gt;&lt;FieldRef Name=&#039;ChildListLookupColumnName&#039; RefType=&#039;Id&#039;/&gt;&lt;FieldRef List=&#039;ParentListName&#039; Name=&#039;ID&#039;/&gt;&lt;/Eq&gt;&lt;/Join&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);           </span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>With the join created the next step is to set up the <a href="http://msdn.microsoft.com/en-us/library/ee539975%28office.14%29.aspx">projected fields</a>, these are the fields from the parent list which you want to display or filter against in the where clause, see below. The Name element is any name you want to give it and this will be used in the view fields and query sections of the SPQuery object. As far as I can see the type is always Lookup. The ShowField element is the name of the field in the parent list you want to map to this new field.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fbda5f1d-078d-497a-9f94-31c0eb226806" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Projected Fields</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;">StringBuilder projectedFields = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> StringBuilder();</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        projectedFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Field Name=&#039;ParentListField1&#039; Type=&#039;Lookup&#039; List=&#039;ParentListName&#039; ShowField=&#039;Field1&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li><span style="background:#ffffff;color:#000000;">        projectedFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Field Name=&#039;ParentListField2&#039; Type=&#039;Lookup&#039; List=&#039;ParentListName&#039; ShowField=&#039;Field2&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        projectedFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Field Name=&#039;ParentListField3&#039; Type=&#039;Lookup&#039; List=&#039;ParentListName&#039; ShowField=&#039;Field3&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li><span style="background:#ffffff;color:#000000;">        projectedFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Field Name=&#039;ParentListField4&#039; Type=&#039;Lookup&#039; List=&#039;ParentListName&#039; ShowField=&#039;Field4&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        projectedFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Field Name=&#039;ParentListField5&#039; Type=&#039;Lookup&#039; List=&#039;ParentListName&#039; ShowField=&#039;Field5&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li><span style="background:#ffffff;color:#000000;">        projectedFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Field Name=&#039;ParentListField6&#039; Type=&#039;Lookup&#039; List=&#039;ParentListName&#039; ShowField=&#039;Field6&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>You must ensure all projected fields are listed in the view fields SPQuery property. Again anyone who has done basic Caml queries will have seen this before the only consideration is when using fields from the parent list you have to use the name set in the projected fields not the name of the column in the parent list, see below.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:25570a2c-8ac2-4978-9ae0-4274bca37b19" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">View Fields</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;">StringBuilder viewFields = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> StringBuilder();</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        viewFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;FieldRef Name=&#039;ParentListField1&#039;/&gt;&lt;FieldRef Name=&#039;ParentListField2&#039;/&gt;&lt;FieldRef Name=&#039;ParentListField3&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
<li><span style="background:#ffffff;color:#000000;">        viewFields.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;FieldRef Name=&#039;ParentListField4&#039;/&gt;&lt;FieldRef Name=&#039;ParentListField5&#039;/&gt;&lt;FieldRef Name=&#039;ParentListField6&#039;/&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
</ol></div>
</p></div>
</p></div>
<p>With the join between the lists done and the mapping for the fields in the parent list you can then write your Caml query as per normal and filter against details in the parent list, see below for a simple example Caml query.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9c0ac567-de66-44a1-8fd9-f13af2429cdc" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Example Caml Query</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;">sb.Append(</span><span style="background:#ffffff;color:#800000;">&quot;&lt;Where&gt;&lt;IsNull&gt;&lt;FieldRef Name=&#039;ParentListField1&#039; /&gt;&lt;/IsNull&gt;&lt;/Where&gt;&quot;</span><span style="background:#ffffff;color:#000000;">);</span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>The final element is to associate all these with your SPQuery object and then pass this to the list GetItems method.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8217d755-33fa-42a5-84ea-505e0b0ce42c" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Associated with SPQuery</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#0000ff;">var</span><span style="background:#ffffff;color:#000000;"> query = </span><span style="background:#ffffff;color:#0000ff;">new</span><span style="background:#ffffff;color:#000000;"> SPQuery();</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        query.Joins = joinDetails.ToString();</span></li>
<li><span style="background:#ffffff;color:#000000;">        query.ProjectedFields = projectedFields.ToString();</span></li>
<li style="background:#f3f3f3;"><span style="background:#ffffff;color:#000000;">        query.ViewFields = viewFields.ToString();</span></li>
<li><span style="background:#ffffff;color:#000000;">        query.Query = sb.ToString();</span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>Getting to this point did take some changing of the various Caml query properties and it was slightly frustrating but I don’t think this is particularly related to joins within Caml but more of a general issue with Caml.</p>
<h2>Issues</h2>
<p>There was one issue which seems particularly related to joins and this was when trying to get a DataTable of the results instead of a SPListItemCollection, see example below. This throws a NullReferenceException, see stack trace below, and it seems the only way around this is to work with the SPListItemCollection and not a DataTable</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:21f863b8-f481-4413-b7d5-f9600456e8f1" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Get DataTable</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="background:#ffffff;color:#000000;">var queryResults = list.GetItems(query).GetDataTable();  </span></li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h5>Stack Trace</h5>
<p>The error was System.NullReferenceException: Object reference not set to an instance of an object.&#160;&#160;&#160;&#160; at Microsoft.SharePoint.SPFieldMap.EnsureFieldArray()&#160;&#160;&#160;&#160; at Microsoft.SharePoint.SPFieldMap.GetFieldObject(Int32 columnNumber)&#160;&#160;&#160;&#160; at Microsoft.SharePoint.SPListItemCollection.GetVisibleFieldIndices(Boolean isJsGrid, Int32[]&amp; arrVisibleFieldIndices, Int32&amp; iVisibleFieldCount)&#160;&#160;&#160;&#160; at Microsoft.SharePoint.SPListItemCollection.GetDataTableCore(Boolean isJsGrid)</p>
<h2>Conclusion</h2>
<p>I think that the join functionality in SharePoint 2010 is a useful feature, however given the fiddly nature of this along with how easy it is to accomplish the same thing using Linq to SharePoint will mean a lot of people won’t use this approach. In my circumstances it works very well and with a few extra lines of code, compared to a normal Caml Query, gives me a lot of extra functionality.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/200/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=200&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/05/11/caml-query-join-lists/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>
	</item>
		<item>
		<title>Incorrect date format when using SharePoint DateTimeControl in application page launched via ModalDialog</title>
		<link>http://raymondlittle.wordpress.com/2012/05/02/incorrect-date-format-when-using-sharepoint-datetimecontrol-in-application-page-launched-via-modaldialog/</link>
		<comments>http://raymondlittle.wordpress.com/2012/05/02/incorrect-date-format-when-using-sharepoint-datetimecontrol-in-application-page-launched-via-modaldialog/#comments</comments>
		<pubDate>Wed, 02 May 2012 16:00:20 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=198</guid>
		<description><![CDATA[I was working on a project where I needed to create a custom UI for a customer in order to do some complex logic and form formatting. The form was developed as an application page which was deployed into the 14 hive and it was presented to the user via a Popup using the JavaScript [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=198&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was working on a project where I needed to create a custom UI for a customer in order to do some complex logic and form formatting. The form was developed as an application page which was deployed into the 14 hive and it was presented to the user via a Popup using the JavaScript ModalDialog functionality. The modal dialog was being launched from a custom web part which was in a subsite below the main site</p>
<p>As part of the form I needed to prompt the user with a field which would capture a date so I used the SharePoint DateTimeControl under the Microsoft.SharePoint.WebControls namespace. I have used this several times before so I didn’t think anything of it until when testing I noticed the date format was US instead of UK. I have had this before so I added some code in the page load of my application page to set the controls regional settings to match the current web regional settings, see below.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:37d258bd-ee1e-47f2-b7ea-532c284a0e14" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Set DateTimeControl Region</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;">
<li>DtDueDate.LocaleId = <span style="color:#2b91af;">Convert</span>.ToInt32(<span style="color:#2b91af;">SPContext</span>.Current.Web.RegionalSettings.LocaleId);</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>This normally works, however when I started testing it was still not setting the region to UK, 2057, instead it was still set to US, 1033. While debugging I noticed the SPContext details where indicating the current web was the top level web not the subsite from which the modal dialog was getting launched. </p>
<p>This explained why the DateTimeControl was displaying in the wrong format as the regional settings at the top level site where set to US. I changed the regional settings at the top level web and the control starting displaying in UK format.</p>
<p>While I was happy this was working I was slightly confused as to why the context was showing the current web to be the root web not the web for the subsite. After some digging around I realised that while I was on the subsite in the main browser window when I was calling the popup I was passing in a relative URL, see examples below, and this meant the context in the popup used this URL not the location from which it was getting called.</p>
<p><strong>Original URL of popup window where context is the root site</strong></p>
<p>“/_layouts/ApplicationPages/Page.aspx”</p>
<p><strong>Adjusted URL of popup window where context is the subsite</strong></p>
<p>“/subsite/_layouts/ApplicationPages/Page.aspx”</p>
<p>This caused me some issues but once I got my head around it it made perfect sense so hopefully this will help others or help me in the future when I forget all about it <img style="border-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://raymondlittle.files.wordpress.com/2012/05/wlemoticon-smile.png?w=450" /></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/198/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=198&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/05/02/incorrect-date-format-when-using-sharepoint-datetimecontrol-in-application-page-launched-via-modaldialog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/05/wlemoticon-smile.png" medium="image">
			<media:title type="html">Smile</media:title>
		</media:content>
	</item>
		<item>
		<title>Generate WSP and coy to another location on post build event in Visual Studio 2010</title>
		<link>http://raymondlittle.wordpress.com/2012/04/19/generate-wsp-and-coy-to-another-location-on-post-build-event-in-visual-studio-2010/</link>
		<comments>http://raymondlittle.wordpress.com/2012/04/19/generate-wsp-and-coy-to-another-location-on-post-build-event-in-visual-studio-2010/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 10:46:22 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Post Build Commands]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=192</guid>
		<description><![CDATA[When working on all SharePoint 2010 projects deployments are generally done via PowerShell scripts. I usually have a set PowerShell script which I copy and alter updating items such as the site URL, solution name and feature ID and I save this in a deployment folder under where the solution file is located. I then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=192&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When working on all SharePoint 2010 projects deployments are generally done via PowerShell scripts. I usually have a set PowerShell script which I copy and alter updating items such as the site URL, solution name and feature ID and I save this in a deployment folder under where the solution file is located. I then create a solution folder in Visual Studio called ‘Deployment files’ and add my PowerShell scripts to this. This way the deployment files are part of the solution and should be added into our code repository as well. </p>
<p>In my PowerShell scripts I don’t hard coded the WSP location I find the current directory from which the script is getting executed and then append the WSP name, see below. This means the WSP has to be in the same folder as the scripts which isn’t generally an issue but it means I have to package the project in Visual Studio and then copy the WSP from the build folder to my scripts folder. While this only takes a few minutes you need to do it each time which can get a bit repetitive.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e50f6500-99f8-4869-b4cd-be50ee491700" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Find Solution Location</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li>$scriptpath = $MyInvocation.MyCommand.Path</li>
<li style="background:#f3f3f3;">$dir = Split-Path $scriptpath</li>
<li>$solutionName=&quot;WSPNAME.wsp&quot;</li>
<li style="background:#f3f3f3;">$solutionPath = $dir + &quot;&#092;&quot; + $solutionName</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>I decided to look into using post build commands to see if I could generate the WSP then copy it to the folder where my scripts are located. Since I have done it several times I first looked at copying the WSP file to another location. This can be done by</p>
<ol>
<li>Right click on the project and select properties</li>
</ol>
<p><a href="http://raymondlittle.files.wordpress.com/2012/04/projectpropertieswindow.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="ProjectPropertiesWindow" border="0" alt="ProjectPropertiesWindow" src="http://raymondlittle.files.wordpress.com/2012/04/projectpropertieswindow_thumb.png?w=419&h=241" width="419" height="241" /></a></p>
<ol>
<li>Next select the Build Events option on the RHS</li>
<li>In the post-build event command line window enter the script below</li>
</ol>
<p>copy $(TargetDir)$(TargetName).wsp $(SolutionDir)DeploymentFiles</p>
<ol>
<ol>
<li>The <strong>TargetDir</strong> should be the full path to the build folder where the WSP will be created</li>
<li>The <strong>TargetName</strong> should be the same name as the project so in my case if the project name was TestProject I would be looking for a file called TestProject.wsp</li>
<li>The <strong>SolutionDir</strong> should be the full path to the folder where the solution file is located.</li>
<li>\DeploymentFiles is simply the name of the folder located under the main solution folder where I keep my PowerShell scripts</li>
</ol>
</ol>
<p>3. Save the file and build the project</p>
<p>If it has worked you should get a message in the Output screen indicating the file has been copied.</p>
<p>This was fine except this simply copies the WSP from the bin folder to my deployment scripts folder but what I need is to ensure I am getting the latest version of the WSP so I need to generate the WSP before copying the file. As this was not something I had done before I done some research and it seems the only way to do this is to edit the project file.</p>
<p>I opened my project file in Notepad++, might be worth taking a backup of this first, and found the section at the end, see below, and added the suggested XML, see below.</p>
<p><strong>Section After which I need to add my addition XML</strong>    <br />&lt;Import Project=&quot;$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets&quot; /&gt;</p>
<p><strong>Required XML     <br /></strong>&lt;PropertyGroup&gt;    <br />&#160; &lt;BuildDependsOn&gt;$(BuildDependsOn);CreatePackage&lt;/BuildDependsOn&gt;    <br />&lt;/PropertyGroup&gt;”</p>
<p>What I found was as I had already added my post build event to copy the WSP there was already a section in the project file called “PropertyGroup”, see below.</p>
<p><a href="http://raymondlittle.files.wordpress.com/2012/04/postbuildcopyprojectxml.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="PostBuildCopyProjectXML" border="0" alt="PostBuildCopyProjectXML" src="http://raymondlittle.files.wordpress.com/2012/04/postbuildcopyprojectxml_thumb.png?w=524&h=83" width="524" height="83" /></a></p>
<p>At this point I wasn’t sure if I was supposed to add a new PropertyGroup element or add my BuildDependsOn section to the existing property group. I looked around on Google but there wasn’t much details on this so added the section to generate the WSP as another PropertyGroup element but I added this before my copy build event in case the order was important. When I opened Visual Studio, or if you already had it opened you will need to reload the project, and build it I found it did copy the file and it did build the WSP it did them in the wrong order. It first copied the WSP then it built a new version of the WSP. </p>
<p>Obviously this isn’t much use as I would always end up with an old version of the WSP. I tried adding the post build code which moves the file into the same property group, see below, but I still ended up with the same outcome where the file was moved first then a new version created.</p>
<p><strong>XML with generate new WSP and move file in one property group in project file     <br /><a href="http://raymondlittle.files.wordpress.com/2012/04/postbuildandgeneratewspprojectxml.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="PostBuildAndGenerateWSPProjectXML" border="0" alt="PostBuildAndGenerateWSPProjectXML" src="http://raymondlittle.files.wordpress.com/2012/04/postbuildandgeneratewspprojectxml_thumb.png?w=518&h=80" width="518" height="80" /></a></strong></p>
<p>I spent some time researching this issue on Google and found an article on <a href="http://social.technet.microsoft.com/Forums/ar/sharepoint2010programming/thread/5b456474-4844-4dd5-9216-c9cc98bbf802">how to generate a WSP in post build command</a> and noticed that I had “&lt;BuildDependsOn&gt;” in my version but in the article above it was “&lt;PostBuildEventDependsOn&gt;”. As soon as I changed this, see new project XML below, things executed in the correct order and it first built my WSP then copied it.</p>
<p> <strong>Final version of project file XML   <br /></strong>
<p>&lt;PropertyGroup&gt;&#160; &lt;PostBuildEventDependsOn&gt;$(PostBuildEventDependsOn);CreatePackage&lt;/PostBuildEventDependsOn&gt;   <br />&#160; &lt;PostBuildEvent&gt;copy $(TargetDir)$(TargetName).wsp $(SolutionDir)DeploymentFiles&lt;/PostBuildEvent&gt;    <br />&lt;/PropertyGroup&gt;</p>
<p>I hope this will help others as it took me a while to get this right. As always please be careful with making changes to the project file as this can cause issues.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/192/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=192&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/04/19/generate-wsp-and-coy-to-another-location-on-post-build-event-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/04/projectpropertieswindow_thumb.png" medium="image">
			<media:title type="html">ProjectPropertiesWindow</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/04/postbuildcopyprojectxml_thumb.png" medium="image">
			<media:title type="html">PostBuildCopyProjectXML</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/04/postbuildandgeneratewspprojectxml_thumb.png" medium="image">
			<media:title type="html">PostBuildAndGenerateWSPProjectXML</media:title>
		</media:content>
	</item>
		<item>
		<title>JQuery to see if the user is on either the display, edit or new list item form</title>
		<link>http://raymondlittle.wordpress.com/2012/04/18/jquery-to-see-if-the-user-is-on-either-the-display-edit-or-new-list-item-form/</link>
		<comments>http://raymondlittle.wordpress.com/2012/04/18/jquery-to-see-if-the-user-is-on-either-the-display-edit-or-new-list-item-form/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 15:38:27 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=183</guid>
		<description><![CDATA[There have been a few situations where I have wanted to run some JQuery on either a display, edit or new item form to change the layout of the pages or hide certain fields. Typically I done this by checking the URL to see if I’m on the list in question, see example below, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=183&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There have been a few situations where I have wanted to run some JQuery on either a display, edit or new item form to change the layout of the pages or hide certain fields. Typically I done this by checking the URL to see if I’m on the list in question, see example below, and then running my JQuery.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:15613343-f55a-4f7e-b702-6e8a3aa3cc97" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">if</span> (window.location.href.indexOf(<span style="color:#800000;">&#039;/Lists/TestListName/&#039;</span>) != -1) {</li>
<li style="background:#f3f3f3;">        alert(<span style="color:#800000;">&#039;Am in list&#039;</span>);</li>
<li>        }</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>This works fine but it results in the above condition generally being true when you are interacting with the list i.e. looking at a particular view. I decided to take a look around to see if there was anything out there which would allow me to see if I was on one of the list forms but I didn’t find anything.</p>
<p>I spent some time looking at the HTML of the list forms to see if there was any way I could accurately check which page I was on. In the end I decided I would use the ribbon as part of the way I would target the type of form along with the breadcrumb. When checking the HTML I noticed the display form had an element for the ribbon which had an ID of ‘Ribbon.ListForm.Display’. From my previous experience of working with the ribbon I knew this was a relatively safe way of checking as the element wouldn’t appear on any other pages as it was particularly targeted at List Forms ribbon options, see function below.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1ef637d8-893a-4692-ba06-c8761485e67b" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Check if display form</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">function</span> IsDisplayForm()</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">var</span> isDisplayForm = <span style="color:#0000ff;">false</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#0000ff;">var</span> ribbonLiElement = document.getElementById(<span style="color:#800000;">&quot;Ribbon.ListForm.Display&quot;</span>);    </li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">if</span> ($(ribbonLiElement).length &gt; 0) {</li>
<li>        isDisplayForm = <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">    }            </li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">return</span> isDisplayForm;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>While this worked for display forms the solution for the edit and the new forms was slightly more complex as while both of these have a ribbon element they are both ‘Ribbon.ListForm.Edit’. This meant I needed another way to be able to distinguish between a new and an edit form. After comparing the HTML I decided to use the current breadcrumb node as the text for the two types of forms is slightly different. For the new forms it is ‘New Item’ and for the edit form it is ‘Edit Item’, see code below.</p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b96e90c8-f048-4827-bba6-66d3ee43fccc" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Check if edit form</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">function</span> IsEditForm() {</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">var</span> isEditForm = <span style="color:#0000ff;">false</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">var</span> ribbonLiElement = document.getElementById(<span style="color:#800000;">&quot;Ribbon.ListForm.Edit&quot;</span>);</li>
<li>    <span style="color:#0000ff;">var</span> currentBreadcrumbElement = $(<span style="color:#800000;">&quot;span.s4-breadcrumbCurrentNode&quot;</span>);    </li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">if</span> (currentBreadcrumbElement.length &gt; 0 &amp;&amp; currentBreadcrumbElement.text().toLowerCase() == <span style="color:#800000;">&quot;edit item&quot;</span></li>
<li>    &amp;&amp; $(ribbonLiElement).length &gt; 0) {</li>
<li style="background:#f3f3f3;">        isEditForm = <span style="color:#0000ff;">true</span>;</li>
<li>    } </li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#0000ff;">return</span> isEditForm;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>
<div style="margin:0;display:inline;float:none;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:555dd93a-11e2-4477-989b-aee06657012c" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Check if new form</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;">
<li><span style="color:#0000ff;">function</span> IsNewForm() {</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">var</span> isNewForm = <span style="color:#0000ff;">false</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">var</span> ribbonLiElement = document.getElementById(<span style="color:#800000;">&quot;Ribbon.ListForm.Edit&quot;</span>);</li>
<li>    <span style="color:#0000ff;">var</span> currentBreadcrumbElement = $(<span style="color:#800000;">&quot;span.s4-breadcrumbCurrentNode&quot;</span>);        </li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">if</span> (currentBreadcrumbElement.length &gt; 0 &amp;&amp; currentBreadcrumbElement.text().toLowerCase() == <span style="color:#800000;">&quot;new item&quot;</span></li>
<li>    &amp;&amp; $(ribbonLiElement).length &gt; 0) {</li>
<li style="background:#f3f3f3;">        isNewForm = <span style="color:#0000ff;">true</span>;</li>
<li>    }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#0000ff;">return</span> isNewForm;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p><strong>Conclusion</strong></p>
<p>The easy option for this may have been to simply check the URL for NewForm.aspx, DispForm.aspx&#160; or EditForm.aspx but I decided it would be better not to use this approach as it is possible to create custom versions of these forms which would have different names. </p>
<p>The one area I have tested where this will not work is if you create a custom InfoPath form using the option in the list ribbon so this is worth keeping in mind.</p>
<p>The other limitation is I have hardcoded the text I’m testing against so this will only work in English but it could be change for other languages.</p>
<p>As always please check this works on a development environment and is tested thoroughly before using. Fingers crossed this will help some people.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/183/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=183&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/04/18/jquery-to-see-if-the-user-is-on-either-the-display-edit-or-new-list-item-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>
	</item>
		<item>
		<title>InfoPath 2010 Security Exceptions</title>
		<link>http://raymondlittle.wordpress.com/2012/03/14/infopath-2010-security-exceptions/</link>
		<comments>http://raymondlittle.wordpress.com/2012/03/14/infopath-2010-security-exceptions/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 16:12:15 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[CRM 2011]]></category>
		<category><![CDATA[InfoPath 2010]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=178</guid>
		<description><![CDATA[While working on a recent project I had to create an InfoPath 2010 form which took some parameters and queried CRM to retrieve additional details. At first I tried to setup a new Web Service data connection but I kept getting errors so instead decided to handle it all in code on the page load. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=178&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While working on a recent project I had to create an InfoPath 2010 form which took some parameters and queried CRM to retrieve additional details. At first I tried to setup a new Web Service data connection but I kept getting errors so instead decided to handle it all in code on the page load. </p>
<p>Anyone who has developed an InfoPath form with code behind knows it can be a very frustrating process to write the code and debug issues. In addition the deployment process can take a while which results in the whole processes being very time consuming. To get around this I decided there was no reason why I couldn’t create a Windows Forms application and use it as a test harness for the code.</p>
<p>I created a new Windows Forms Application, designed my UI, added my CRM references and implemented the code. After a few iterations of testing I was successfully able to pull information back from a development CRM instance. I then moved my code into the InfoPath form, made the required changes and deployed it. As with all development I was hopeful this would work first time around but it didn’t <img style="border-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://raymondlittle.files.wordpress.com/2012/03/wlemoticon-smile.png?w=450" />. I looked at the ULS logs on the SharePoint server and noticed the error below. </p>
<h2>Error</h2>
<p>System.Security.SecurityException: Request for the permission of type &#8216;System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&#8242; failed.</p>
<p>&#160;</p>
<p>After much looking around and testing I was unable to establish what was causing the issue. I checked my original application and it worked and compared the code with the InfoPath version but I was unable to see any major differences. After checking the code and when I was happy it was not a code issue my attention turned to what security issues could be causing this error. Immediately it suddenly occurred to me that I hadn’t changed the Security Level option in the form settings, <a href="https://raymondlittle.wordpress.com/2012/03/14/adjust-infopath-2010-security-settings/">steps to adjust InfoPath Security settings</a>. To be honest this probably should have been my first port of call but fingers cross this article will help someone else or at least act as a reminder for me <img style="border-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://raymondlittle.files.wordpress.com/2012/03/wlemoticon-smile.png?w=450" /></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=178&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/03/14/infopath-2010-security-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/03/wlemoticon-smile.png" medium="image">
			<media:title type="html">Smile</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/03/wlemoticon-smile.png" medium="image">
			<media:title type="html">Smile</media:title>
		</media:content>
	</item>
		<item>
		<title>Adjust InfoPath 2010 Security Settings</title>
		<link>http://raymondlittle.wordpress.com/2012/03/14/adjust-infopath-2010-security-settings/</link>
		<comments>http://raymondlittle.wordpress.com/2012/03/14/adjust-infopath-2010-security-settings/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 16:10:22 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[InfoPath 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=175</guid>
		<description><![CDATA[Only set forms you have developed to Full Trust as doing this can leave you open to potential security issues. Open form in design mode Click on the ‘File’ tab In the ‘Form Information Section’ click on ‘Advanced form options’ &#160; In the new window click on ‘Security and Trust’ in the LHS Deselect ‘Automatically [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=175&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Only set forms you have developed to Full Trust as doing this can leave you open to potential security issues.</p>
<ul>
<li>Open form in design mode</li>
<li>Click on the ‘File’ tab</li>
<li>In the ‘Form Information Section’ click on ‘Advanced form options’</li>
</ul>
<p> <a href="http://raymondlittle.files.wordpress.com/2012/03/infopathformoptiosn_thumb1.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;margin:0;" title="InfoPathFormOptiosn_thumb[1]" border="0" alt="InfoPathFormOptiosn_thumb[1]" src="http://raymondlittle.files.wordpress.com/2012/03/infopathformoptiosn_thumb1_thumb.png?w=384&h=322" width="384" height="322" /></a>
<p>&#160;</p>
<ul>
<li>In the new window click on ‘Security and Trust’ in the LHS</li>
<li>Deselect ‘Automatically determine security level’</li>
<li>Select the ‘Full Trust’ option</li>
</ul>
<p><a href="http://raymondlittle.files.wordpress.com/2012/03/infopathformoptionspopup_thumb1.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="InfoPathFormOptionsPopup_thumb[1]" border="0" alt="InfoPathFormOptionsPopup_thumb[1]" src="http://raymondlittle.files.wordpress.com/2012/03/infopathformoptionspopup_thumb1_thumb.png?w=381&h=273" width="381" height="273" /></a></p>
<ul>
<li>Click ok</li>
<li>Publish and redeploy your form</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/175/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=175&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/03/14/adjust-infopath-2010-security-settings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/03/infopathformoptiosn_thumb1_thumb.png" medium="image">
			<media:title type="html">InfoPathFormOptiosn_thumb[1]</media:title>
		</media:content>

		<media:content url="http://raymondlittle.files.wordpress.com/2012/03/infopathformoptionspopup_thumb1_thumb.png" medium="image">
			<media:title type="html">InfoPathFormOptionsPopup_thumb[1]</media:title>
		</media:content>
	</item>
		<item>
		<title>Archiving documents timer job</title>
		<link>http://raymondlittle.wordpress.com/2012/02/07/archiving-timer-job/</link>
		<comments>http://raymondlittle.wordpress.com/2012/02/07/archiving-timer-job/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 15:25:20 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[InfoPath 2007]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Timer Jobs]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=167</guid>
		<description><![CDATA[Whilst working a recent project I had to create a timer job which moved some InfoPath forms from an archive document library to a location which was dynamically generated depending on the forms modified date. I am not going to cover the basics on how to create a timer job as Andrew Connell already has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=167&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Whilst working a recent project I had to create a timer job which moved some InfoPath forms from an archive document library to a location which was dynamically generated depending on the forms modified date. I am not going to cover the basics on how to create a timer job as <a href="http://www.andrewconnell.com">Andrew Connell</a> already has a very good article on how to <a href="http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx">create a custom timer job</a>.</p>
<p>In this article I will focus on how to move a InfoPath form but it could equally to word documents, PDF, etc. The same could apply to list data but it will require some code changes. </p>
<p>As with all my projects I first mapped out what steps, see below.</p>
<ol>
<li>Get configuration data</li>
<li>Open the site in which my forms are stored</li>
<li>Get the document library </li>
<li>Get the items which meet a certain criteria</li>
<li>Iterate through the items and for each item perform the following</li>
<ol>
<li>Get the form modified date</li>
<li>Check and see if a document library already exists for the current year and if not create one</li>
<li>In the document library check if a folder for the month exists and if not create it</li>
<li>Copy the form to the new location</li>
<li>Set the metadata to be copied across. One consideration that may apply to others is version, however this was not relevant for me. </li>
<li>Delete the original</li>
</ol>
</ol>
<p>I will assume you have read the post above by Andrew Connell so I will jump in once we have already got our site collection. </p>
<h2>Point 1</h2>
<p>As with all projects you want to minimise the amount of data which is hard coded as this reduces the need for additional deployments when certain environment specific variables change. There are a few options on where to store these but I decided to store mine in the web.config of the web application. This requires a few steps to read the data from the web.config so I created a helper method which returned a custom class which is used to store the variables.&#160; </p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:397bfb44-9f40-4222-8649-05bba8614dfd" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#2b91af;">ConfigurationData</span> config = GetConfigurationData(webApplication.Name);</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b7a6b6a2-216c-4a27-9756-6a3956c4883c" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Method to get the configuration data from the web.config </span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;webAppName&quot;&gt;</span><span style="color:#008000;">A string of the web application name</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;returns&gt;</span><span style="color:#008000;">An ConfigurationData object with the configuration details</span><span style="color:#808080;">&lt;/returns&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#2b91af;">ConfigurationData</span> GetConfigurationData(<span style="color:#0000ff;">string</span> webAppName)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">ConfigurationData</span> configData = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">ConfigurationData</span>();</li>
<li>            <span style="color:#2b91af;">Configuration</span> config = <span style="color:#2b91af;">WebConfigurationManager</span>.OpenWebConfiguration(<span style="color:#a31515;">&quot;/&quot;</span>, webAppName);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">if</span> (config != <span style="color:#0000ff;">null</span>)</li>
<li>            {</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (config.AppSettings.Settings[<span style="color:#a31515;">&quot;Property1&quot;</span>] != <span style="color:#0000ff;">null</span>)</li>
<li>                {</li>
<li style="background:#f3f3f3;">                    configData.Property1 = config.AppSettings.Settings[<span style="color:#a31515;">&quot;Property1&quot;</span>].Value;</li>
<li>                }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>                <span style="color:#0000ff;">if</span> (config.AppSettings.Settings[<span style="color:#a31515;">&quot;Property2&quot;</span>] != <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">                {</li>
<li>                    configData.Property2 = config.AppSettings.Settings[<span style="color:#a31515;">&quot;Property2&quot;</span>].Value;</li>
<li style="background:#f3f3f3;">                }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (config.AppSettings.Settings[<span style="color:#a31515;">&quot;Property3&quot;</span>] != <span style="color:#0000ff;">null</span>)</li>
<li>                {</li>
<li style="background:#f3f3f3;">                    configData.Property3 = <span style="color:#2b91af;">Int32</span>.Parse(config.AppSettings.Settings[<span style="color:#a31515;">&quot;Property3&quot;</span>].Value);</li>
<li>                }</li>
<li style="background:#f3f3f3;">            }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">return</span> configData;</li>
<li>        }</li>
</ol></div>
</p></div>
</p></div>
<h2>Points 2, 3 and 4</h2>
<p>With the configuration data retrieved from the web.config the next few steps are very straightforward to anyone who has done any SharePoint development.</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8cae15c9-4b07-42fc-8371-d59dee5a186d" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#008000;">//open site</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> site = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPSite</span>(siteCollection.Url))</li>
<li>            <span style="color:#0000ff;">using</span> (<span style="color:#0000ff;">var</span> web = site.OpenWeb(config.Property1))</li>
<li style="background:#f3f3f3;">            {                </li>
<li>                web.AllowUnsafeUpdates = <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">try</span></li>
<li>                {</li>
<li style="background:#f3f3f3;">                    <span style="color:#2b91af;">SPList</span> list = <span style="color:#0000ff;">null</span>;</li>
<li>                    <span style="color:#0000ff;">try</span></li>
<li style="background:#f3f3f3;">                    {</li>
<li>                        <span style="color:#008000;">//get library</span></li>
<li style="background:#f3f3f3;">                        list = web.Lists[config.Property2];</li>
<li>                    }</li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">ArgumentException</span> ae)</li>
<li>                    {</li>
<li style="background:#f3f3f3;">                        LogDetails(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;There was a problem gettting the archive list, the error was {0}&quot;</span>, ae.ToString()));</li>
<li>                        <span style="color:#0000ff;">return</span>;</li>
<li style="background:#f3f3f3;">                    }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">if</span> (list == <span style="color:#0000ff;">null</span>)</li>
<li>                    {</li>
<li style="background:#f3f3f3;">                        LogDetails(<span style="color:#a31515;">&quot;Unable to get the archive list&quot;</span>);</li>
<li>                        <span style="color:#0000ff;">return</span>;</li>
<li style="background:#f3f3f3;">                    }                    </li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">var</span> query = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SPQuery</span>();                    </li>
<li>                    query.Query = <span style="color:#a31515;">&quot;YOUR CAML QUERY&quot;</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>                    LogDetails(<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;CAML Query is &quot;</span>, query.Query));</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>                    <span style="color:#008000;">//get items</span></li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">var</span> listItems = list.GetItems(query);</li>
<li>                    <span style="color:#0000ff;">var</span> listItemCount = listItems.Count;</li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">if</span> (listItems == <span style="color:#0000ff;">null</span> || listItemCount == 0)</li>
<li>                    {</li>
<li style="background:#f3f3f3;">                        LogDetails(<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;There were no items returned by the query {0}&quot;</span>, query.Query));</li>
<li>                        <span style="color:#0000ff;">return</span>;</li>
<li style="background:#f3f3f3;">                    }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                    LogDetails(<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;Got items &quot;</span>, listItemCount.ToString()));</li>
</ol></div>
</p></div>
</p></div>
</p>
<h2>Point 5</h2>
<p>At this stage we now have a list of all items items which meet the relevant archive criteria but for obvious reason the actual logic has been removed from this blog. My next step was to iterate through all items and move them to the appropriate location. At first I used a foreach loop but this didn’t work as I was adjusting the item collection and this caused a runtime error. Next I tried a for loop using the count of the number of items. While this seemed to work I found it was only iterating through half of the list and after the half way point I was getting an error saying “<strong>Specified argument was out of the range of valid values</strong>.”. For example if the count of items was 10 it could loop through items 1-5 but as soon as it reached 6 it give the error above. To get around this I changed the code to start at the last item in the count and work backwards i.e. 10, 9, 8. To keep the code contained I separated the main functionality out into a few different methods. </p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:badef23c-8200-401b-abf3-e237d993fc7e" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Loop through items</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#008000;">//loop through items moving them</span></li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> itemNumber = listItemCount; itemNumber &gt; 0; itemNumber&#8211;)</li>
<li>                    {</li>
<li style="background:#f3f3f3;">                        <span style="color:#0000ff;">try</span></li>
<li>                        {</li>
<li style="background:#f3f3f3;">                            LogDetails(<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;Start item &quot;</span>, itemNumber.ToString()));</li>
<li>                            <span style="color:#2b91af;">SPListItem</span> listItem = listItems[itemNumber -1];</li>
<li style="background:#f3f3f3;">                            <span style="color:#0000ff;">if</span> (listItem == <span style="color:#0000ff;">null</span>)</li>
<li>                            {</li>
<li style="background:#f3f3f3;">                                LogDetails(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;There was a problem getting the list item at index {0} returned by the query {1}&quot;</span>,</li>
<li>                                    itemNumber.ToString(), query.Query));</li>
<li style="background:#f3f3f3;">                            }</li>
<li>                            <span style="color:#0000ff;">else</span></li>
<li style="background:#f3f3f3;">                            {</li>
<li>                                MoveItem(listItem, web);</li>
<li style="background:#f3f3f3;">                            }</li>
<li>                        }</li>
<li style="background:#f3f3f3;">                        <span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</li>
<li>                        {</li>
<li style="background:#f3f3f3;">                            LogDetails(ex.ToString());                            </li>
<li>                        }                                                                        </li>
<li style="background:#f3f3f3;">                    }</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>This function takes an item, gets the modified date and passes this to another helper function which gets the folder which the item has to be moved to. Next it builds up the URL to where the item has to be copied to. The item is then moved but because this method doesn’t return the SPFile object I had to then get it from the destination folder. Once I had the new item I then set some properties to ensure metadata is retained as otherwise the created and modified details would be incorrect. Finally the original item is deleted.</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ba7d673e-0e7a-45c0-9f29-2bbc935370b3" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Function for each item</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Method to perform the move of an individual item</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;listItem&quot;&gt;</span><span style="color:#008000;">An SPListItem of the item to be moved</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;web&quot;&gt;</span><span style="color:#008000;">An SpWeb object which represents the web in which the item is located</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> MoveItem(<span style="color:#2b91af;">SPListItem</span> listItem, <span style="color:#2b91af;">SPWeb</span> web)</li>
<li>        {           </li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">DateTime</span> modifiedDate = <span style="color:#2b91af;">DateTime</span>.Parse(listItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Modified].ToString());            </li>
<li>            <span style="color:#2b91af;">SPFolder</span> destinationLocation = GetDestinationLibrary(modifiedDate, web);            </li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">string</span> destinationLocationURL = <span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;{0}/{1}/{2}&quot;</span>, web.Url, destinationLocation.Url, listItem.File.Name);           </li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">try</span></li>
<li>            {</li>
<li style="background:#f3f3f3;">                listItem.File.CopyTo(destinationLocationURL);</li>
<li>            }</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</li>
<li>            {</li>
<li style="background:#f3f3f3;">                LogDetails(<span style="color:#2b91af;">String</span>.Concat(<span style="color:#a31515;">&quot;There was a problem copying the file to the new URL, the error was {0} &quot;</span>,</li>
<li>                    ex.ToString()));</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">throw</span>;</li>
<li>            }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#2b91af;">SPFile</span> file = <span style="color:#0000ff;">null</span>;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">try</span></li>
<li>            {</li>
<li style="background:#f3f3f3;">                file = destinationLocation.Files[listItem.File.Name];</li>
<li>            }</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</li>
<li>            {</li>
<li style="background:#f3f3f3;">                LogDetails(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;There was a problem getting the new file {0} in location {1}, the error was {2}&quot;</span>,</li>
<li>                    listItem.File.Name, destinationLocation.Url, ex.ToString()));</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">throw</span>;</li>
<li>            }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#0000ff;">if</span> (file == <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                LogDetails(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;Unable to find file {0} in new location.&quot;</span>,</li>
<li style="background:#f3f3f3;">                    listItem.File.Name));</li>
<li>                <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">NullReferenceException</span>(<span style="color:#a31515;">&quot;Unable to find new file in destination location&quot;</span>);</li>
<li style="background:#f3f3f3;">            }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">var</span> fileItem = file.Item;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">try</span></li>
<li>            {</li>
<li style="background:#f3f3f3;">                <span style="color:#008000;">/*since we are running with elevated permissions we need to set the</span></li>
<li>                 <span style="color:#008000;">* author and editor back to the details from the original item</span></li>
<li style="background:#f3f3f3;">                 <span style="color:#008000;">* otherwise it will appear as system account                                         * </span></li>
<li>                 <span style="color:#008000;">* */</span></li>
<li style="background:#f3f3f3;">                fileItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Author] = listItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Author];</li>
<li>                fileItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Created] = listItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Created];</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>                fileItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Editor] = listItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Editor];</li>
<li style="background:#f3f3f3;">                fileItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Modified] = listItem[<span style="color:#2b91af;">SPBuiltInFieldId</span>.Modified];</li>
<li>                fileItem.UpdateOverwriteVersion();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>                listItem.Delete();</li>
<li style="background:#f3f3f3;">            }</li>
<li>            <span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#008000;">//log error and move on to next record</span></li>
<li style="background:#f3f3f3;">                LogDetails(<span style="color:#2b91af;">String</span>.Format(<span style="color:#a31515;">&quot;There was a setting the file properties for item {0} the problem was {1}&quot;</span>,</li>
<li>                    listItem.Title, ex.ToString()));</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">throw</span>;</li>
<li>            }</li>
<li style="background:#f3f3f3;">        }</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>As mentioned above the previous snippet calls this GetDestinationLibrary method which uses the modified date to see if a document library exists for the year in the same web and if not it creates one. Next it calls a separate function which performs the same idea but this one uses the month and creates a folder inside the document library.</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b29ebc2c-d643-44a3-8a61-62befa83d73a" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Get document library</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Method to get the URL of document library which the list item should be added to. If the library is not found one is created.</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;modifiedDate&quot;&gt;</span><span style="color:#008000;">A DateTime of the last modified date of the item</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;web&quot;&gt;</span><span style="color:#008000;">An SPWeb object representing the web which contains the items</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;returns&gt;</span><span style="color:#008000;">An SPFolder of the location the item should be copied to</span><span style="color:#808080;">&lt;/returns&gt;</span></li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#2b91af;">SPFolder</span> GetDestinationLibrary(<span style="color:#2b91af;">DateTime</span> modifiedDate, <span style="color:#2b91af;">SPWeb</span> web)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">string</span> destinationLibraryURL = <span style="color:#2b91af;">String</span>.Empty;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">string</span> year = modifiedDate.Year.ToString();</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">SPList</span> desintationDocumentLibrary = <span style="color:#0000ff;">null</span>;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">try</span></li>
<li>            {</li>
<li style="background:#f3f3f3;">                <span style="color:#008000;">//get library</span></li>
<li>                desintationDocumentLibrary = web.Lists[year];</li>
<li style="background:#f3f3f3;">            }</li>
<li>            <span style="color:#0000ff;">catch</span> (<span style="color:#2b91af;">ArgumentException</span>)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                desintationDocumentLibrary = CreateDocumentLibrary(year, web);</li>
<li style="background:#f3f3f3;">            }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">SPFolder</span> monthlyFolder = GetMonthlyFolder(<span style="color:#2b91af;">CultureInfo</span>.CurrentCulture.DateTimeFormat.GetMonthName(modifiedDate.Month), desintationDocumentLibrary);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">return</span> monthlyFolder;</li>
<li>        }</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&#160;</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ca40c0d5-dd86-4cf5-a94a-1c37e2e12250" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Create monthly folder</div>
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;white-space:nowrap;margin:0;padding:0 0 0 5px;">
<li><span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Method to try and find the folder representing the month given a month name. If a month is not found one is created.</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;monthlyName&quot;&gt;</span><span style="color:#008000;">A string of the month name</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;list&quot;&gt;</span><span style="color:#008000;">An SPList which is the list the folder will be created in</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;returns&gt;</span><span style="color:#008000;">An SPFolder representing the folder to which we should move the item</span><span style="color:#808080;">&lt;/returns&gt;</span></li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#2b91af;">SPFolder</span> GetMonthlyFolder(<span style="color:#0000ff;">string</span> monthName, <span style="color:#2b91af;">SPList</span> list)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#2b91af;">SPFolder</span> monthlyFolder = <span style="color:#0000ff;">null</span>;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//loop through to see if the folder exists</span></li>
<li>            <span style="color:#0000ff;">foreach</span> (<span style="color:#2b91af;">SPListItem</span> folder <span style="color:#0000ff;">in</span> list.Folders)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#0000ff;">if</span> (folder.Name.Equals(monthName, <span style="color:#2b91af;">StringComparison</span>.CurrentCultureIgnoreCase))</li>
<li style="background:#f3f3f3;">                {</li>
<li>                    monthlyFolder = folder.Folder;</li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">break</span>;</li>
<li>                }</li>
<li style="background:#f3f3f3;">            }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//if we don&#039;t have it create it</span></li>
<li>            <span style="color:#0000ff;">if</span> (monthlyFolder == <span style="color:#0000ff;">null</span>)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                monthlyFolder = list.RootFolder.SubFolders.Add(monthName);</li>
<li style="background:#f3f3f3;">                monthlyFolder.Update();</li>
<li>            }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#0000ff;">return</span> monthlyFolder;</li>
<li style="background:#f3f3f3;">        }</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h1>Conclusion</h1>
<p>Putting all this together hopefully some people will find this an interesting article with some useful ideas. As always if anyone who reads this can think of any improvements I am always happy to discuss them.</p>
<p>Lastly if you do use this code as a base for your own project please ensure this is tailored to meet our own solution and has been properly tested on a development environment before being deployed to a live farm.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/167/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=167&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2012/02/07/archiving-timer-job/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>
	</item>
		<item>
		<title>Filtering Data from a SharePoint List in SSRS Reports</title>
		<link>http://raymondlittle.wordpress.com/2011/12/22/filtering-data-from-a-sharepoint-list-in-ssrs-reports/</link>
		<comments>http://raymondlittle.wordpress.com/2011/12/22/filtering-data-from-a-sharepoint-list-in-ssrs-reports/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 14:59:53 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[SSRS]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=158</guid>
		<description><![CDATA[I have created a few SSRS reports which pull data from SharePoint lists and while its relatively easy I have come across the same problem a few times. Every time I try and filter my results using dates it never seems to work as I expect. In this latest scenario I had a custom SharePoint [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=158&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have created a few SSRS reports which pull data from SharePoint lists and while its relatively easy I have come across the same problem a few times. Every time I try and filter my results using dates it never seems to work as I expect. </p>
<p>In this latest scenario I had a custom SharePoint List which had a column for the item start date. All I wanted to do was present the user with 2 date pickers which allow them to set a start and an end date. I then wanted to execute a query using these to return all items where the start date was in between the selected dates.</p>
<p>I generated my CAML query using <a href="http://www.u2u.net/Tools/wincamlquerybuilder/CamlQueryBuilder.aspx">U2U CAML Query Builder</a> and it worked as expected with some hardcoded dates. </p>
<p>Next I opened Visual Studio and created a new Report Project. I then created a new report using the wizard to help setup a new DataSource of type ‘Microsoft SharePoint List’. I entered the query using the one generated in U2U leaving the values as hard coded to test it was working. </p>
<p>After formatting the report I ran it and all was well. Next I decided to create two new parameters one for the start date and one for the end date. I set the default values to be the first day of the month for the start date and the last day of the month for the end date, see formula for default values below.</p>
<p>Start date default value: =CDate(DateSerial(Year(Now), Month(Now), 1))   <br />End date default value: =DateSerial(Year(Now), Month(Now)+1, 0)</p>
<p>I checked and these displayed on the report preview with the correct default values. At this point I was thinking everything was going too smoothly and I was right. My final step was to update the query to add in the values from the date pickers. I started by adding parameters to the dataset properties and mapped these to my report parameters, see figure 1.</p>
<p>Figure 1   <br /><a href="http://raymondlittle.files.wordpress.com/2011/12/dsprops.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="DSProps" border="0" alt="DSProps" src="http://raymondlittle.files.wordpress.com/2011/12/dsprops_thumb.png?w=466&h=270" width="466" height="270" /></a></p>
<p>Next I went to my query and removed the hardcoded, see figure 2, values and added what I think is the correct way to include parameters, see figure 3.</p>
<p>Figure 2   <br /><a href="http://raymondlittle.files.wordpress.com/2011/12/dshardcodedquery.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="DSHardCodedQuery" border="0" alt="DSHardCodedQuery" src="http://raymondlittle.files.wordpress.com/2011/12/dshardcodedquery_thumb.png?w=461&h=273" width="461" height="273" /></a></p>
<p>Figure 3   <br /><a href="http://raymondlittle.files.wordpress.com/2011/12/dsparamquery.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="DSParamQuery" border="0" alt="DSParamQuery" src="http://raymondlittle.files.wordpress.com/2011/12/dsparamquery_thumb.png?w=466&h=272" width="466" height="272" /></a></p>
<p>As soon as I make this change my report stops returning any results. I have spent time looking around and playing with passing different formats of the dates but no matter what I do it simply doesn’t return any values.</p>
<p>For now as a workaround I have managed to get my report working by not including any filters in my query but by adding a filter on the dataset, see figure 4. While this works I am aware this will probably return all items from the list and then show or hide the rows as needed. If anyone can help with a better solution I would appreciate it.</p>
<p>Figure 4   <br /><a href="http://raymondlittle.files.wordpress.com/2011/12/dsrowfilters.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="DSRowFilters" border="0" alt="DSRowFilters" src="http://raymondlittle.files.wordpress.com/2011/12/dsrowfilters_thumb.png?w=496&h=326" width="496" height="326" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=158&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2011/12/22/filtering-data-from-a-sharepoint-list-in-ssrs-reports/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>

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

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

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

		<media:content url="http://raymondlittle.files.wordpress.com/2011/12/dsrowfilters_thumb.png" medium="image">
			<media:title type="html">DSRowFilters</media:title>
		</media:content>
	</item>
		<item>
		<title>PowerShell script for updating workflows</title>
		<link>http://raymondlittle.wordpress.com/2011/12/22/powershell-script-for-updating-workflows/</link>
		<comments>http://raymondlittle.wordpress.com/2011/12/22/powershell-script-for-updating-workflows/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 14:12:14 +0000</pubDate>
		<dc:creator>raymondlittle1000</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://raymondlittle.wordpress.com/?p=148</guid>
		<description><![CDATA[The other day when working on a project I needed to update some existing Visual Studio workflows. I had a Visual Studio project which contained a variety of workflows which were used across several SharePoint sites. While keeping all the workflows in one solution works it is not ideal for deployment as you can’t auto [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=148&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The other day when working on a project I needed to update some existing Visual Studio workflows. I had a Visual Studio project which contained a variety of workflows which were used across several SharePoint sites. While keeping all the workflows in one solution works it is not ideal for deployment as you can’t auto associate the workflows as part of the deployment.</p>
<p>I decided to try and automate the deployment so started thinking what my options were. Being a developer my first thought was code in a feature receiver on activated and deactivated, however I decided that this would be another opportunity to practice some PowerShell. This gave me the flexibility of changing the script if need be without having to alter code. </p>
<p>Before starting to write the script I went through in my head what it need to do and came up with the following:</p>
<ol>
<li>Get the list to which the workflow should be deployed</li>
<li>Check to see if the workflow was already deployed</li>
<ol>
<li>If it was already deployed check to see if there were any running instances</li>
<ol>
<li>If there were running instances set the workflow to not allow any new instances but allow existing ones to finish.</li>
<li>If not remove the workflow and go to step 3</li>
</ol>
<li>If not move on to step 3</li>
</ol>
<li>Associate the new version of the workflow to the list</li>
</ol>
<p>&#160;</p>
<p>With these steps in mind I started to create my script. I will cover each of the above and go through the script required to achieve the goal. </p>
<h2>Point 1</h2>
<p>First I wanted to get the list the workflow was associated with. This is very easy and can be accomplished by getting the web object then using that to get the list object much the same way you would if you were writing C#</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:731ef631-8bd0-46ab-a3fc-91780ff56808" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Get Workflow List</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;">
<li>Write-Host &#039;Get web&#039;</li>
<li style="background:#f3f3f3;">$hrWeb = Get-SPWeb $siteURL</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">Write-Host &#039;Get Lists&#039;</li>
<li>$list = $hrWeb.Lists[$listName]</li>
<li style="background:#f3f3f3;">$taskList = $hrWeb.Lists[$taskListName]</li>
<li>$workflowHistoryList = $hrWeb.Lists[$historyListName]</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h2>Point 2</h2>
<p>With the list the next task was to see if the workflow is currently deployed to the list. This presented a problem as my plan was to automatically deploy the workflow with a name and the date of the deployment i.e. “Workflow 22/12/2011’. This caused problems as I wouldn’t know the last deployment date so I had to use a like comparison on the name. I wasn’t comfortable just using a like comparison as I felt this wasn’t always going to get me what I wanted so I added an additional check to validate the name of the workflow but also if it was of the correct base workflow type.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:da6c2c9a-6828-4c1b-84b6-fafeb23d113f" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Try and find workflow</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;">
<li>Write-Host &#039;Get base template&#039;</li>
<li style="background:#f3f3f3;">$basetemplate = $hrWeb.WorkflowTemplates.GetTemplateByName($workflowTemplateName,$culture);</li>
<li>Write-Host $basetemplate.Id</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>#set up variable to hold workflow instance if we find it</li>
<li style="background:#f3f3f3;">$existingWorkflow = $null</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">Write-Host &#039;Get workflow association&#039;</li>
<li>#loop through all associations on list</li>
<li style="background:#f3f3f3;">foreach($tempWorkflowAssociation in $list.WorkflowAssociations)</li>
<li>{</li>
<li style="background:#f3f3f3;">#check if the base template id matches the base template of the current WF associaton</li>
<li>#in additon check the name of the current WF association against the one we are interested in</li>
<li style="background:#f3f3f3;">if($tempWorkflowAssociation.BaseTemplate.Id -eq $basetemplate.Id -and $tempWorkflowAssociation.Name -like $workflowName +&quot;*&quot; )</li>
<li>{</li>
<li style="background:#f3f3f3;">$existingWorkflow = $tempWorkflowAssociation</li>
<li>break</li>
<li style="background:#f3f3f3;">}</li>
<li>}</li>
<li style="background:#f3f3f3;">#check we have a workflow</li>
<li>if($existingWorkflow -ne $null)</li>
<li style="background:#f3f3f3;">{</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h2>Point 2.1</h2>
<p>As you can see the last line in the previous snippet makes sure we have a workflow returned so at this point we know there is a version of the workflow we are interested in currently deployed to the list. Now I need to know if there are any running instances for this.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:78cb43e3-2d60-45ba-ac34-4f6f7dde6c60" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Check running instances</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;">
<li>Write-Host &#039;Got workflow associated with list&#039;</li>
<li style="background:#f3f3f3;">if($existingWorkflow.RunningInstances -ge 0)</li>
<li>{</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h2>Point 2.1.1</h2>
<p>If there are running instances these must continue working until they are complete but not allow any new instances of this workflow to be initiated. This can be done by setting the ‘No new instances’ option through the workflow settings in the UI or by the following command.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e959ab52-6b8d-4813-94a1-5ee2c46b5397" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">No new instances</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;">
<li>Write-Host &#039;There are running instances so set to allow no new running instances&#039;</li>
<li style="background:#f3f3f3;">$existingWorkflow.set_Enabled($false)</li>
<li>$list.UpdateWorkflowAssociation($existingWorkflow)</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h2>Point 2.1.2</h2>
<p>If there are no running instances then we want to remove the current version. This can be done by the command below</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5735eebb-b332-4ee6-9949-5ab5f9218502" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Remove workflow</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;">
<li>Write-Host &#039;No running instances so remove&#039;</li>
<li style="background:#f3f3f3;">$list.RemoveWorkflowAssociation($existingWorkflow)</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<h2>Point 3</h2>
<p>We are now in a position where we can try and associate the new version of the workflow to the list. As I mentioned above when attaching the new workflow I am adding it with the name and the date so I first build up the name then create an new workflow association object the add it to the list.</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:36cd471f-9d84-4222-a8a3-9d635f0fdd51" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Associate new workflow</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;">
<li>Write-Host &#039;Create workflow association details&#039;</li>
<li style="background:#f3f3f3;">$date = Get-Date</li>
<li>$workflowName = $workflowName + &quot; &quot; + $date.ToShortDateString()</li>
<li style="background:#f3f3f3;">$newWorkflow=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($basetemplate, $workflowName,$taskList,$workflowHistoryList)  </li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">$newWorkflow.AllowManual = $allowManualStart</li>
<li>$newWorkflow.AutoStartChange = $autoStartChange</li>
<li style="background:#f3f3f3;">$newWorkflow.AutoStartCreate = $autoStartCreate</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">Write-Host &#039;Add workflow to list&#039;</li>
<li>$list.AddWorkflowAssociation($newWorkflow)</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>At this point the new version of the workflow should be attached to the list. I started creating this with some hardcoded values but then extracted it out into a reusable function. The full function is below.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:925727df-f490-4fcb-9e04-f60a0fcbb187" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Fully assocaition function</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;">
<li>function AssocaiteWorkflow([string]$siteURL, [string]$listName, [string]$taskListName, [string]$historyListName,</li>
<li style="background:#f3f3f3;">    [string]$workflowTemplateName, [string]$workflowName, [bool]$allowManualStart, [bool]$autoStartChange, [bool]$autoStartCreate)</li>
<li>{ </li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>Write-Host &#039;Start WF assocaition for &#039; $workflowName &#039; on list &#039; $listName &#039; and site &#039; $siteURL</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>#get culture</li>
<li style="background:#f3f3f3;">$culture= Get-Culture</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">Write-Host &#039;Get web&#039;</li>
<li>$hrWeb = Get-SPWeb $siteURL</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>Write-Host &#039;Get Lists&#039;</li>
<li style="background:#f3f3f3;">$list = $hrWeb.Lists[$listName]</li>
<li>$taskList = $hrWeb.Lists[$taskListName]</li>
<li style="background:#f3f3f3;">$workflowHistoryList = $hrWeb.Lists[$historyListName]</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">Write-Host &#039;Get base template&#039;</li>
<li>$basetemplate = $hrWeb.WorkflowTemplates.GetTemplateByName($workflowTemplateName,$culture);</li>
<li style="background:#f3f3f3;">Write-Host $basetemplate.Id</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">#set up variable to hold workflow instance if we find it</li>
<li>$existingWorkflow = $null</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>Write-Host &#039;Get workflow association&#039;</li>
<li style="background:#f3f3f3;">#loop through all associations on list</li>
<li>foreach($tempWorkflowAssociation in $list.WorkflowAssociations)</li>
<li style="background:#f3f3f3;">{</li>
<li>#check if the base template id matches the base template of the current WF associaton</li>
<li style="background:#f3f3f3;">#in additon check the name of the current WF association against the one we are interested in</li>
<li>if($tempWorkflowAssociation.BaseTemplate.Id -eq $basetemplate.Id -and $tempWorkflowAssociation.Name -like $workflowName +&quot;*&quot; )</li>
<li style="background:#f3f3f3;">{</li>
<li>$existingWorkflow = $tempWorkflowAssociation</li>
<li style="background:#f3f3f3;">break</li>
<li>}</li>
<li style="background:#f3f3f3;">}</li>
<li>#check we have a workflow</li>
<li style="background:#f3f3f3;">if($existingWorkflow -ne $null)</li>
<li>{</li>
<li style="background:#f3f3f3;">Write-Host &#039;Got workflow associated with list&#039;</li>
<li>if($existingWorkflow.RunningInstances -ge 0)</li>
<li style="background:#f3f3f3;">{</li>
<li>Write-Host &#039;There are running instances so set to allow no new running instances&#039;</li>
<li style="background:#f3f3f3;">$existingWorkflow.set_Enabled($false)</li>
<li>$list.UpdateWorkflowAssociation($existingWorkflow)</li>
<li style="background:#f3f3f3;">}</li>
<li>else</li>
<li style="background:#f3f3f3;">{</li>
<li>Write-Host &#039;No running instances so remove&#039;</li>
<li style="background:#f3f3f3;">$list.RemoveWorkflowAssociation($existingWorkflow)</li>
<li>}</li>
<li style="background:#f3f3f3;">}</li>
<li>else</li>
<li style="background:#f3f3f3;">{</li>
<li>Write-Host &#039;No workflow associated with list&#039;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">Write-Host &#039;Create workflow association details&#039;</li>
<li>$date = Get-Date</li>
<li style="background:#f3f3f3;">$workflowName = $workflowName + &quot; &quot; + $date.ToShortDateString()</li>
<li>$newWorkflow=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($basetemplate, $workflowName,$taskList,$workflowHistoryList)  </li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>$newWorkflow.AllowManual = $allowManualStart</li>
<li style="background:#f3f3f3;">$newWorkflow.AutoStartChange = $autoStartChange</li>
<li>$newWorkflow.AutoStartCreate = $autoStartCreate</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>Write-Host &#039;Add workflow to list&#039;</li>
<li style="background:#f3f3f3;">$list.AddWorkflowAssociation($newWorkflow)</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>You can then call this in the standard way i.e. </p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ee4c3bda-8aab-46bf-a83c-1aa2453dfea6" class="wlWriterEditableSmartContent">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;">Call function</div>
<div style="background:#ddd;overflow:auto;">
<ol start="1" style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;">
<li>AssocaiteWorkflow &quot;http://sharepoint&quot; &quot;Test List Name&quot; &quot;Tasks List Name&quot; &quot;Workflow History List Name&quot; &quot;Visual Studio Workflow Name&quot; &quot;Workflow Instance Name&quot; $true $true $true</li>
</ol></div>
</p></div>
</p></div>
<p>&#160;</p>
<p>As with all PowerShell scripts while they are very useful please always test them on a development environment before running them on live. As always I can’t be responsible for any issues that arise so if you use this you do so at your own risk.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raymondlittle.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raymondlittle.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raymondlittle.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raymondlittle.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raymondlittle.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raymondlittle.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raymondlittle.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raymondlittle.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raymondlittle.wordpress.com&#038;blog=19495960&#038;post=148&#038;subd=raymondlittle&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raymondlittle.wordpress.com/2011/12/22/powershell-script-for-updating-workflows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9847e000b1f5fe94f43e2adffad2d00c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raymondlittle1000</media:title>
		</media:content>
	</item>
	</channel>
</rss>
