<?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>Raymund Macaalay&#039;s Dev Blog</title>
	<atom:link href="http://anyrest.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://anyrest.wordpress.com</link>
	<description>ANYthing Regarding Software Technology, helping IT Professionals 1 blog post at a time.</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:46:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='anyrest.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/417620907fac844e618341e1c762d8b5?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Raymund Macaalay&#039;s Dev Blog</title>
		<link>http://anyrest.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://anyrest.wordpress.com/osd.xml" title="Raymund Macaalay&#039;s Dev Blog" />
	<atom:link rel='hub' href='http://anyrest.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How to render MVC View on a Modal Popup Window</title>
		<link>http://anyrest.wordpress.com/2012/01/16/how-to-render-mvc-view-on-a-modal-popup-window/</link>
		<comments>http://anyrest.wordpress.com/2012/01/16/how-to-render-mvc-view-on-a-modal-popup-window/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 09:00:32 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Telerik]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1485</guid>
		<description><![CDATA[You might be wondering how to place an MVC View easily on a pop-up window like the image above that&#8217;s why you are in this page now? Well you a bit lucky as I will tell you how easy is this to execute, but first of all let me tell you that I am using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1485&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://anyrest.files.wordpress.com/2012/01/ui.png"><img class="alignnone size-full wp-image-1486" title="UI" src="http://anyrest.files.wordpress.com/2012/01/ui.png?w=630&#038;h=267" alt="" width="630" height="267" /></a></p>
<p>You might be wondering how to place an MVC View easily on a pop-up window like the image above that&#8217;s why you are in this page now?</p>
<p>Well you a bit lucky as I will tell you how easy is this to execute, but first of all let me tell you that I am using the Window Control from <a href="http://demos.telerik.com/aspnet-mvc/">Telerik Extensions for ASP.NET MVC</a> to make my life easy and not re-invent the wheel and if you don&#8217;t have problems with that then read ahead.</p>
<p>Now lets start.  Lets pretend we want an application to set up a new client Account and what we want is when a user clicks a link create account it will pop up a window for account creation.  So like any other MVC Application you need your Model which in this case we will call &#8220;AccountSetupViewModel&#8221; which is a model for setting up accounts, a View for the pop up which we can call &#8220;NewAccount.cshtml&#8221; and Controller which we can call &#8220;SetupController&#8221;.  We also need some JavaScript file to separate our JavaScript commands to others for a cleaner implementation.</p>
<p>Lets first make our ViewModel in <strong>AccountSetupViewModel.cs</strong> under Controllers -&gt; ViewModels -&gt; Setup folder, we will make it simple so it will only contain AccountCode and AccountName</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">AccountSetupViewModel</span>
{
    [<span style="color:#2b91af;">Required</span>]
    <span style="color:blue;">public</span> <span style="color:blue;">string</span> AccountCode { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    [<span style="color:#2b91af;">Required</span>]
    <span style="color:blue;">public</span> <span style="color:blue;">string</span> AccountName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

}</pre>
<p>Now lets create a query to execute with firstName and lastName as a parameter and name it as GetAccountViewModel which we will place in  <strong>AccountSetupQuery.cs</strong> under Controllers -&gt; Queries -&gt; Setup, you can also create an interface for it if you wish.  This method will combine the firstName and lastName and sets the AccountName viewModel.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:#2b91af;">AccountSetupViewModel</span> GetAccountViewModel(<span style="color:blue;">string</span> firstName, <span style="color:blue;">string</span> lastName)
{
    <span style="color:blue;">var</span> viewModel = <span style="color:blue;">new</span> <span style="color:#2b91af;">AccountSetupViewModel</span>();

    viewModel.AccountCode = <span style="color:#a31515;">"RSM"</span>;
    viewModel.AccountName = firstName + <span style="color:#a31515;">" "</span> + lastName;

    <span style="color:blue;">return</span> viewModel;
}</pre>
<p>Now lets create our Controller called <strong>SetupController.cs</strong> just in the Controllers directory and create a method called &#8220;GetNewAccountViewHtml&#8221;, this will be the method that will output a JsonResult to render our view</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;">[<span style="color:#2b91af;">HttpPost</span>]
<span style="color:blue;">public</span> <span style="color:#2b91af;">JsonResult</span> GetNewAccountViewHtml(<span style="color:blue;">string</span> firstName, <span style="color:blue;">string</span> lastName)
{
    <span style="color:blue;">string</span> viewHtml = <span style="color:blue;">string</span>.Empty;
    <span style="color:blue;">var</span> viewModel = accountSetupQuery.GetAccountViewModel(firstName, lastName);

    viewHtml = RenderRazorViewToString(<span style="color:#a31515;">"NewAccount"</span>, viewModel);

    <span style="color:blue;">var</span> hashtable = <span style="color:blue;">new</span> <span style="color:#2b91af;">Hashtable</span>();
    hashtable[<span style="color:#a31515;">"viewHtml"</span>] = viewHtml;

    <span style="color:blue;">return</span> Json(hashtable);
}</pre>
<p>You notice we have a method called RenderRazorViewToString, like how it&#8217;s called its purpose is to Render the MVC Razor View to string.   We will be using the MVC engine to render the view model as HTML so we can easily place it on the pop-up window.  You can place it in the controller but best if there is a separate class for this as you will  definitely reuse this a lot.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">private</span> <span style="color:blue;">string</span> RenderRazorViewToString(<span style="color:blue;">string</span> viewName, <span style="color:blue;">object</span> model)
{
    ViewData.Model = model;

    <span style="color:blue;">using</span> (<span style="color:blue;">var</span> stringWriter = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringWriter</span>())
    {
        <span style="color:blue;">var</span> viewResult = <span style="color:#2b91af;">ViewEngines</span>.Engines.FindPartialView(ControllerContext, viewName);
        <span style="color:blue;">var</span> viewContext = <span style="color:blue;">new</span> <span style="color:#2b91af;">ViewContext</span>(ControllerContext, viewResult.View, ViewData, TempData, stringWriter);
        viewResult.View.Render(viewContext, stringWriter);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);

        <span style="color:blue;">return</span> stringWriter.GetStringBuilder().ToString();
    }
}</pre>
<p>Next lets create a view and call it &#8220;<strong>NewAccount.cshtml</strong>&#8221; and place it in Views -&gt; Setup, this is the view that we will be using on the pop-up window.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@model </span>CI5.Web.Mvc.Controllers.ViewModels.Setup.<span style="color:#2b91af;">AccountSetupViewModel</span>
<span style="background:yellow;">@</span><span style="color:blue;">using</span> Telerik.Web.Mvc.UI;
<span style="background:yellow;">@{</span>
    Layout = <span style="color:blue;">null</span>;
<span style="background:yellow;">}</span>

This is an MVC view in a pop up <span style="color:blue;">&lt;</span><span style="color:maroon;">br</span> <span style="color:blue;">/&gt;</span>
Account Code : <span style="background:yellow;">@</span>Model.AccountCode <span style="color:blue;">&lt;</span><span style="color:maroon;">br</span> <span style="color:blue;">/&gt;</span>
Account Name : <span style="background:yellow;">@</span>Model.AccountName</pre>
<p>Now you have all of  the contents you need for that pop-up, now lets create a view to call the pop-up we can use &#8220;<strong>Default.cshtml</strong>&#8221; under View -&gt; Setup Folder in this instance.  On your view it will be simple as registering JavaScript on an a link to pop up the window so put this on your view.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:maroon;">li</span> <span style="color:red;">class</span><span style="color:blue;">="newclient"&gt;&lt;</span><span style="color:maroon;">a</span> <span style="color:red;">href</span><span style="color:blue;">="#newClient"</span> <span style="color:red;">title</span><span style="color:blue;">=""</span> <span style="color:red;">onclick</span><span style="color:blue;">="javascript:AccountSetupForm.displayPopUpWindow('Raymund', 'Macaalay');"&gt;</span>New Client<span style="color:blue;">&lt;/</span><span style="color:maroon;">a</span><span style="color:blue;">&gt;&lt;/</span><span style="color:maroon;">li</span><span style="color:blue;">&gt;</span></pre>
<p>Then using a javascript file that initializes on load of the &#8220;<strong>_SiteLayout.cshtml</strong>&#8220;</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:maroon;">script</span> <span style="color:red;">src</span><span style="color:blue;">="</span><span style="background:yellow;">@</span><span style="color:blue;">Url.Content(</span><span style="color:#a31515;">"~/Scripts/AccountSetup.js"</span><span style="color:blue;">)"</span> <span style="color:red;">type</span><span style="color:blue;">="text/javascript"&gt;&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span></pre>
<p>which we call &#8220;<strong>AccountSetup.js</strong>&#8221; located in Scripts folder we will create a function trigger the Telerik window pop up.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">var</span> AccountSetupForm = (<span style="color:blue;">function</span> () {
    <span style="color:blue;">return</span> {
        init: <span style="color:blue;">function</span> () {
        },
        displayPopUpWindow: <span style="color:blue;">function</span> (firstName, lastName) {

            <span style="color:blue;">var</span> postData = {
                firstName: firstName,
                lastName: lastName
            };

            $.post(<span style="color:maroon;">"/Setup/GetNewAccountViewHtml"</span>, postData, <span style="color:blue;">function</span> (data) {
                $.telerik.window.create({
                    title: <span style="color:maroon;">"Sample Window"</span>,
                    html: unescape(data.viewHtml),
                    modal: <span style="color:blue;">true</span>,
                    resizable: <span style="color:blue;">false</span>,
                    visible: <span style="color:blue;">false</span>,
                    width: 500,
                    height: 200
                })
            .data(<span style="color:maroon;">'tWindow'</span>).center().open();
            });
        }
    };

})();

$(document).ready(<span style="color:blue;">function</span> () {
    AccountSetupForm.init();
});</pre>
<p>So for a full view on how this is structured please refer to the image below.  I highlighted what was used on the codes above.</p>
<p><a href="http://anyrest.files.wordpress.com/2012/01/layout1.png"><img class="alignnone size-full wp-image-1490" title="Layout" src="http://anyrest.files.wordpress.com/2012/01/layout1.png?w=630" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1485/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1485/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1485&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2012/01/16/how-to-render-mvc-view-on-a-modal-popup-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2012/01/ui.png" medium="image">
			<media:title type="html">UI</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2012/01/layout1.png" medium="image">
			<media:title type="html">Layout</media:title>
		</media:content>
	</item>
		<item>
		<title>Foreign Key Drop Downs on Telerik MVC Grid</title>
		<link>http://anyrest.wordpress.com/2012/01/13/foreign-key-drop-downs-on-telerik-mvc-grid/</link>
		<comments>http://anyrest.wordpress.com/2012/01/13/foreign-key-drop-downs-on-telerik-mvc-grid/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 09:00:23 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[S#arp Architecture]]></category>
		<category><![CDATA[Telerik]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1479</guid>
		<description><![CDATA[If you are using MVC on your projects and you are using a Grid View control most probably it will be the Telerik Grid.  While it is one of the best ones around it still have its cons but whats good with it is that the product is regularly updated even for the open source [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1479&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are using MVC on your projects and you are using a Grid View control most probably it will be the <a href="http://demos.telerik.com/aspnet-mvc/grid" target="_blank">Telerik Grid</a>.  While it is one of the best ones around it still have its cons but whats good with it is that the product is regularly updated even for the open source license.  One thing I noticed with the older version before <a href="http://www.telerik.com/products/aspnet-mvc/whats-new/release-history/q3-2011-version-2011-3-1115-0.aspx" target="_blank">2011.3.1115.0</a> is that foreign keys are not natively supported, which means if you have a data structure similar to below then it will be task to perform operations on those in the grid as you will go by the template method.</p>
<p><a href="http://anyrest.files.wordpress.com/2012/01/table.png"><img class="alignnone size-full wp-image-1480" title="Table" src="http://anyrest.files.wordpress.com/2012/01/table.png?w=630&#038;h=237" alt="" width="630" height="237" /></a></p>
<p>Logically you will put a drop down on the grid so when you edit or add an item then you will be presented with the contact type options.  Well with that old version it would not be straightforward as you have to make your own template to display that drop down.  For those who are interested well here is how made it.</p>
<p><a href="http://anyrest.files.wordpress.com/2012/01/mvc-grid-with-drop-down.png"><img class="alignnone size-full wp-image-1481" title="MVC Grid with Drop Down" src="http://anyrest.files.wordpress.com/2012/01/mvc-grid-with-drop-down.png?w=630&#038;h=151" alt="" width="630" height="151" /></a></p>
<p>First is to create your view model, for this sample lets call it AccountContactViewModel</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">AccountContactViewModel</span>
{
    <span style="color:blue;">public</span> <span style="color:blue;">int</span> Id { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> LastName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> FirstName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> TelephoneNumber { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> FaxNumber { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> MobileNumber { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> EmailAddress { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">private</span> <span style="color:#2b91af;">NumericKeyValuePair</span> contactTypes;

    [<span style="color:#2b91af;">UIHint</span>(<span style="color:#a31515;">"AccountContactType"</span>), <span style="color:#2b91af;">Required</span>]
    <span style="color:blue;">public</span> <span style="color:#2b91af;">NumericKeyValuePair</span> ContactTypes
    {
        <span style="color:blue;">get</span>
        {
            <span style="color:blue;">if</span> (<span style="color:blue;">this</span>.contactTypeViewModel == <span style="color:blue;">null</span>)
            {
                <span style="color:#2b91af;">NumericKeyValuePair</span> o = <span style="color:blue;">new</span> <span style="color:#2b91af;">NumericKeyValuePair</span>();
                o.Key = 0;
                o.Value = <span style="color:blue;">string</span>.Empty;

                <span style="color:blue;">return</span> o;
            }

            <span style="color:blue;">return</span> <span style="color:blue;">this</span>.contactTypeViewModel;
        }

        <span style="color:blue;">set</span>
        {
            <span style="color:blue;">this</span>.contactTypeViewModel = <span style="color:blue;">value</span>;
        }
    }
}</pre>
<p>If you noticed I used a NumericKeyValuePair which is just a class to define Key and value parings to be used for dropdowns</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">NumericKeyValuePair</span>
{
    <span style="color:blue;">public</span> <span style="color:blue;">int</span> Key { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> Value { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}</pre>
<p>Also you will notice the UIHint property, that&#8217;s the property to set the name of the field template to use to display the data field which is used for rendering data fields in a data model.  In simple words this is what you need to name your Editor Template to show your drop down so it automatically maps the data field in the model and render it in the control.</p>
<p>Next is you need an Editor template to show that drop down values and like I said above we will name it AccountContactType</p>
<p><a href="http://anyrest.files.wordpress.com/2012/01/drowpdownview.png"><img class="alignnone size-full wp-image-1482" title="DrowpDownView" src="http://anyrest.files.wordpress.com/2012/01/drowpdownview.png?w=630" alt=""   /></a></p>
<p>The code behind is easy as it is just using a DropDownListFor and it is feeding data from a ContactType Model, you can also manually add items like such</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@</span>Html.DropDownList(ViewData.TemplateInfo.GetFullHtmlFieldName(<span style="color:blue;">string</span>.Empty), <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">SelectListItem</span>&gt;
{
    <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectListItem</span>{ Value = <span style="color:#a31515;">"0"</span>, Text = <span style="color:blue;">string</span>.Empty },
    <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectListItem</span>{ Value=<span style="color:#a31515;">"1"</span>, Text = <span style="color:#a31515;">"Primary Debtor"</span> }, 
    <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectListItem</span>{ Value=<span style="color:#a31515;">"2"</span>, Text = <span style="color:#a31515;">"Other Debtor"</span> },
    <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectListItem</span>{ Value=<span style="color:#a31515;">"3"</span>, Text = <span style="color:#a31515;">"Primary Internal"</span> },  
    <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectListItem</span>{ Value=<span style="color:#a31515;">"4"</span>, Text = <span style="color:#a31515;">"Other Internal"</span> }
})</pre>
<p>Then on your View where your MVC Grid is, one column should be defined as a Client Template which calls the Client Template above and assigns the ContactTypeId to be linked to the ContactTypes, this is important for the grid to know which item to choose when the row is bound.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@(</span>Html.Telerik().Grid&lt;<span style="color:#2b91af;">AccountContactViewModel</span>&gt;()
.Name(<span style="color:#a31515;">"grdAccountContact"</span>)
.Columns(columns =&gt;
{
    columns.Command(a =&gt;
    {
        a.Delete().ButtonType(<span style="color:#2b91af;">GridButtonType</span>.Image);
    }).Width(80);
    columns.Bound(a =&gt; a.FirstName);
    columns.Bound(a =&gt; a.LastName);
    columns.Bound(a =&gt; a.TelephoneNumber);
    columns.Bound(a =&gt; a.MobileNumber);
    columns.Bound(a =&gt; a.FaxNumber);
    columns.Bound(a =&gt; a.EmailAddress);
    columns.Bound(a =&gt; a.ContactTypeViewModel).ClientTemplate(<span style="color:#a31515;">"&lt;#= ContactTypes.Value #&gt;"</span>).Title(<span style="color:#a31515;">"Contact Type"</span>);
    columns.Bound(a =&gt; a.Id).Hidden();
})
.ToolBar(commands =&gt; commands.Insert())
.DataBinding(d =&gt; d.Ajax()
    .OperationMode(<span style="color:#2b91af;">GridOperationMode</span>.Server)
    .Select(<span style="color:#a31515;">"SelectAccountContacts"</span>, <span style="color:#a31515;">"Setup"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId })
    .Update(<span style="color:#a31515;">"UpdateAccountContacts"</span>, <span style="color:#a31515;">"Setup"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId })
    .Delete(<span style="color:#a31515;">"DeleteAccountContacts"</span>, <span style="color:#a31515;">"Setup"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId })
.Editable(e =&gt; e.Mode(<span style="color:#2b91af;">GridEditMode</span>.InCell))
.DataKeys(k =&gt; k.Add(a =&gt; a.Id))
.ToolBar(b =&gt; b.SubmitChanges())
.ClientEvents(e =&gt; e
    .OnDataBound(<span style="color:#a31515;">"GridHelper.onDataBound"</span>)
    .OnEdit(<span style="color:#a31515;">"GridHelper.onEdit"</span>)
    .OnSave(<span style="color:#a31515;">"GridHelper.onSave"</span>))
)</pre>
<p>Now for your Controller you will have something simple as this</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;">[<span style="color:#2b91af;">GridAction</span>]
<span style="color:blue;">public</span> <span style="color:#2b91af;">ActionResult</span> SelectAccountContacts(<span style="color:blue;">int</span>? accountId)
{
    <span style="color:blue;">var</span> viewModel = accountSetupQuery.GetAccountContacts(accountId.Value);

    <span style="color:blue;">return</span> View(<span style="color:blue;">new</span> <span style="color:#2b91af;">GridModel</span>&lt;<span style="color:#2b91af;">AccountContactViewModel</span>&gt;(viewModel));
}</pre>
<p>which calls this query</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:#2b91af;">IList</span>&lt;<span style="color:#2b91af;">AccountContactViewModel</span>&gt; GetAccountContacts(<span style="color:blue;">int</span> accountId)
{
    <span style="color:blue;">var</span> accountContacts = 
    (<span style="color:blue;">from</span> a <span style="color:blue;">in</span> Session.Query&lt;<span style="color:#2b91af;">AccountContact</span>&gt;()
    <span style="color:blue;">where</span> a.Account.Id == accountId
    <span style="color:blue;">select</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">AccountContactViewModel</span>
    {
        Id = a.Account.Id,
        LastName = a.LastName,
        FirstName = a.FirstName,
        TelephoneNumber = a.TelephoneNumber,
        FaxNumber = a.FaxNumber,
        MobileNumber = a.MobileNumber,
        EmailAddress = a.EmailAddress,
        ContactTypeViewModel = <span style="color:blue;">new</span> <span style="color:#2b91af;">NumericKeyValuePair</span>() { Key = a.ContactType.Id, Value = a.ContactType.Name }
    }).ToList();

    <span style="color:blue;">return</span> accountContacts;
}</pre>
<p>Take note of that Session.Query&lt;AccountContact&gt; as I am using <a href="http://www.sharparchitecture.net/" target="_blank">S#arp Architecture</a>, my LINQ is executing its query to a nHibernateQuery Session of the Account Contact Entity defined in my domain, you can use any queryable collection here.</p>
<p>Now thanks to the new version you don&#8217;t have to use the Client Template and all you have to do is to define the column as a Foreign Key.  Here is how it should be done now for those interested.  (BTW the codes above can still be used as a reference for using Client Template)</p>
<p>First is we modify our view model and we remove all instances of the NumericKeyValuePair and replace it with the Foreign Key Id and we will name it ContactTypeId.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">AccountContactViewModel</span>
{
    <span style="color:blue;">public</span> <span style="color:blue;">int</span> Id { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> LastName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> FirstName { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> TelephoneNumber { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> FaxNumber { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> MobileNumber { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">string</span> EmailAddress { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">int</span> ContactTypeId { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }  
}</pre>
<p>Next is we get rid of the client template then your Grid should be defined like this</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@(</span>Html.Telerik().Grid&lt;<span style="color:#2b91af;">AccountContactViewModel</span>&gt;()
.Name(<span style="color:#a31515;">"grdAccountContact"</span>)
.Columns(columns =&gt;
{
    columns.Command(a =&gt;
    {
        a.Delete().ButtonType(<span style="color:#2b91af;">GridButtonType</span>.Image);
    }).Width(80);
    columns.Bound(a =&gt; a.FirstName);
    columns.Bound(a =&gt; a.LastName);
    columns.Bound(a =&gt; a.TelephoneNumber);
    columns.Bound(a =&gt; a.MobileNumber);
    columns.Bound(a =&gt; a.FaxNumber);
    columns.Bound(a =&gt; a.EmailAddress);
    columns.ForeignKey(a =&gt; a.ContactTypeId, Model.ContactTypes, <span style="color:#a31515;">"Id"</span>, <span style="color:#a31515;">"Name"</span>);
    columns.Bound(a =&gt; a.Id).Hidden();
})
.ToolBar(commands =&gt; commands.Insert())
.DataBinding(d =&gt; d.Ajax()
    .OperationMode(<span style="color:#2b91af;">GridOperationMode</span>.Server)
    .Select(<span style="color:#a31515;">"SelectAccountContacts"</span>, <span style="color:#a31515;">"Setup"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId })
    .Update(<span style="color:#a31515;">"UpdateAccountContacts"</span>, <span style="color:#a31515;">"Setup"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId }))
.Editable(e =&gt; e.Mode(<span style="color:#2b91af;">GridEditMode</span>.InCell))
.DataKeys(k =&gt; k.Add(a =&gt; a.Id))
.ToolBar(b =&gt; b.SubmitChanges())
.ClientEvents(e =&gt; e.OnDataBound(<span style="color:#a31515;">"GridHelper.onDataBound"</span>)
    .OnEdit(<span style="color:#a31515;">"GridHelper.onEdit"</span>)
    .OnSave(<span style="color:#a31515;">"GridHelper.onSave"</span>))
<span style="background:yellow;">)</span></pre>
<p>You notice that the old ClientTemplate is now replaced to ForeignKey and it is populated by from the Model.ContactTypes so make sure before the MVC Grid is populated the Model.ContactTypes already have a value.</p>
<p>Now you can still use the same controller but you will need to change your query to something like this</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:#2b91af;">IList</span>&lt;<span style="color:#2b91af;">AccountContactViewModel</span>&gt; GetAccountContacts(<span style="color:blue;">int</span> accountId)
{
    <span style="color:blue;">var</span> accountContacts = (<span style="color:blue;">from</span> a <span style="color:blue;">in</span> Session.Query&lt;<span style="color:#2b91af;">AccountContact</span>&gt;()
    <span style="color:blue;">where</span> a.Account.Id == accountId
    <span style="color:blue;">select</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">AccountContactViewModel</span>
    {
        Id = a.Account.Id,
        LastName = a.LastName,
        FirstName = a.FirstName,
        TelephoneNumber = a.TelephoneNumber,
        FaxNumber = a.FaxNumber,
        MobileNumber = a.MobileNumber,
        EmailAddress = a.EmailAddress,
        ContactTypeId = a.ContactType.Id
    }).ToList();

    <span style="color:blue;">return</span> accountContacts;
}</pre>
<p>You will notice that you don&#8217;t have that NumericKeyValuePair and it was replaced with the Foreign Key Id in your AccountContactViewModel.</p>
<p>That&#8217;s it, its simpler than before, now you can put a of foreign keys on that table without creating lots of Client Template.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1479/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1479/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1479/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1479&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2012/01/13/foreign-key-drop-downs-on-telerik-mvc-grid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2012/01/table.png" medium="image">
			<media:title type="html">Table</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2012/01/mvc-grid-with-drop-down.png" medium="image">
			<media:title type="html">MVC Grid with Drop Down</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2012/01/drowpdownview.png" medium="image">
			<media:title type="html">DrowpDownView</media:title>
		</media:content>
	</item>
		<item>
		<title>The Easy way of changing Collation of all Database objects in SQL Server</title>
		<link>http://anyrest.wordpress.com/2011/12/19/the-easy-way-of-changing-collation-of-all-database-objects-in-sql-server/</link>
		<comments>http://anyrest.wordpress.com/2011/12/19/the-easy-way-of-changing-collation-of-all-database-objects-in-sql-server/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 09:00:19 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Database Collation]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1472</guid>
		<description><![CDATA[Did you encountered a problem where you wanted to change your database collation to default or even just changing it to a different type? I guess what you had initially done (like me) was to change the collation of the Database well that does not quite work well as the existing columns will not be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1472&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Did you encountered a problem where you wanted to change your database collation to default or even just changing it to a different type? I guess what you had initially done (like me) was to change the collation of the Database</p>
<p><a href="http://anyrest.files.wordpress.com/2011/12/change-db-collation.png"><img class="alignnone size-full wp-image-1473" title="Change Db Collation" src="http://anyrest.files.wordpress.com/2011/12/change-db-collation.png?w=630&#038;h=565" alt="" width="630" height="565" /></a></p>
<p>well that does not quite work well as the existing columns will not be changed and retain its current collation type, only the newly created objects will use this new collation type. So you are left to the option of changing the columns one at a time by going to the column property and restoring it to default or choosing the collation type you want.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/12/collation1.png"><img class="alignnone size-full wp-image-1474" title="Collation1" src="http://anyrest.files.wordpress.com/2011/12/collation1.png?w=630" alt=""   /></a></p>
<p><a href="http://anyrest.files.wordpress.com/2011/12/restore-default.png"><img class="alignnone size-full wp-image-1475" title="Restore Default" src="http://anyrest.files.wordpress.com/2011/12/restore-default.png?w=630" alt=""   /></a></p>
<p>Well that&#8217;s great if you need to change 10 columns or less but what if you want to change the whole database? What if it&#8217;s a primary key or a foreign key? Well isn&#8217;t that a nightmare? Well I will give you an easy solution and all you need to do is to run 6 easy steps. If you don&#8217;t want to recreate the database and pump data by using DTS or SSIS then this is the solution for you just make sure backup and restore everything before doing any changes.</p>
<h3><span style="color:#3366ff;">Step 1: Prepare your DB and Change the collation to your desired one.</span></h3>
<p>Like I had said backup your database as a part of the preparation, once that&#8217;s done change your collation to the desired type by going to the database properties by right clicking on the database and choosing properties, once your on the properties window choose options and you can see the collation from there, choose what you want then hit OK. This will ensure that new objects created will be using the new collation.</p>
<h3><span style="color:#3366ff;">Step 2: Create you Change Collation Script.</span></h3>
<p>Next is to create a script to change the collation of every object in your database. You need to use the information_schema to extract columns needed to be changed and from there we run a loop on all objects creating alter scripts on each item. Since it is a collation change we will only need fields that uses character types and text types. What you need is to have a lot of commands similar to this</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">ALTER</span> <span style="color:blue;">TABLE</span> <span style="color:teal;">TABLENAME</span> <span style="color:blue;">ALTER</span> <span style="color:blue;">COLUMN</span> <span style="color:teal;">COLUMNNAME</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>100<span style="color:gray;">)</span> <span style="color:blue;">COLLATE</span> <span style="color:teal;">Latin1_General_CI_AS</span> <span style="color:gray;">NULL</span></pre>
<p>So here is the code to generate that</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">OPEN</span> <span style="color:teal;">MyTableCursor</span>

<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">WHILE</span> <span style="color:magenta;">@@FETCH_STATUS</span> <span style="color:gray;">=</span> 0
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">DECLARE</span> <span style="color:teal;">MyColumnCursor</span> <span style="color:blue;">Cursor</span>
		<span style="color:blue;">FOR</span> 
		<span style="color:blue;">SELECT</span> <span style="color:teal;">COLUMN_NAME</span><span style="color:gray;">,</span><span style="color:teal;">DATA_TYPE</span><span style="color:gray;">,</span> <span style="color:teal;">CHARACTER_MAXIMUM_LENGTH</span><span style="color:gray;">,</span><span style="color:teal;">IS_NULLABLE</span> <span style="color:blue;">from</span> <span style="color:lime;">information_schema</span><span style="color:gray;">.</span><span style="color:lime;">columns</span>
			<span style="color:blue;">WHERE</span> <span style="color:teal;">table_name</span> <span style="color:gray;">=</span> <span style="color:teal;">@TableName</span> <span style="color:gray;">AND</span><span style="color:blue;">  </span><span style="color:gray;">(</span><span style="color:teal;">Data_Type</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%char%'</span> <span style="color:gray;">OR</span> <span style="color:teal;">Data_Type</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%text%'</span><span style="color:gray;">)</span> <span style="color:gray;">AND</span> <span style="color:teal;">COLLATION_NAME</span> <span style="color:gray;">&lt;&gt;</span> <span style="color:teal;">@CollationName</span>
			<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> <span style="color:teal;">ordinal_position</span> 
		<span style="color:blue;">Open</span> <span style="color:teal;">MyColumnCursor</span>

		<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyColumnCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@ColumnName</span><span style="color:gray;">,</span> <span style="color:teal;">@DataType</span><span style="color:gray;">,</span> <span style="color:teal;">@CharacterMaxLen</span><span style="color:gray;">,</span> <span style="color:teal;">@IsNullable</span>
		<span style="color:blue;">WHILE</span> <span style="color:magenta;">@@FETCH_STATUS</span> <span style="color:gray;">=</span> 0
			<span style="color:blue;">BEGIN</span>
			<span style="color:blue;">SET</span> <span style="color:teal;">@SQLText</span> <span style="color:gray;">=</span> <span style="color:red;">'ALTER TABLE '</span> <span style="color:gray;">+</span> <span style="color:teal;">@TableName</span> <span style="color:gray;">+</span> <span style="color:red;">' ALTER COLUMN ['</span> <span style="color:gray;">+</span> <span style="color:teal;">@ColumnName</span> <span style="color:gray;">+</span> <span style="color:red;">'] '</span> <span style="color:gray;">+</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">+</span> <span style="color:red;">'('</span> <span style="color:gray;">+</span> <span style="color:blue;">CASE</span> <span style="color:blue;">WHEN</span> <span style="color:teal;">@CharacterMaxLen</span> <span style="color:gray;">=</span> <span style="color:gray;">-</span>1 <span style="color:blue;">THEN</span> <span style="color:red;">'MAX'</span> <span style="color:blue;">ELSE</span> <span style="color:teal;">@CharacterMaxLen</span> <span style="color:blue;">END</span> <span style="color:gray;">+</span> <span style="color:red;">') COLLATE '</span> <span style="color:gray;">+</span> <span style="color:teal;">@CollationName</span> <span style="color:gray;">+</span> <span style="color:red;">' '</span> <span style="color:gray;">+</span> <span style="color:blue;">CASE</span> <span style="color:blue;">WHEN</span> <span style="color:teal;">@IsNullable</span> <span style="color:gray;">=</span> <span style="color:red;">'NO'</span> <span style="color:blue;">THEN</span> <span style="color:red;">'NOT NULL'</span> <span style="color:blue;">ELSE</span> <span style="color:red;">'NULL'</span> <span style="color:blue;">END</span>
			<span style="color:blue;">PRINT</span> <span style="color:teal;">@SQLText</span> 

		<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyColumnCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@ColumnName</span><span style="color:gray;">,</span> <span style="color:teal;">@DataType</span><span style="color:gray;">,</span> <span style="color:teal;">@CharacterMaxLen</span><span style="color:gray;">,</span> <span style="color:teal;">@IsNullable</span>
		<span style="color:blue;">END</span>
		<span style="color:blue;">CLOSE</span> <span style="color:teal;">MyColumnCursor</span>
		<span style="color:blue;">DEALLOCATE</span> <span style="color:teal;">MyColumnCursor</span>

<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">END</span>
<span style="color:blue;">CLOSE</span> <span style="color:teal;">MyTableCursor</span>
<span style="color:blue;">DEALLOCATE</span> <span style="color:teal;">MyTableCursor</span></pre>
<p>Run it then save the script for later use. Lets call the script &#8220;ChangeCollation.sql&#8221;</p>
<p>If you don&#8217;t have relationships, primary keys and foreign keys then you don&#8217;t need to do the next step.</p>
<h3><span style="color:#3366ff;">Step 3: Create a Stored Procedure to Script Indexes and Relationships</span></h3>
<p>Well if you have relationships, primary keys and foreign keys then that&#8217;s a good practice but you need to script them as you need to drop those before changing the collation. Initially I thought I can do this with the wizard and choose to script indexes but it does not create on its own the table creation is always included so with a little help from Google I don&#8217;t have to write a single piece of code. I found this really good script to do it and I got it from here <a href="http://sqlblog.com/blogs/adam_machanic/archive/2010/04/04/rejuvinated-script-creates-and-drops-for-candidate-keys-and-referencing-foreign-keys.aspx" target="_blank">http://sqlblog.com/blogs/adam_machanic/archive/2010/04/04/rejuvinated-script-creates-and-drops-for-candidate-keys-and-referencing-foreign-keys.aspx</a><br />
I only separated the Create Indexes and Drop Indexes as we need to run a process in the middle.</p>
<p>Here is the Create Index script courtesy of <a href="http://sqlblog.com/blogs/adam_machanic/default.aspx" target="_blank">Adam Machanic</a></p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:green;">/*</span>
<span style="color:green;">Script Table Keys</span>
<span style="color:green;">(C) 2010 Adam Machanic - amachanic@gmail.com</span>
<span style="color:green;">http://sqlblog.com/blogs/adam_machanic/archive/2010/04/04/rejuvinated-script-creates-and-drops-for-candidate-keys-and-referencing-foreign-keys.aspx</span>
<span style="color:green;">This script produces a script of all of the candidate keys (primary keys or unique </span>
<span style="color:green;">constraints) as well as referencing foreign keys, for the target table. To use, put</span>
<span style="color:green;">SSMS into "results in text" mode and run the script. The output will be a formatted</span>
<span style="color:green;">script that you can cut and paste to use elsewhere.</span>

<span style="color:green;">Don't forget to configure the maximum text size before using. The default is 256</span>
<span style="color:green;">characters--not enough for many cases.</span>

<span style="color:green;">Tools-&gt;Options-&gt;Query Results-&gt;Results to Text-&gt;Maximum number of characters-&gt;8192</span>
<span style="color:green;">*/</span>
<span style="color:blue;">CREATE</span> <span style="color:blue;">PROC</span> <span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[ScriptCreateTableKeys]</span>
	<span style="color:teal;">@table_name</span> <span style="color:blue;">SYSNAME</span>
<span style="color:blue;">AS</span>
<span style="color:blue;">BEGIN</span>
	<span style="color:blue;">SET</span> <span style="color:blue;">NOCOUNT</span> <span style="color:blue;">ON</span>

	<span style="color:green;">--Note: Disabled keys and constraints are ignored</span>
	<span style="color:green;">--TODO: Drop and re-create referencing XML indexes, FTS catalogs</span>

	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@crlf</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>2<span style="color:gray;">)</span>
	<span style="color:blue;">SET</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">=</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>13<span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>10<span style="color:gray;">)</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@version</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>4<span style="color:gray;">)</span>
	<span style="color:blue;">SET</span> <span style="color:teal;">@version</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">@@VERSION</span><span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:red;">'Microsoft SQL Server'</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> 2<span style="color:gray;">,</span> 4<span style="color:gray;">)</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@object_id</span> <span style="color:blue;">INT</span>
	<span style="color:blue;">SET</span> <span style="color:teal;">@object_id</span> <span style="color:gray;">=</span> <span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:teal;">@table_name</span><span style="color:gray;">)</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@sql</span> <span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:magenta;">MAX</span><span style="color:gray;">)</span>

	<span style="color:blue;">IF</span> <span style="color:teal;">@version</span> <span style="color:gray;">NOT</span> <span style="color:gray;">IN</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">'2005'</span><span style="color:gray;">,</span> <span style="color:red;">'2008'</span><span style="color:gray;">)</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">RAISERROR</span><span style="color:gray;">(</span><span style="color:red;">'This script only supports SQL Server 2005 and 2008'</span><span style="color:gray;">,</span> 16<span style="color:gray;">,</span> 1<span style="color:gray;">)</span>
		<span style="color:blue;">RETURN</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">SET</span> <span style="color:teal;">@sql</span> <span style="color:gray;">=</span> <span style="color:red;">''</span> <span style="color:gray;">+</span>
		<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'CASE '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'WHEN 1 IN (i.is_primary_key, i.is_unique_constraint) THEN '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'''ALTER TABLE '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'QUOTENAME(OBJECT_SCHEMA_NAME(i.object_id)) + ''.'' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'QUOTENAME(OBJECT_NAME(i.object_id)) + @crlf + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'''ADD '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'CASE k.is_system_named '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'WHEN 0 THEN ''CONSTRAINT '' + QUOTENAME(k.name) + @crlf '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'ELSE '''' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'END + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'CASE k.type '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'WHEN ''UQ'' THEN ''UNIQUE'' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'ELSE ''PRIMARY KEY'' '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'END + '' '' + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'i.type_desc  + @crlf + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'kc.key_columns + @crlf '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'ELSE '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'''CREATE UNIQUE '' + i.type_desc + '' INDEX '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'QUOTENAME(i.name) + @crlf + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'''ON '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'QUOTENAME(OBJECT_SCHEMA_NAME(i.object_id)) + ''.'' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'QUOTENAME(OBJECT_NAME(i.object_id)) + @crlf + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'kc.key_columns + @crlf + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'COALESCE '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'''INCLUDE '' + @crlf + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'''( '' + @crlf + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'STUFF '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
											<span style="color:red;">''','' + @crlf + '' '' + QUOTENAME(c.name) AS [text()] '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'FROM sys.index_columns AS ic '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'JOIN sys.columns AS c ON '</span> <span style="color:gray;">+</span>
											<span style="color:red;">'c.object_id = ic.object_id '</span> <span style="color:gray;">+</span>
											<span style="color:red;">'AND c.column_id = ic.column_id '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
											<span style="color:red;">'ic.object_id = i.object_id '</span> <span style="color:gray;">+</span>
											<span style="color:red;">'AND ic.index_id = i.index_id '</span> <span style="color:gray;">+</span>
											<span style="color:red;">'AND ic.is_included_column = 1 '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'ORDER BY '</span> <span style="color:gray;">+</span>
											<span style="color:red;">'ic.key_ordinal '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'FOR XML PATH(''''), TYPE '</span> <span style="color:gray;">+</span>
									<span style="color:red;">').value(''.'', ''VARCHAR(MAX)'') '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'), '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'1, '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'3, '</span> <span style="color:gray;">+</span>
								<span style="color:red;">''''' '</span> <span style="color:gray;">+</span>
							<span style="color:red;">') + @crlf + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">''')'' + @crlf, '</span> <span style="color:gray;">+</span>
						<span style="color:red;">''''' '</span> <span style="color:gray;">+</span>
					<span style="color:red;">') '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'END + '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'''WITH '' + @crlf + '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'''('' + @crlf + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">''' PAD_INDEX = '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'CASE CONVERT(VARCHAR, i.is_padded) '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'WHEN 1 THEN ''ON'' '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'ELSE ''OFF'' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'END + '','' + @crlf + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'CASE i.fill_factor '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'WHEN 0 THEN '''' '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'ELSE '</span> <span style="color:gray;">+</span>
						<span style="color:red;">''' FILLFACTOR = '' + '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'CONVERT(VARCHAR, i.fill_factor) + '','' + @crlf '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'END + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">''' IGNORE_DUP_KEY = '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'CASE CONVERT(VARCHAR, i.ignore_dup_key) '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'WHEN 1 THEN ''ON'' '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'ELSE ''OFF'' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'END + '','' + @crlf + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">''' ALLOW_ROW_LOCKS = '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'CASE CONVERT(VARCHAR, i.allow_row_locks) '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'WHEN 1 THEN ''ON'' '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'ELSE ''OFF'' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'END + '','' + @crlf + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">''' ALLOW_PAGE_LOCKS = '' + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'CASE CONVERT(VARCHAR, i.allow_page_locks) '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'WHEN 1 THEN ''ON'' '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'ELSE ''OFF'' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'END + '</span> <span style="color:gray;">+</span>
				<span style="color:blue;">CASE</span> <span style="color:teal;">@version</span>
					<span style="color:blue;">WHEN</span> <span style="color:red;">'2005'</span> <span style="color:blue;">THEN</span> <span style="color:red;">''</span>
					<span style="color:blue;">ELSE</span>			 
						<span style="color:red;">''','' + @crlf + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">''' DATA_COMPRESSION = '' + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'CASE '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'WHEN MIN(p.data_compression_desc) = MAX(p.data_compression_desc) THEN MAX(p.data_compression_desc) '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'ELSE ''[PARTITIONS USE MULTIPLE COMPRESSION TYPES]'' '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'END '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'FROM sys.partitions AS p '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'p.object_id = i.object_id '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'AND p.index_id = i.index_id '</span> <span style="color:gray;">+</span>
							<span style="color:red;">') '</span>
				<span style="color:blue;">END</span> <span style="color:gray;">+</span> <span style="color:red;">'+ @crlf + '</span> <span style="color:gray;">+</span>
			<span style="color:red;">''') '' + @crlf + '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'''ON '' + ds.data_space + '';'' + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'@crlf + @crlf COLLATE database_default AS [-- Create Candidate Keys] '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'FROM sys.indexes AS i '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'LEFT OUTER JOIN sys.key_constraints AS k ON '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'k.parent_object_id = i.object_id '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'AND k.unique_index_id = i.index_id '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'CROSS APPLY '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'''( '' + @crlf + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'STUFF '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
									<span style="color:red;">''','' + @crlf + '' '' + QUOTENAME(c.name) AS [text()] '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'FROM sys.index_columns AS ic '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'JOIN sys.columns AS c ON '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'c.object_id = ic.object_id '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'AND c.column_id = ic.column_id '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'ic.object_id = i.object_id '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'AND ic.index_id = i.index_id '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'AND ic.key_ordinal &gt; 0 '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'ORDER BY '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'ic.key_ordinal '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'FOR XML PATH(''''), TYPE '</span> <span style="color:gray;">+</span>
							<span style="color:red;">').value(''.'', ''VARCHAR(MAX)'') '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'), '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'1, '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'3, '</span> <span style="color:gray;">+</span>
						<span style="color:red;">''''' '</span> <span style="color:gray;">+</span>
					<span style="color:red;">') + @crlf + '</span> <span style="color:gray;">+</span>
				<span style="color:red;">''')'' '</span> <span style="color:gray;">+</span>
		<span style="color:red;">') AS kc (key_columns) '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'CROSS APPLY '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'QUOTENAME(d.name) + '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'CASE d.type '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'WHEN ''PS'' THEN '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'+ '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'''('' + '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'QUOTENAME(c.name) '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'FROM sys.index_columns AS ic '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'JOIN sys.columns AS c ON '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'c.object_id = ic.object_id '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'AND c.column_id = ic.column_id '</span> <span style="color:gray;">+</span>
									<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'ic.object_id = i.object_id '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'AND ic.index_id = i.index_id '</span> <span style="color:gray;">+</span>
										<span style="color:red;">'AND ic.partition_ordinal = 1 '</span> <span style="color:gray;">+</span>
								<span style="color:red;">') + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">''')'' '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'ELSE '''' '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'END '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'FROM sys.data_spaces AS d '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'d.data_space_id = i.data_space_id '</span> <span style="color:gray;">+</span>
		<span style="color:red;">') AS ds (data_space) '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'i.object_id = @object_id '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'AND i.is_unique = 1 '</span> <span style="color:gray;">+</span>
			<span style="color:green;">--filtered and hypothetical indexes cannot be candidate keys</span>
			<span style="color:blue;">CASE</span> <span style="color:teal;">@version</span>
				<span style="color:blue;">WHEN</span> <span style="color:red;">'2008'</span> <span style="color:blue;">THEN</span> <span style="color:red;">'AND i.has_filter = 0 '</span>
				<span style="color:blue;">ELSE</span> <span style="color:red;">''</span>
			<span style="color:blue;">END</span> <span style="color:gray;">+</span>
			<span style="color:red;">'AND i.is_hypothetical = 0 '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'AND i.is_disabled = 0 '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'ORDER BY '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'i.index_id '</span>

	<span style="color:blue;">EXEC</span> <span style="color:maroon;">sp_executesql</span>
<span style="color:teal;">@sql</span><span style="color:gray;">,</span>
		<span style="color:red;">N'@object_id INT, @crlf CHAR(2)'</span><span style="color:gray;">,</span>
		<span style="color:teal;">@object_id</span><span style="color:gray;">,</span> <span style="color:teal;">@crlf</span>

	<span style="color:blue;">SELECT</span>
		<span style="color:red;">'ALTER TABLE '</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:teal;">parent_object_id</span><span style="color:gray;">))</span> <span style="color:gray;">+</span> <span style="color:red;">'.'</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:teal;">parent_object_id</span><span style="color:gray;">))</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span>
		<span style="color:blue;">CASE</span> <span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:teal;">is_not_trusted</span>
			<span style="color:blue;">WHEN</span> 0 <span style="color:blue;">THEN</span> <span style="color:red;">'WITH CHECK '</span>
			<span style="color:blue;">ELSE</span> <span style="color:red;">'WITH NOCHECK '</span>
		<span style="color:blue;">END</span> <span style="color:gray;">+</span> 
			<span style="color:red;">'ADD '</span> <span style="color:gray;">+</span>
				<span style="color:blue;">CASE</span> <span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:teal;">is_system_named</span>
					<span style="color:blue;">WHEN</span> 0 <span style="color:blue;">THEN</span> <span style="color:red;">'CONSTRAINT '</span> <span style="color:gray;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:teal;">name</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span>
					<span style="color:blue;">ELSE</span> <span style="color:red;">''</span>
				<span style="color:blue;">END</span> <span style="color:gray;">+</span>
		<span style="color:red;">'FOREIGN KEY '</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> 
		<span style="color:red;">'( '</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">STUFF</span>
<span style="color:gray;">(</span>
<span style="color:gray;">(</span>
					<span style="color:blue;">SELECT</span>
<span style="color:gray;">(</span>
						<span style="color:blue;">SELECT</span> 
							<span style="color:red;">','</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> <span style="color:red;">' '</span> <span style="color:gray;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:teal;">c</span><span style="color:gray;">.</span><span style="color:teal;">name</span><span style="color:gray;">)</span> <span style="color:blue;">AS</span> <span style="color:teal;">[text()]</span>
						<span style="color:blue;">FROM</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">foreign_key_columns</span> <span style="color:blue;">AS</span> <span style="color:teal;">fc</span>
						<span style="color:gray;">JOIN</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">columns</span> <span style="color:blue;">AS</span> <span style="color:teal;">c</span> <span style="color:blue;">ON</span>
							<span style="color:teal;">c</span><span style="color:gray;">.</span><span style="color:magenta;">object_id</span> <span style="color:gray;">=</span> <span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">parent_object_id</span>
							<span style="color:gray;">AND</span> <span style="color:teal;">c</span><span style="color:gray;">.</span><span style="color:teal;">column_id</span> <span style="color:gray;">=</span> <span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">parent_column_id</span>
						<span style="color:blue;">WHERE</span> 
							<span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">constraint_object_id</span> <span style="color:gray;">=</span> <span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:magenta;">object_id</span>
						<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span>
							<span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">constraint_column_id</span>
						<span style="color:blue;">FOR</span> <span style="color:blue;">XML</span> <span style="color:blue;">PATH</span><span style="color:gray;">(</span><span style="color:red;">''</span><span style="color:gray;">),</span> <span style="color:blue;">TYPE</span>
					<span style="color:gray;">).</span><span style="color:teal;">value</span><span style="color:gray;">(</span><span style="color:red;">'.'</span><span style="color:gray;">,</span> <span style="color:red;">'VARCHAR(MAX)'</span><span style="color:gray;">)</span>
				<span style="color:gray;">),</span>
				1<span style="color:gray;">,</span>
				3<span style="color:gray;">,</span>
				<span style="color:red;">''</span>
			<span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> 
		<span style="color:red;">') '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'REFERENCES '</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:teal;">referenced_object_id</span><span style="color:gray;">))</span> <span style="color:gray;">+</span> <span style="color:red;">'.'</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:teal;">referenced_object_id</span><span style="color:gray;">))</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span>
		<span style="color:red;">'( '</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">STUFF</span>
<span style="color:gray;">(</span>
<span style="color:gray;">(</span>
					<span style="color:blue;">SELECT</span>
<span style="color:gray;">(</span>
						<span style="color:blue;">SELECT</span> 
							<span style="color:red;">','</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> <span style="color:red;">' '</span> <span style="color:gray;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:teal;">c</span><span style="color:gray;">.</span><span style="color:teal;">name</span><span style="color:gray;">)</span> <span style="color:blue;">AS</span> <span style="color:teal;">[text()]</span>
						<span style="color:blue;">FROM</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">foreign_key_columns</span> <span style="color:blue;">AS</span> <span style="color:teal;">fc</span>
						<span style="color:gray;">JOIN</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">columns</span> <span style="color:blue;">AS</span> <span style="color:teal;">c</span> <span style="color:blue;">ON</span>
							<span style="color:teal;">c</span><span style="color:gray;">.</span><span style="color:magenta;">object_id</span> <span style="color:gray;">=</span> <span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">referenced_object_id</span>
							<span style="color:gray;">AND</span> <span style="color:teal;">c</span><span style="color:gray;">.</span><span style="color:teal;">column_id</span> <span style="color:gray;">=</span> <span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">referenced_column_id</span>
						<span style="color:blue;">WHERE</span> 
							<span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">constraint_object_id</span> <span style="color:gray;">=</span> <span style="color:teal;">fk</span><span style="color:gray;">.</span><span style="color:magenta;">object_id</span>
						<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span>
							<span style="color:teal;">fc</span><span style="color:gray;">.</span><span style="color:teal;">constraint_column_id</span>
						<span style="color:blue;">FOR</span> <span style="color:blue;">XML</span> <span style="color:blue;">PATH</span><span style="color:gray;">(</span><span style="color:red;">''</span><span style="color:gray;">),</span> <span style="color:blue;">TYPE</span>
					<span style="color:gray;">).</span><span style="color:teal;">value</span><span style="color:gray;">(</span><span style="color:red;">'.'</span><span style="color:gray;">,</span> <span style="color:red;">'VARCHAR(MAX)'</span><span style="color:gray;">)</span>
				<span style="color:gray;">),</span>
				1<span style="color:gray;">,</span>
				3<span style="color:gray;">,</span>
				<span style="color:red;">''</span>
			<span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> 
		<span style="color:red;">');'</span> <span style="color:gray;">+</span> 
			<span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:blue;">COLLATE</span> <span style="color:teal;">database_default</span> <span style="color:blue;">AS</span> <span style="color:teal;">[-- Create Referencing FKs]</span>
	<span style="color:blue;">FROM</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">foreign_keys</span> <span style="color:blue;">AS</span> <span style="color:teal;">fk</span>
	<span style="color:blue;">WHERE</span>
		<span style="color:teal;">referenced_object_id</span> <span style="color:gray;">=</span> <span style="color:teal;">@object_id</span>
		<span style="color:gray;">AND</span> <span style="color:teal;">is_disabled</span> <span style="color:gray;">=</span> 0
	<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span>
		<span style="color:teal;">key_index_id</span>

<span style="color:blue;">END</span></pre>
<h3><span style="color:#3366ff;">Step 4: Create Stored Procedure to Script Drop Indexes and Relationships</span></h3>
<p>Now you also need to create the drop scripts, these is the other half of <a href="http://sqlblog.com/blogs/adam_machanic/default.aspx" target="_blank">Adam Machanic&#8217;</a>s script</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">CREATE</span> <span style="color:blue;">PROC</span> <span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[ScriptDropTableKeys]</span>
	<span style="color:teal;">@table_name</span> <span style="color:blue;">SYSNAME</span>
<span style="color:blue;">AS</span>
<span style="color:blue;">BEGIN</span>
	<span style="color:blue;">SET</span> <span style="color:blue;">NOCOUNT</span> <span style="color:blue;">ON</span>

	<span style="color:green;">--Note: Disabled keys and constraints are ignored</span>
	<span style="color:green;">--TODO: Drop and re-create referencing XML indexes, FTS catalogs</span>

	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@crlf</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>2<span style="color:gray;">)</span>
	<span style="color:blue;">SET</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">=</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>13<span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>10<span style="color:gray;">)</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@version</span> <span style="color:blue;">CHAR</span><span style="color:gray;">(</span>4<span style="color:gray;">)</span>
	<span style="color:blue;">SET</span> <span style="color:teal;">@version</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:magenta;">@@VERSION</span><span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:red;">'Microsoft SQL Server'</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> 2<span style="color:gray;">,</span> 4<span style="color:gray;">)</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@object_id</span> <span style="color:blue;">INT</span>
	<span style="color:blue;">SET</span> <span style="color:teal;">@object_id</span> <span style="color:gray;">=</span> <span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:teal;">@table_name</span><span style="color:gray;">)</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@sql</span> <span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:magenta;">MAX</span><span style="color:gray;">)</span>

	<span style="color:blue;">IF</span> <span style="color:teal;">@version</span> <span style="color:gray;">NOT</span> <span style="color:gray;">IN</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">'2005'</span><span style="color:gray;">,</span> <span style="color:red;">'2008'</span><span style="color:gray;">)</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">RAISERROR</span><span style="color:gray;">(</span><span style="color:red;">'This script only supports SQL Server 2005 and 2008'</span><span style="color:gray;">,</span> 16<span style="color:gray;">,</span> 1<span style="color:gray;">)</span>
		<span style="color:blue;">RETURN</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">SELECT</span>
		<span style="color:red;">'ALTER TABLE '</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:teal;">parent_object_id</span><span style="color:gray;">))</span> <span style="color:gray;">+</span> <span style="color:red;">'.'</span> <span style="color:gray;">+</span> 
			<span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:teal;">parent_object_id</span><span style="color:gray;">))</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:gray;">+</span>
		<span style="color:red;">'DROP CONSTRAINT '</span> <span style="color:gray;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:gray;">(</span><span style="color:teal;">name</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:red;">';'</span> <span style="color:gray;">+</span> 
			<span style="color:teal;">@crlf</span> <span style="color:gray;">+</span> <span style="color:teal;">@crlf</span> <span style="color:blue;">COLLATE</span> <span style="color:teal;">database_default</span> <span style="color:blue;">AS</span> <span style="color:teal;">[-- Drop Referencing FKs]</span>
	<span style="color:blue;">FROM</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">foreign_keys</span>
	<span style="color:blue;">WHERE</span>
		<span style="color:teal;">referenced_object_id</span> <span style="color:gray;">=</span> <span style="color:teal;">@object_id</span>
		<span style="color:gray;">AND</span> <span style="color:teal;">is_disabled</span> <span style="color:gray;">=</span> 0
	<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span>
		<span style="color:teal;">key_index_id</span> <span style="color:blue;">DESC</span>

	<span style="color:blue;">SET</span> <span style="color:teal;">@sql</span> <span style="color:gray;">=</span> <span style="color:red;">''</span> <span style="color:gray;">+</span>
		<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'statement AS [-- Drop Candidate Keys] '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'FROM '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'( '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'SELECT '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'CASE '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'WHEN 1 IN (i.is_unique_constraint, i.is_primary_key) THEN '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'''ALTER TABLE '' + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'QUOTENAME(OBJECT_SCHEMA_NAME(i.object_id)) + ''.'' + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'QUOTENAME(OBJECT_NAME(i.object_id)) + @crlf + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'''DROP CONSTRAINT '' + QUOTENAME(i.name) + '';'' + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'@crlf + @crlf COLLATE database_default '</span> <span style="color:gray;">+</span>
					<span style="color:red;">'ELSE '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'''DROP INDEX '' + QUOTENAME(i.name) + @crlf + '</span> <span style="color:gray;">+</span>
						<span style="color:red;">'''ON '' + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + ''.'' + '</span> <span style="color:gray;">+</span>
							<span style="color:red;">'QUOTENAME(OBJECT_NAME(object_id)) + '';'' + '</span> <span style="color:gray;">+</span>
								<span style="color:red;">'@crlf + @crlf COLLATE database_default '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'END AS statement, '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'i.index_id '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'FROM sys.indexes AS i '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'WHERE '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'i.object_id = @object_id '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'AND i.is_unique = 1 '</span> <span style="color:gray;">+</span>
				<span style="color:green;">--filtered and hypothetical indexes cannot be candidate keys</span>
				<span style="color:blue;">CASE</span> <span style="color:teal;">@version</span>
					<span style="color:blue;">WHEN</span> <span style="color:red;">'2008'</span> <span style="color:blue;">THEN</span> <span style="color:red;">'AND i.has_filter = 0 '</span>
					<span style="color:blue;">ELSE</span> <span style="color:red;">''</span>
				<span style="color:blue;">END</span> <span style="color:gray;">+</span>
				<span style="color:red;">'AND i.is_hypothetical = 0 '</span> <span style="color:gray;">+</span>
				<span style="color:red;">'AND i.is_disabled = 0 '</span> <span style="color:gray;">+</span>
		<span style="color:red;">') AS x '</span> <span style="color:gray;">+</span>
		<span style="color:red;">'ORDER BY '</span> <span style="color:gray;">+</span>
			<span style="color:red;">'index_id DESC '</span>

	<span style="color:blue;">EXEC</span> <span style="color:maroon;">sp_executesql</span><span style="color:blue;"> </span>
<span style="color:teal;">@sql</span><span style="color:gray;">,</span>
		<span style="color:red;">N'@object_id INT, @crlf CHAR(2)'</span><span style="color:gray;">,</span>
		<span style="color:teal;">@object_id</span><span style="color:gray;">,</span> <span style="color:teal;">@crlf</span>

<span style="color:blue;">END</span></pre>
<h3><span style="color:#3366ff;">Step 5: Bringing them all together</span></h3>
<p>Now you have the two stored procedure all you have to do is to loop though all tables in you database and pass that as the parameter of the Stored Procedure. First we use the ScriptCreateTableKeys.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">DECLARE</span> <span style="color:teal;">@TableName</span> <span style="color:blue;">nvarchar</span><span style="color:gray;">(</span>255<span style="color:gray;">)</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">Cursor</span>
<span style="color:blue;">FOR</span> 
<span style="color:blue;">SELECT</span> <span style="color:teal;">name</span> <span style="color:blue;">FROM</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">tables</span> <span style="color:blue;">WHERE</span> <span style="color:teal;">[type]</span> <span style="color:gray;">=</span> <span style="color:red;">'U'</span> <span style="color:gray;">and</span> <span style="color:teal;">name</span> <span style="color:gray;">&lt;&gt;</span> <span style="color:red;">'sysdiagrams'</span> <span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> <span style="color:teal;">name</span> 
<span style="color:blue;">OPEN</span> <span style="color:teal;">MyTableCursor</span>

<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">WHILE</span> <span style="color:magenta;">@@FETCH_STATUS</span> <span style="color:gray;">=</span> 0
	<span style="color:blue;">BEGIN</span>
	<span style="color:blue;">EXEC</span> <span style="color:teal;">ScriptCreateTableKeys</span><span style="color:blue;"> </span><span style="color:teal;">@TableName</span>

	<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">END</span>
<span style="color:blue;">CLOSE</span> <span style="color:teal;">MyTableCursor</span>
<span style="color:blue;">DEALLOCATE</span> <span style="color:teal;">MyTableCursor</span></pre>
<p>Then lets use the ScriptDropTableKeys</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">DECLARE</span> <span style="color:teal;">@TableName</span> <span style="color:blue;">nvarchar</span><span style="color:gray;">(</span>255<span style="color:gray;">)</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">Cursor</span>
<span style="color:blue;">FOR</span> 
<span style="color:blue;">SELECT</span> <span style="color:teal;">name</span> <span style="color:blue;">FROM</span> <span style="color:lime;">sys</span><span style="color:gray;">.</span><span style="color:lime;">tables</span> <span style="color:blue;">WHERE</span> <span style="color:teal;">[type]</span> <span style="color:gray;">=</span> <span style="color:red;">'U'</span> <span style="color:gray;">and</span> <span style="color:teal;">name</span> <span style="color:gray;">&lt;&gt;</span> <span style="color:red;">'sysdiagrams'</span> <span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> <span style="color:teal;">name</span> 
<span style="color:blue;">OPEN</span> <span style="color:teal;">MyTableCursor</span>

<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">WHILE</span> <span style="color:magenta;">@@FETCH_STATUS</span> <span style="color:gray;">=</span> 0
	<span style="color:blue;">BEGIN</span>
	 <span style="color:blue;">EXEC</span> <span style="color:teal;">ScriptDropTableKeys</span><span style="color:blue;"> </span><span style="color:teal;">@TableName</span>

	<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">MyTableCursor</span> <span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">END</span>
<span style="color:blue;">CLOSE</span> <span style="color:teal;">MyTableCursor</span>
<span style="color:blue;">DEALLOCATE</span> <span style="color:teal;">MyTableCursor</span></pre>
<p>Just make sure when you execute them output the results as text so you can easily copy and paste the results.<br />
Save the first results as &#8220;CreateKeysAndIndexes.sql&#8221; and the second as &#8220;DropKeysAndIndexes.sql&#8221;</p>
<h3><span style="color:#3366ff;">Step 6: Run you saved scripts</span></h3>
<p>In this order run your scripts and wait for the results, time wait might vary depending on your database size.</p>
<p>a. DropKeysAndIndexes.sql<br />
b. ChangeCollation.sql<br />
c. CreateKeysAndIndexes.sql</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1472/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1472/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1472/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1472/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1472/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1472/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1472/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1472/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1472&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/12/19/the-easy-way-of-changing-collation-of-all-database-objects-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/12/change-db-collation.png" medium="image">
			<media:title type="html">Change Db Collation</media:title>
		</media:content>

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

		<media:content url="http://anyrest.files.wordpress.com/2011/12/restore-default.png" medium="image">
			<media:title type="html">Restore Default</media:title>
		</media:content>
	</item>
		<item>
		<title>Not all files showing on Twonky Media Server / Vodafone Broadband Complete HG553</title>
		<link>http://anyrest.wordpress.com/2011/11/22/not-all-files-showing-on-twonky-media-server-vodafone-broadband-complete-hg553/</link>
		<comments>http://anyrest.wordpress.com/2011/11/22/not-all-files-showing-on-twonky-media-server-vodafone-broadband-complete-hg553/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 10:00:12 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Hack]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1454</guid>
		<description><![CDATA[This post will not be programming related but more of a simple hack on a device I just recently acquired, anyways this post still is technology related and I wanted to share this simple solution that others might find useful.  Recently I moved to a different internet provider as my ADSL modem and Wi-Fi Router [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1454&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post will not be programming related but more of a simple hack on a device I just recently acquired, anyways this post still is technology related and I wanted to share this simple solution that others might find useful.  Recently I moved to a different internet provider as my ADSL modem and Wi-Fi Router is getting old, service price is also expensive compared to the new offers currently on the market and there&#8217;s a really good offer on <a href="http://www.vodafone.co.nz/broadband-wireless-router/" target="_blank">Vodafone NZ</a> where they give a boradband modem device for free while keeping the rates reasonable compared to similar data plans. The device that I am talking about is a ADSL2+ Modem with Wi-Fi Router that has <a href="http://www.dlna.org/home" target="_blank">DLNA</a>, 2 USB ports and 1 USB/HSPA port (for wireless modem).  I was searching before for a similar device and the only ones out there are limited to <a href="http://www.buffalo-technology.com/products/wireless/wireless-n-nfiniti/wzr-hp-g300nh-airstation-nfiniti-wireless-n-high-power-router/" target="_blank">Buffalo WZR-HP-G300NH</a>, <a href="http://www.netgear.com.au/home/products/wirelessrouters/high-performance/dgnd3700.aspx" target="_blank">Netgear N600</a> and <a href="http://home.cisco.com/en-eu/products/gateways/WAG320N" target="_blank">Linksys WAG320N</a> which all are ADSL2+ Modem Wi-Fi Router with DLNA and USB ports, well I am out of luck as we don’t have it here in New Zealand and the next best thing that I could only have is the  <a href="http://www.netgear.com/home/products/mobile-broadband/dsl-3g-mobile-broadband/dgn2200m.aspx" target="_blank">Netgear DGN2200M</a> which costs NZ$185 and that’s not DLNA certified yet.  Now you might ask why DLNA, well I have a lot of DLNA devices at home and I want to stream media files such as music, video and photos on those devices and a single device that can integrate them all while staying on a budget would be good.  Given that scenario Vodafone have my answer by giving their device free, now I saved NZ$185 for the device and $15/month on phone and internet bills and my data plan increased by 35GB how good is that (Note: this is not an ad, I am just happy with my experience with Vodafone).</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/hg533.png"><img class="alignleft size-full wp-image-1455" title="HG533" src="http://anyrest.files.wordpress.com/2011/11/hg533.png?w=630" alt=""   /></a>This device that I am talking about is a rebranded Huawei Echolife HG553 for a full information on the product here is a <a href="http://www.vodafone.co.nz/documents/pdf/modem-guides/vodafone-broadband-complete-ug.pdf" target="_blank">downloadable manual from Vodafone</a>, it’s an amazing device and does DLNA very well now I stream media files from this device to my Smart TV, 3D Home Theatre, Android Devices, iPhones, PS3, PC&#8217;s and even an old XBOX with XBMC installed I don’t have to plug and unplug my USB hard drive to share media at home.  The sharing methods are through DLNA, FTP and/or a normal network file share; it also has a Print Server for those who have non-wireless printers at home.  Now let’s go to my problem, when I plugged the 2TB USB hard drive (take note it can take FAT or NTFS formatted drives) it immediately read the media files on it and it indexed it really fast, now I tried to browse the media files on my TV and I only see some of the media files on the USB drive.   I was worried there might be something wrong with the device so I browsed the hard drive though a PC and check whether files are in there, to my surprise it was there and was wondering why it was not indexed.  Knowing that it’s a <a href="http://www.twonky.com/" target="_blank">Twonky</a> software that works on the background, I immediately contacted their support, they replied quick but did not get anywhere as they don’t support OEM device, and here was their response which is fair enough.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/twonky-support.jpg"><img class="alignnone size-full wp-image-1456" title="Twonky Support" src="http://anyrest.files.wordpress.com/2011/11/twonky-support.jpg?w=630&#038;h=320" alt="" width="630" height="320" /></a></p>
<p>Now I contacted Vodafone through their support forum, well I got a really bad answer which is totally unrelated to my question, a Vodafone forum administrator told me that I was posting it in a wrong location, so I moved it to the right one and got no answer after that, I did not mind to call their support as support in any Internet Service providers here in NZ sucks in my experience the average waiting time was 30 minutes and sometimes you don’t get the answer you are looking for specially if I am talking to a less technical person than me.  So anyway here was my post on the forum for those who are interested.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/vodafone-support.jpg"><img class="alignnone size-full wp-image-1457" title="Vodafone Support" src="http://anyrest.files.wordpress.com/2011/11/vodafone-support.jpg?w=630&#038;h=378" alt="" width="630" height="378" /></a><br />
At this point I am on my own, so I tried everything by logging in to the router as support, Vodafone and advanced (all yield a different screen) and the only setting I find regarding DLNA is enabling and disabling it. I even went to the port http://192.168.1.1:9000 as that&#8217;s the advanced setup default for Twonky and get the red access denied screen.  Now I browsed through the files in the shared hard drive and found out that it has a twonkymedia.db folder.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/image2.png"><img class="alignnone size-full wp-image-1458" title="image2" src="http://anyrest.files.wordpress.com/2011/11/image2.png?w=630&#038;h=346" alt="" width="630" height="346" /></a></p>
<p>So I went in and saw an .ini file, at last there is hope!</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/image3.png"><img class="alignnone size-full wp-image-1459" title="image3" src="http://anyrest.files.wordpress.com/2011/11/image3.png?w=630&#038;h=206" alt="" width="630" height="206" /></a></p>
<p>While reading the settings it looks like the most probable cause was an item called maxitemsmaxitems=10000#, now I counted how many media files I got on my hard drive and its more than 50K items most of it are images, that definitely is the reason why it’s not showing everything.  So I changed the settings to 100000, saved and rebooted the device. I browsed again once the device started using my TV and no luck, so I went back to the .ini file and found out it went back to its normal state, it looks like the device rewrites the ini file on reboot.  I also tried not rebooting, changed the maxitemsmaxitems=10000# to maxitemsmaxitems=100000# and wait for 60 minutes and that&#8217;s also a setting called minutesscantime=60# hoping that it will rescan and change the limit to 100000 but still no luck.  Finally I saw this setting ignoredirignoredir=AppleDouble,AppleDB,AppleDesktop,TemporaryUtems#, and if it’s true to its name it looks like it will ignore anything inside a directory.  So I created an AppleDB folder and placed everything there leaving only the files I want to stream on my PS3, Smart TV and 3D Home Theatre, videos are more logical as they are the ones you usually play on those device, I then rebooted and whoalla!</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/image.png"><img class="alignnone size-full wp-image-1460" title="image" src="http://anyrest.files.wordpress.com/2011/11/image.png?w=630&#038;h=299" alt="" width="630" height="299" /></a></p>
<p>Now it’s showing all the videos I wanted and not the massive amounts of photos from 1999 to 2011 which kept my videos not showing due to the file count limit imposed.  I don’t care about the photos as we usually view them on Laptops and Android Devices which a normal share and mapped drive to the router will be suitable and no need for DLNA.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1454/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1454/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1454/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1454&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/11/22/not-all-files-showing-on-twonky-media-server-vodafone-broadband-complete-hg553/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

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

		<media:content url="http://anyrest.files.wordpress.com/2011/11/twonky-support.jpg" medium="image">
			<media:title type="html">Twonky Support</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/11/vodafone-support.jpg" medium="image">
			<media:title type="html">Vodafone Support</media:title>
		</media:content>

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

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

		<media:content url="http://anyrest.files.wordpress.com/2011/11/image.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Sharepoint List View Threshold Error</title>
		<link>http://anyrest.wordpress.com/2011/11/17/sharepoint-list-view-threshold-error/</link>
		<comments>http://anyrest.wordpress.com/2011/11/17/sharepoint-list-view-threshold-error/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 10:00:02 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1446</guid>
		<description><![CDATA[You might have encountered the error below in Sharepoint thats why you stumbled upon this site. The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.&#60;nativehr&#62;0&#215;80070024&#60;/nativehr&#62;&#60;nativestack&#62;&#60;/nativestack&#62; Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1446&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You might have encountered the error below in Sharepoint thats why you stumbled upon this site.</p>
<h2><span style="color:#ff0000;"><em>The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.&lt;nativehr&gt;0&#215;80070024&lt;/nativehr&gt;&lt;nativestack&gt;&lt;/nativestack&gt;</em></span></h2>
<p><span style="font-size:x-small;"><span style="font-family:verdana,sans-serif;"><span style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif;"><strong>Description: </strong>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</span></span></span></p>
<p><strong>Exception Details: </strong>System.Runtime.InteropServices.COMException: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.0&#215;80070024&lt;/nativehr&gt;&lt;nativestack&gt;&lt;/nativestack&gt;</p>
<p><strong>Source Error:</strong></p>
<table width="100%" bgcolor="#ffffcc">
<tbody>
<tr>
<td><code>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code></td>
</tr>
</tbody>
</table>
<p>Before I give you the solution, lets see why does it happen on the first place so you can better understand what you are fixing.  The error basically about the size of the database operation that you are executing in one go, which means it can be viewing a list, loading a page or other database related operations.  That error simply says that the number of operations that exceed the limit set on the server this is now treated as an Expensive Query Exception.  Now you might notice that your user&#8217;s experience this error but not yourself this is because if you are an Administrator then you have higher thresholds.</p>
<p>Now how do you adjust this thresholds.  All you have to do is go to Central Administration -&gt; Application Management -&gt; Manage Web Applications then highlight the Name of the site you are adjusting the threshold</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/resource-throttling.jpg"><img class="alignnone size-full wp-image-1448" title="Resource Throttling" src="http://anyrest.files.wordpress.com/2011/11/resource-throttling.jpg?w=630&#038;h=379" alt="" width="630" height="379" /></a></p>
<p>Then choose General Settings on the ribbon, then Resource Throttling.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/resource-throttling-2.jpg"><img class="alignnone size-full wp-image-1449" title="Resource Throttling 2" src="http://anyrest.files.wordpress.com/2011/11/resource-throttling-2.jpg?w=630&#038;h=393" alt="" width="630" height="393" /></a></p>
<p>Now you can change your setting on the <strong>List View Threshold</strong>, by default it is 5000 so you can increase it to something like 10000 to double the operation count.  Now you can also see the setting List <strong>View Threshold for Auditors and Administrators</strong>, that&#8217;s the reason why you don&#8217;t see the error as it is defaulted to 20000, but if you are an administrator and you have the same issue you can increase both settings.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/11/threshold-settings.jpg"><img class="alignnone size-full wp-image-1450" title="Threshold Settings" src="http://anyrest.files.wordpress.com/2011/11/threshold-settings.jpg?w=630" alt=""   /></a></p>
<p>This should fix your issue now, so go back what you were doing before it happened and it will not throw any exceptions anymore.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1446/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1446/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1446/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1446&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/11/17/sharepoint-list-view-threshold-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/11/resource-throttling.jpg" medium="image">
			<media:title type="html">Resource Throttling</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/11/resource-throttling-2.jpg" medium="image">
			<media:title type="html">Resource Throttling 2</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/11/threshold-settings.jpg" medium="image">
			<media:title type="html">Threshold Settings</media:title>
		</media:content>
	</item>
		<item>
		<title>Telerik MVC Grid workaround for not rendering properly in Internet Explorer</title>
		<link>http://anyrest.wordpress.com/2011/09/29/telerik-mvc-grid-workaround-for-not-rendering-properly-in-internet-explorer/</link>
		<comments>http://anyrest.wordpress.com/2011/09/29/telerik-mvc-grid-workaround-for-not-rendering-properly-in-internet-explorer/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 11:00:38 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Telerik]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1439</guid>
		<description><![CDATA[If you are working with Telerik MVC Grid and Internet Explorer you might have encountered an issue where columns and rows are not rendered properly.  You will notice rows are not in line with their respective columns and even grid lines are not lining properly.  Look at some screen shots below from different browsers I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1439&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are working with Telerik MVC Grid and Internet Explorer you might have encountered an issue where columns and rows are not rendered properly.  You will notice rows are not in line with their respective columns and even grid lines are not lining properly.  Look at some screen shots below from different browsers I used</p>
<p><strong>Firefox Screenshot</strong> &#8211; No issues here</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/firefox.jpg"><img class="alignnone size-full wp-image-1440" title="FireFox" src="http://anyrest.files.wordpress.com/2011/09/firefox.jpg?w=630&#038;h=236" alt="" width="630" height="236" /></a></p>
<p><strong>Chrome Screenshot</strong> &#8211; No issues here</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/chrome.jpg"><img class="alignnone size-full wp-image-1441" title="Chrome" src="http://anyrest.files.wordpress.com/2011/09/chrome.jpg?w=630&#038;h=246" alt="" width="630" height="246" /></a></p>
<p><strong>Internet Explorer Screenshot</strong> &#8211; Well I guess the issue is visible columns dont line up and headers dont line up as well</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/internet-explorer.jpg"><img class="alignnone size-full wp-image-1442" title="Internet Explorer" src="http://anyrest.files.wordpress.com/2011/09/internet-explorer.jpg?w=630&#038;h=275" alt="" width="630" height="275" /></a></p>
<p>Now the real issue is that you have a hidden field and there is where Internet Explorer f@&lt;#$!! up.  Originally your code might look like this</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@(</span>Html.Telerik().Grid&lt;<span style="color:#2b91af;">InvoiceViewModel</span>&gt;()
    .Name(<span style="color:#a31515;">"grdTransactions"</span> + Model.GridViewType)
    .DataKeys(d =&gt; d.Add(a =&gt; a.Id))
    .ToolBar(t =&gt; t.SubmitChanges())
    .Columns(c =&gt;
    {
        c.Add(a =&gt; a.Id).Hidden();
        c.Add(a =&gt; a.BookingID).Width(90);
        c.Add(a =&gt; a.TransactionNumber).Width(90).Title(<span style="color:#a31515;">"Tran No"</span>);
        c.Add(a =&gt; a.Description);
        c.Add(a =&gt; a.TicketNumber).Width(120);
        c.Add(a =&gt; a.Passengers).Width(200);
        c.Add(a =&gt; a.InvoiceNett).Width(90).Format(<span style="color:#a31515;">"{0:c}"</span>).HtmlAttributes(<span style="color:blue;">new</span> { align = <span style="color:#a31515;">"right"</span> });
        c.Add(a =&gt; a.InvoiceGST).Width(90).Format(<span style="color:#a31515;">"{0:c}"</span>).HtmlAttributes(<span style="color:blue;">new</span> { align = <span style="color:#a31515;">"right"</span> });
        c.Add(a =&gt; a.InvoiceGross).Width(90).Format(<span style="color:#a31515;">"{0:c}"</span>).HtmlAttributes(<span style="color:blue;">new</span> { align = <span style="color:#a31515;">"right"</span> });
    })
    .DataBinding(d =&gt; d.Ajax()
        .OperationMode(<span style="color:#2b91af;">GridOperationMode</span>.Client)
        .Select(<span style="color:#a31515;">"SelectTransactions"</span>, <span style="color:#a31515;">"Invoicing"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId, invoiceId = Model.InvoiceId })
        .Update(<span style="color:#a31515;">"UpdateTransactions"</span>, <span style="color:#a31515;">"Invoicing"</span>, <span style="color:blue;">new</span> { accountId = Model.AccountId, invoiceId = Model.InvoiceId }))
    .Pageable(p =&gt; p.PageSize(50))
    .Editable(e =&gt; e.Mode(<span style="color:#2b91af;">GridEditMode</span>.InCell))
    .Sortable()
    .Scrollable()
    .Groupable()
    .Filterable()
    .ClientEvents(c =&gt; c.OnLoad(<span style="color:#a31515;">"onLoad"</span>)
        .OnDataBound(<span style="color:#a31515;">"onDataBound"</span>)
        .OnSubmitChanges(<span style="color:#a31515;">"onSubmitChanges"</span>))              
    <span style="background:yellow;">)</span></pre>
<p>All you need to do is move that hidden column to the end so Internet Explorer will not get confused, so your new column structure should look like such</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;">.Columns(c =&gt;
{
    c.Add(a =&gt; a.BookingID).Width(90);
    c.Add(a =&gt; a.TransactionNumber).Width(90).Title(<span style="color:#a31515;">"Tran No"</span>);
    c.Add(a =&gt; a.Description);
    c.Add(a =&gt; a.TicketNumber).Width(120);
    c.Add(a =&gt; a.Passengers).Width(200);
    c.Add(a =&gt; a.InvoiceNett).Width(90).Format(<span style="color:#a31515;">"{0:c}"</span>).HtmlAttributes(<span style="color:blue;">new</span> { align = <span style="color:#a31515;">"right"</span> });
    c.Add(a =&gt; a.InvoiceGST).Width(90).Format(<span style="color:#a31515;">"{0:c}"</span>).HtmlAttributes(<span style="color:blue;">new</span> { align = <span style="color:#a31515;">"right"</span> });
    c.Add(a =&gt; a.InvoiceGross).Width(90).Format(<span style="color:#a31515;">"{0:c}"</span>).HtmlAttributes(<span style="color:blue;">new</span> { align = <span style="color:#a31515;">"right"</span> });
    c.Add(a =&gt; a.Id).Hidden();
})</pre>
<p>Happy Coding!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1439/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1439&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/09/29/telerik-mvc-grid-workaround-for-not-rendering-properly-in-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/firefox.jpg" medium="image">
			<media:title type="html">FireFox</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/chrome.jpg" medium="image">
			<media:title type="html">Chrome</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/internet-explorer.jpg" medium="image">
			<media:title type="html">Internet Explorer</media:title>
		</media:content>
	</item>
		<item>
		<title>Large JSON Result for Telerik&#8217;s MVC Grid</title>
		<link>http://anyrest.wordpress.com/2011/09/27/large-json-result-for-teleriks-mvc-grid/</link>
		<comments>http://anyrest.wordpress.com/2011/09/27/large-json-result-for-teleriks-mvc-grid/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 11:00:13 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Telerik]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1434</guid>
		<description><![CDATA[You might have encountered this error &#8220;Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.&#8221; when pushing a large data to Telerik&#8217;s MVC Grid. My best guess is you have tried solutions to update the web.config to contain sections something similar to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1434&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You might have encountered this error &#8220;Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.&#8221; when pushing a large data to Telerik&#8217;s MVC Grid.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/large-json.jpg"><img class="alignnone size-full wp-image-1436" title="Large JSON" src="http://anyrest.files.wordpress.com/2011/09/large-json.jpg?w=630&#038;h=330" alt="" width="630" height="330" /></a></p>
<p>My best guess is you have tried solutions to update the web.config to contain sections something similar to below.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:maroon;">configuration</span><span style="color:blue;">&gt;</span> 
   <span style="color:blue;">&lt;</span><span style="color:maroon;">system.web.extensions</span><span style="color:blue;">&gt;</span>
       <span style="color:blue;">&lt;</span><span style="color:maroon;">scripting</span><span style="color:blue;">&gt;</span>
           <span style="color:blue;">&lt;</span><span style="color:maroon;">webServices</span><span style="color:blue;">&gt;</span>
               <span style="color:blue;">&lt;</span><span style="color:maroon;">jsonSerialization</span> <span style="color:red;">maxJsonLength</span><span style="color:blue;">="2147483644"/&gt;</span>
           <span style="color:blue;">&lt;/</span><span style="color:maroon;">webServices</span><span style="color:blue;">&gt;</span>
       <span style="color:blue;">&lt;/</span><span style="color:maroon;">scripting</span><span style="color:blue;">&gt;</span>
   <span style="color:blue;">&lt;/</span><span style="color:maroon;">system.web.extensions</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">&lt;/</span><span style="color:maroon;">configuration</span><span style="color:blue;">&gt;</span></pre>
<p>Well that wont work because the value of the MaxJsonLength only applies to the internal JavaScriptSerializer that is used by the asynchronous communication layer to invoke Web services methods. The solution is simple we just need to return a Large Json Result for the MVC Grid.</p>
<p>So lets say you Initially have a method called SelectSomethingAjax, you usually return it as ActionResult</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;">[<span style="color:#2b91af;">GridAction</span>]
<span style="color:blue;">public</span> <span style="color:#2b91af;">ActionResult</span> SelectInvoiceAjax()
{
    <span style="color:blue;">var</span> model = GetRecords();

    <span style="color:blue;">return</span> View(<span style="color:blue;">new</span> <span style="color:#2b91af;">GridModel</span>&lt;<span style="color:#2b91af;">YourViewModel</span>&gt;
    {
        Data = model.YourViewModel
    });
}</pre>
<p>Instead of doing that you should implement your own ActionResult that will output a JSON result where you can modify the properties of the JavaScriptSerializer. So with the help of this post (<a href="http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/" target="_blank">http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/</a>) which I found here (<a href="http://www.java2s.com/Open-Source/ASP.NET/AJAX/ajaxmapdataconnector/DataConDemoWebRole/Business/LargeJsonResult.cs.htm" target="_blank">http://www.java2s.com/Open-Source/ASP.NET/AJAX/ajaxmapdataconnector/DataConDemoWebRole/Business/LargeJsonResult.cs.htm</a>) we have an implementation of the LargeJsonResult.<br />
(Below is copied on his blog site)</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Web;
<span style="color:blue;">using</span> System.Web.Mvc;
<span style="color:blue;">using</span> System.Web.Script.Serialization;

<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">LargeJsonResult</span> : <span style="color:#2b91af;">JsonResult</span>
{
    <span style="color:blue;">const</span> <span style="color:blue;">string</span> JsonRequest_GetNotAllowed = <span style="color:#a31515;">"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."</span>;
    <span style="color:blue;">public</span> LargeJsonResult()
    {
        MaxJsonLength = 1024000;
        RecursionLimit = 100;
    }

    <span style="color:blue;">public</span> <span style="color:blue;">int</span> MaxJsonLength { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public</span> <span style="color:blue;">int</span> RecursionLimit { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> ExecuteResult( <span style="color:#2b91af;">ControllerContext</span> context )
    {
        <span style="color:blue;">if</span>( context == <span style="color:blue;">null</span> )
        {
            <span style="color:blue;">throw</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">ArgumentNullException</span>( <span style="color:#a31515;">"context"</span> );
        }
        <span style="color:blue;">if</span>( JsonRequestBehavior == <span style="color:#2b91af;">JsonRequestBehavior</span>.DenyGet &amp;&amp;
            <span style="color:#2b91af;">String</span>.Equals( context.HttpContext.Request.HttpMethod, <span style="color:#a31515;">"GET"</span>, <span style="color:#2b91af;">StringComparison</span>.OrdinalIgnoreCase ) )
        {
            <span style="color:blue;">throw</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">InvalidOperationException</span>( JsonRequest_GetNotAllowed );
        }

        <span style="color:#2b91af;">HttpResponseBase</span> response = context.HttpContext.Response;

        <span style="color:blue;">if</span>( !<span style="color:#2b91af;">String</span>.IsNullOrEmpty( ContentType ) )
        {
            response.ContentType = ContentType;
        }
        <span style="color:blue;">else</span>
        {
            response.ContentType = <span style="color:#a31515;">"application/json"</span>;
        }
        <span style="color:blue;">if</span>( ContentEncoding != <span style="color:blue;">null</span> )
        {
            response.ContentEncoding = ContentEncoding;
        }
        <span style="color:blue;">if</span>( Data != <span style="color:blue;">null</span> )
        {
            <span style="color:#2b91af;">JavaScriptSerializer</span> serializer = <span style="color:blue;">new</span> <span style="color:#2b91af;">JavaScriptSerializer</span>() { MaxJsonLength = MaxJsonLength, RecursionLimit = RecursionLimit };
            response.Write( serializer.Serialize( Data ) );
        }
    }
}</pre>
<p>So here is how we return it now on your SelectSomethingAjax</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">public</span> <span style="color:#2b91af;">LargeJsonResult</span> SelectSomthingAjax()
{
    <span style="color:blue;">var</span> model = GetRecords();

    <span style="color:blue;">return</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">LargeJsonResult</span>
    {
        MaxJsonLength = <span style="color:blue;">int</span>.MaxValue,
        JsonRequestBehavior = System.Web.Mvc.<span style="color:#2b91af;">JsonRequestBehavior</span>.AllowGet,
        Data = <span style="color:blue;">new</span> <span style="color:#2b91af;">GridModel</span>&lt;<span style="color:#2b91af;">YourViewModel</span>&gt;
        {
            Data = model.YourViewModel
        }
    };
}</pre>
<p>If you notice we omitted the [GridAction] attribute as it returns the ActionResult as a JsonResult and since we are already returning it we don&#8217;t need that attribute.   Now with that solution I can output more records without that error.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/grid.jpg"><img class="alignnone size-full wp-image-1435" title="Grid" src="http://anyrest.files.wordpress.com/2011/09/grid.jpg?w=630&#038;h=200" alt="" width="630" height="200" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1434/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1434&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/09/27/large-json-result-for-teleriks-mvc-grid/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/large-json.jpg" medium="image">
			<media:title type="html">Large JSON</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/grid.jpg" medium="image">
			<media:title type="html">Grid</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Implementation of MVC Cascading Ajax Drop Down</title>
		<link>http://anyrest.wordpress.com/2011/09/21/simple-implementation-of-mvc-cascading-ajax-drop-down/</link>
		<comments>http://anyrest.wordpress.com/2011/09/21/simple-implementation-of-mvc-cascading-ajax-drop-down/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 10:00:39 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Telerik]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1419</guid>
		<description><![CDATA[Don&#8217;t you miss developing the traditional way? where you can just use Update Panels and Auto Post backs, it was so easy to develop the UI before where you can see it in the design view and interact with the objects in a rich manner. But that was the past, now we have MVC where [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1419&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t you miss developing the traditional way? where you can just use Update Panels and Auto Post backs, it was so easy to develop the UI before where you can see it in the design view and interact with the objects in a rich manner. But that was the past, now we have MVC where its presents us a different way of developing things the proper way.  If you are starting to use MVC like me then  you might be thinking how do I make cascading drop downs without the Auto Post back and Update Panels then read further as this is how I implemented it in the most easiest way.</p>
<p>Lets start! Lets say you have the following structure below.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/product-catalog.jpg"><img class="alignnone size-full wp-image-1421" title="Product Catalog" src="http://anyrest.files.wordpress.com/2011/09/product-catalog.jpg?w=630&#038;h=195" alt="" width="630" height="195" /></a></p>
<p>You need to have models for each of them</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/models.jpg"><img class="alignnone size-full wp-image-1422" title="Models" src="http://anyrest.files.wordpress.com/2011/09/models.jpg?w=630" alt=""   /></a></p>
<p>Now you need a controller to perform all of the operations, Lets call it CascadingDropDown.cs, we need 3 Actions Results one for the Index which will Load the page and populate the Category DropDown, Select Category to populate Sub Category Drop Down and Select Sub Category to populate Products Drop Down.  If you notice the name might be confusing that is because it is named after an Action Result not action to perform, which means what Action was invoked for this to happen.  Below is a sample code.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Collections.Generic;
<span style="color:blue;">using</span> System.Linq;
<span style="color:blue;">using</span> System.Web;
<span style="color:blue;">using</span> System.Web.Mvc;

<span style="color:blue;">namespace</span> MvcApplication1.Controllers
{
    <span style="color:blue;">using</span> Models;

    <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">CascadingDropDownController</span> : <span style="color:#2b91af;">Controller</span>
    {
        <span style="color:blue;">public</span> <span style="color:#2b91af;">ActionResult</span> Index()
        {
            <span style="color:#2b91af;">ProductCatalog</span> productCatalog = <span style="color:blue;">new</span> <span style="color:#2b91af;">ProductCatalog</span>();
            productCatalog.Categories = <span style="color:#2b91af;">ProductCatalog</span>.GetCategories();

            <span style="color:blue;">return</span> View(productCatalog);
        }

        [<span style="color:#2b91af;">HttpPost</span>]
        <span style="color:blue;">public</span> <span style="color:#2b91af;">ActionResult</span> SelectCategory(<span style="color:blue;">int</span>? selectedCategoryId)
        {
            <span style="color:#2b91af;">ProductCatalog</span> productCatalog = <span style="color:blue;">new</span> <span style="color:#2b91af;">ProductCatalog</span>();
            productCatalog.SubCategories = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">SubCategory</span>&gt;();

            <span style="color:blue;">if</span> (selectedCategoryId.HasValue)
            {
                productCatalog.SubCategories = (<span style="color:blue;">from</span> s <span style="color:blue;">in</span> <span style="color:#2b91af;">ProductCatalog</span>.GetSubCategories()
                                                <span style="color:blue;">where</span> s.CategoryId == selectedCategoryId
                                                <span style="color:blue;">orderby</span> s.Name
                                                <span style="color:blue;">select</span> s).ToList();
            }

            <span style="color:blue;">return</span> PartialView(<span style="color:#a31515;">"SubCategoriesUserControl"</span>, productCatalog);

        }

        [<span style="color:#2b91af;">HttpPost</span>]
        <span style="color:blue;">public</span> <span style="color:#2b91af;">ActionResult</span> SelectSubCategory(<span style="color:blue;">int</span>? selectedSubCategoryId)
        {
            <span style="color:#2b91af;">ProductCatalog</span> productCatalog = <span style="color:blue;">new</span> <span style="color:#2b91af;">ProductCatalog</span>();
            productCatalog.Products = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Product</span>&gt;();

            <span style="color:blue;">if</span> (selectedSubCategoryId.HasValue)
            {
                productCatalog.Products = (<span style="color:blue;">from</span> s <span style="color:blue;">in</span> <span style="color:#2b91af;">ProductCatalog</span>.GetProducts()
                                           <span style="color:blue;">where</span> s.SubCategoryId == selectedSubCategoryId
                                           <span style="color:blue;">orderby</span> s.Name
                                           <span style="color:blue;">select</span> s).ToList();
            }

            <span style="color:blue;">return</span> PartialView(<span style="color:#a31515;">"ProductsUserControl"</span>, productCatalog);

        }
    }
}</pre>
<p>Take note we use partial view on the SelectCategory and SelectSubCategory as we will send a partial view to the response.  While on the Index we need the full rendering to the response.  Now you have your controller we need to create those views.  We need 1 view for Index and 3 partial views for the 3 dropdowns.</p>
<p>Now lets create the Index view, all you have to do is to right-click on the Index method on your controller then you can start coding.  This view will display all of the dropdowns.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/add-view.jpg"><img class="alignnone size-full wp-image-1423" title="Add View" src="http://anyrest.files.wordpress.com/2011/09/add-view.jpg?w=630" alt=""   /></a></p>
<h3><span style="color:#3366ff;">Index.cshtml</span></h3>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@model </span>MvcApplication1.Models.<span style="color:#2b91af;">ProductCatalog</span>
<span style="background:yellow;">@{</span>
    ViewBag.Title = <span style="color:#a31515;">"Index"</span>;
    Layout = <span style="color:#a31515;">"~/Views/Shared/_Layout.cshtml"</span>;
<span style="background:yellow;">}</span>
<span style="color:blue;">&lt;</span><span style="color:maroon;">table</span> <span style="color:red;">cellpadding</span><span style="color:blue;">="0"</span> <span style="color:red;">cellspacing</span><span style="color:blue;">="4"</span> <span style="color:red;">border</span><span style="color:blue;">="0"&gt;</span>
    <span style="color:blue;">&lt;</span><span style="color:maroon;">tr</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>Category<span style="color:red;">&amp;nbsp;</span><span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span><span style="color:red;">&amp;nbsp;</span>:<span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span><span style="background:yellow;">@</span>Html.Partial(<span style="color:#a31515;">"CategoriesUserControl"</span>, Model)<span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
    <span style="color:blue;">&lt;/</span><span style="color:maroon;">tr</span><span style="color:blue;">&gt;</span>
    <span style="color:blue;">&lt;</span><span style="color:maroon;">tr</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>Sub - Category<span style="color:red;">&amp;nbsp;</span><span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span><span style="color:red;">&amp;nbsp;</span>:<span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:maroon;">div</span> <span style="color:red;">id</span><span style="color:blue;">="SubCategories"&gt;</span><span style="background:yellow;">@</span>Html.Partial(<span style="color:#a31515;">"SubCategoriesUserControl"</span>, Model)<span style="color:blue;">&lt;/</span><span style="color:maroon;">div</span><span style="color:blue;">&gt;&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
    <span style="color:blue;">&lt;/</span><span style="color:maroon;">tr</span><span style="color:blue;">&gt;</span>
    <span style="color:blue;">&lt;</span><span style="color:maroon;">tr</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>Products<span style="color:red;">&amp;nbsp;</span><span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span><span style="color:red;">&amp;nbsp;</span>:<span style="color:blue;">&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;&lt;</span><span style="color:maroon;">div</span> <span style="color:red;">id</span><span style="color:blue;">="Products"&gt;</span><span style="background:yellow;">@</span>Html.Partial(<span style="color:#a31515;">"ProductsUserControl"</span>, Model)<span style="color:blue;">&lt;/</span><span style="color:maroon;">div</span><span style="color:blue;">&gt;&lt;/</span><span style="color:maroon;">td</span><span style="color:blue;">&gt;</span>
    <span style="color:blue;">&lt;/</span><span style="color:maroon;">tr</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">&lt;/</span><span style="color:maroon;">table</span><span style="color:blue;">&gt;</span></pre>
<p>Now lets create partial views and for those who does not know how just right-click on the parent folder where your Index view is and Select Add -&gt; View then tick the &#8220;create as a partial view&#8221;.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/add-partial-view.jpg"><img class="alignnone size-full wp-image-1424" title="Add Partial View" src="http://anyrest.files.wordpress.com/2011/09/add-partial-view.jpg?w=630" alt=""   /></a></p>
<p>Now for the codes:</p>
<h3><span style="color:#3366ff;">CategoriesUserControl.cshtml</span></h3>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@model </span>MvcApplication1.Models.<span style="color:#2b91af;">ProductCatalog</span>
<span style="background:yellow;">@</span><span style="color:blue;">using</span> (Ajax.BeginForm(<span style="color:#a31515;">"SelectCategory"</span>, <span style="color:#a31515;">"CascadingDropDown"</span>, <span style="color:blue;">new</span> <span style="color:#2b91af;">AjaxOptions</span> { UpdateTargetId = <span style="color:#a31515;">"SubCategories"</span> }))
{ 
    <span style="background:yellow;">@</span>Html.DropDownListFor(
            m =&gt; m.SelectedCategoryId,
            <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectList</span>(Model.Categories, <span style="color:#a31515;">"Id"</span>, <span style="color:#a31515;">"Name"</span>),
           <span style="color:blue;">string</span>.Empty
        )
}
<span style="color:blue;">&lt;</span><span style="color:maroon;">script</span> <span style="color:red;">type</span><span style="color:blue;">="text/javascript"&gt;</span>
    $(<span style="color:maroon;">'#SelectedCategoryId'</span>).change(<span style="color:blue;">function</span> () {
        $(<span style="color:blue;">this</span>).parents(<span style="color:maroon;">'form'</span>).submit();
    });
<span style="color:blue;">&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span></pre>
<h3><span style="color:#3366ff;">SubCategoriesUserControl.cshtml</span></h3>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@model </span>MvcApplication1.Models.<span style="color:#2b91af;">ProductCatalog</span>
<span style="background:yellow;">@</span><span style="color:blue;">if</span> (Model.SubCategories != <span style="color:blue;">null</span> &amp;&amp; Model.SubCategories.Count() &gt; 0)
{
    <span style="color:blue;">using</span> (Ajax.BeginForm(<span style="color:#a31515;">"SelectSubCategory"</span>, <span style="color:#a31515;">"CascadingDropDown"</span>, <span style="color:blue;">new</span> <span style="color:#2b91af;">AjaxOptions</span> { UpdateTargetId = <span style="color:#a31515;">"Products"</span> }))
    { 
    <span style="background:yellow;">@</span>Html.HiddenFor(m =&gt; m.SelectedCategoryId)
    <span style="background:yellow;">@</span>Html.DropDownListFor(
            m =&gt; m.SelectedSubCategoryId,
            <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectList</span>(Model.SubCategories, <span style="color:#a31515;">"Id"</span>, <span style="color:#a31515;">"Name"</span>),
            <span style="color:blue;">string</span>.Empty
            )
    }
}
<span style="color:blue;">&lt;</span><span style="color:maroon;">script</span> <span style="color:red;">type</span><span style="color:blue;">="text/javascript"&gt;</span>
    $(<span style="color:maroon;">'#SelectedSubCategoryId'</span>).change(<span style="color:blue;">function</span> () {
        $(<span style="color:blue;">this</span>).parents(<span style="color:maroon;">'form'</span>).submit();
    });   
<span style="color:blue;">&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span></pre>
<h3><span style="color:#3366ff;">ProductsUserControl.cshtml</span></h3>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="background:yellow;">@model </span>MvcApplication1.Models.<span style="color:#2b91af;">ProductCatalog</span>
<span style="background:yellow;">@</span><span style="color:blue;">if</span> (Model.Products != <span style="color:blue;">null</span> &amp;&amp; Model.Products.Count() &gt; 0)
{
    <span style="background:yellow;">@</span>Html.DropDownList(
        <span style="color:#a31515;">"Products"</span>,
            <span style="color:blue;">new</span> <span style="color:#2b91af;">SelectList</span>(Model.Products, <span style="color:#a31515;">"Id"</span>, <span style="color:#a31515;">"Name"</span>),
            <span style="color:blue;">string</span>.Empty
            )
}</pre>
<p>If you notice instead of Html.BeginForm we use Ajax.BeginForm using the parameters below.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/parameters.jpg"><img class="alignnone size-full wp-image-1428" title="Parameters" src="http://anyrest.files.wordpress.com/2011/09/parameters.jpg?w=630&#038;h=60" alt="" width="630" height="60" /></a></p>
<ul>
<li><strong>actionName</strong> will be name of your method in your controller</li>
<li><strong>controllerName</strong> will be theame of your controller</li>
<li><strong>AjaxOptions</strong> as the name says AjaxOptions, at this point we only need the UpdateTargetId which we defined as &lt;div&gt; in the index.cshtml file.</li>
</ul>
<p>If you notice there are jQuery scripts below CategoriesUserControl.cshtml and SubCategoriesUserControl.cshtml, they will handle the auto postback on the partially rendered HTML.</p>
<p>Also on your _Layout.cshtml add this on the header if you haven&#8217;t done it yet</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:maroon;">script</span> <span style="color:red;">src</span><span style="color:blue;">="</span><span style="background:yellow;">@</span><span style="color:blue;">Url.Content(</span><span style="color:#a31515;">"~/Scripts/jquery-1.5.1.min.js"</span><span style="color:blue;">)"</span> <span style="color:red;">type</span><span style="color:blue;">="text/javascript"&gt;&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">&lt;</span><span style="color:maroon;">script</span> <span style="color:red;">src</span><span style="color:blue;">="</span><span style="background:yellow;">@</span><span style="color:blue;">Url.Content(</span><span style="color:#a31515;">"~/Scripts/jquery.unobtrusive-ajax.min.js"</span><span style="color:blue;">)"</span> <span style="color:red;">type</span><span style="color:blue;">="text/javascript"&gt;&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span></pre>
<p><span style="color:#ff0000;"><em><strong>Note : If you are using ASP.Net MVC3 then by default it uses unobtrusive jquery so you need to remove all MicrosoftAjax scripts and use only those two above otherwise the onchange event will open a new page instead of partially rendering it.</strong></em></span></p>
<p class="style1"><span style="color:#ff0000;"><strong><em>Note : If you are using</em></strong></span><strong><em> </em></strong><strong><em><a href="http://www.telerik.com/products/aspnet-mvc.aspx" target="_blank">Telerik MVC Controls</a></em></strong><strong><em></em></strong><span style="color:#ff0000;"><strong><em> then you only need the unobtrusive ajax the other script is already referenced by default if you are using Telerik.</em></strong></span></p>
<p>Make sure also on your web.config you have the following lines.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">key</span><span style="color:blue;">=</span>"<span style="color:blue;">ClientValidationEnabled</span>"<span style="color:blue;"> </span><span style="color:red;">value</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">&lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">key</span><span style="color:blue;">=</span>"<span style="color:blue;">UnobtrusiveJavaScriptEnabled</span>"<span style="color:blue;"> </span><span style="color:red;">value</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;">/&gt;</span></pre>
<p>At this point you have done everything you need and your ready to roll.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/index-mozilla-firefox.jpg"><img class="alignnone size-full wp-image-1427" title="Index - Mozilla Firefox" src="http://anyrest.files.wordpress.com/2011/09/index-mozilla-firefox.jpg?w=630" alt=""   /></a></p>
<p>If you need the full source code you can download it here (<a href="http://www.codeproject.com/KB/Blogs/258172/CascadingDropDownMVC.zip" target="_blank">http://www.codeproject.com/KB/Blogs/258172/CascadingDropDownMVC.zip</a>).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1419/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1419/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1419/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1419/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1419/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1419/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1419/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1419/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1419&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/09/21/simple-implementation-of-mvc-cascading-ajax-drop-down/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/product-catalog.jpg" medium="image">
			<media:title type="html">Product Catalog</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/models.jpg" medium="image">
			<media:title type="html">Models</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/add-view.jpg" medium="image">
			<media:title type="html">Add View</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/add-partial-view.jpg" medium="image">
			<media:title type="html">Add Partial View</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/parameters.jpg" medium="image">
			<media:title type="html">Parameters</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/index-mozilla-firefox.jpg" medium="image">
			<media:title type="html">Index - Mozilla Firefox</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple TSQL and C# Entity Generator for S#arp Architecture</title>
		<link>http://anyrest.wordpress.com/2011/09/16/simple-tsql-and-c-entity-generator-for-sarp-architecture/</link>
		<comments>http://anyrest.wordpress.com/2011/09/16/simple-tsql-and-c-entity-generator-for-sarp-architecture/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 10:00:36 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entities]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[S#arp Architecture]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1410</guid>
		<description><![CDATA[Recently we started a big project which uses S#arp Architecture and for those who does not know what it is you can go to the site (http://www.sharparchitecture.net/) and have a read, but in a gist it is an open source architectural foundation that uses ASP.NET MVC framework with NHibernate. The following principles of this architecture [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1410&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently we started a big project which uses S#arp Architecture and for those who does not know what it is you can go to the site (<a href="http://www.sharparchitecture.net/" target="_blank">http://www.sharparchitecture.net/</a>) and have a read, but in a gist it is an open source architectural foundation that uses ASP.NET MVC framework with NHibernate.</p>
<p style="text-align:center;"><a href="http://www.sharparchitecture.net/"><img class="size-full wp-image-1412 aligncenter" src="http://anyrest.files.wordpress.com/2011/09/sarp-architecture-mozilla-firefox_2011-09-14_09-59-49.jpg?w=630" alt=""   /></a></p>
<p>The following principles of this architecture are Domain Driven Design Focus, Loosely coupled, Preconfigured Infrastructure and Open Ended Presentation that means having those principles in mind the developer can concentrate more on Domain and User Experince part of the application that is being developed.</p>
<p>Having said that we used this open-source architecture to leverage on its principles and now we are on the first phase of the project where we are building the Domain layer where we define all the entities needed.  If you have a big database this will be a tedious task mapping tables and columns to a classes so I created a TSQL script to generate those entities so that I&#8217;ll just copy and paste the output to a cs file and were ready to go.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">DECLARE</span> <span style="color:teal;">@TableName</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@ProjectName</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span> <span style="color:gray;">=</span> <span style="color:red;">'&lt;&lt;&lt;YourApplicationName&gt;&gt;&gt;.Domain'</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@DataType</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@ColumnName</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@IsNullable</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@AppDataType</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span>
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@ConstraintType</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span> 
<span style="color:blue;">DECLARE</span> <span style="color:teal;">@ReferringTable</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span>200<span style="color:gray;">)</span> 

<span style="color:blue;">DECLARE</span> <span style="color:teal;">ClassGenerator</span> <span style="color:blue;">CURSOR</span>

<span style="color:green;">--Get all Tables</span>
<span style="color:blue;">FOR</span> <span style="color:blue;">SELECT</span> <span style="color:teal;">TABLE_NAME</span> <span style="color:blue;">from</span> <span style="color:gray;">&lt;&lt;&lt;</span><span style="color:teal;">YourDatabaseName</span><span style="color:gray;">&gt;&gt;&gt;.</span><span style="color:lime;">INFORMATION_SCHEMA</span><span style="color:gray;">.</span><span style="color:lime;">TABLES</span> <span style="color:blue;">where</span> <span style="color:teal;">TABLE_TYPE</span> <span style="color:gray;">=</span> <span style="color:red;">'BASE TABLE'</span> <span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> <span style="color:teal;">TABLE_NAME</span>
<span style="color:blue;">OPEN</span> <span style="color:teal;">ClassGenerator</span>
<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">ClassGenerator</span>
<span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>

<span style="color:blue;">PRINT</span> <span style="color:red;">'namespace '</span> <span style="color:gray;">+</span> <span style="color:teal;">@ProjectName</span>
<span style="color:blue;">PRINT</span> <span style="color:red;">'{'</span>
<span style="color:blue;">PRINT</span> <span style="color:red;">' using System;'</span>
<span style="color:blue;">PRINT</span> <span style="color:red;">' using SharpArch.Domain.DomainModel;'</span>

<span style="color:blue;">WHILE</span> <span style="color:magenta;">@@FETCH_STATUS</span> <span style="color:gray;">=</span> 0
<span style="color:blue;">BEGIN</span> 

		<span style="color:blue;">PRINT</span> <span style="color:red;">' public class '</span> <span style="color:gray;">+</span> <span style="color:teal;">dbo</span><span style="color:gray;">.</span><span style="color:teal;">Singularize</span><span style="color:gray;">(</span><span style="color:teal;">@TableName</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:red;">' : Entity'</span>
		<span style="color:blue;">PRINT</span> <span style="color:red;">' {'</span>

		<span style="color:blue;">DECLARE</span> <span style="color:teal;">EntityGenerator</span> <span style="color:blue;">CURSOR</span>
		<span style="color:blue;">FOR</span> 

		<span style="color:green;">--Get all columns and their key types from all the tables</span>
		<span style="color:blue;">SELECT</span> 
		<span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">COLUMN_NAME</span><span style="color:gray;">,</span> 
		<span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">IS_NULLABLE</span><span style="color:gray;">,</span> 
		<span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">DATA_TYPE</span><span style="color:gray;">,</span> 
		<span style="color:teal;">CST</span><span style="color:gray;">.</span><span style="color:teal;">CONSTRAINT_TYPE</span><span style="color:gray;">,</span>
		<span style="color:teal;">KCU</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span> <span style="color:blue;">AS</span> <span style="color:teal;">REFERENTIAL_TABLE_SOURCE</span>
		<span style="color:blue;">FROM</span> 
		<span style="color:lime;">INFORMATION_SCHEMA</span><span style="color:gray;">.</span><span style="color:lime;">KEY_COLUMN_USAGE</span> <span style="color:teal;">KCU</span>
		<span style="color:gray;">INNER</span> <span style="color:gray;">JOIN</span> 
		<span style="color:lime;">INFORMATION_SCHEMA</span><span style="color:gray;">.</span><span style="color:lime;">REFERENTIAL_CONSTRAINTS</span> <span style="color:teal;">RCN</span>
		<span style="color:blue;">ON</span> <span style="color:teal;">KCU</span><span style="color:gray;">.</span><span style="color:teal;">CONSTRAINT_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">RCN</span><span style="color:gray;">.</span><span style="color:teal;">UNIQUE_CONSTRAINT_NAME</span> 
		<span style="color:gray;">RIGHT</span> <span style="color:gray;">OUTER</span> <span style="color:gray;">JOIN</span> 
		<span style="color:lime;">INFORMATION_SCHEMA</span><span style="color:gray;">.</span><span style="color:lime;">KEY_COLUMN_USAGE</span> <span style="color:teal;">KCU2</span>
		<span style="color:gray;">INNER</span> <span style="color:gray;">JOIN</span> 
		<span style="color:lime;">INFORMATION_SCHEMA</span><span style="color:gray;">.</span><span style="color:lime;">TABLE_CONSTRAINTS</span> <span style="color:teal;">CST</span>
		<span style="color:blue;">ON</span> <span style="color:teal;">KCU2</span><span style="color:gray;">.</span><span style="color:teal;">CONSTRAINT_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">CST</span><span style="color:gray;">.</span><span style="color:teal;">CONSTRAINT_NAME</span> 
		<span style="color:gray;">AND</span> <span style="color:teal;">KCU2</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">CST</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span> 
		<span style="color:blue;">ON</span> <span style="color:teal;">RCN</span><span style="color:gray;">.</span><span style="color:teal;">CONSTRAINT_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">KCU2</span><span style="color:gray;">.</span><span style="color:teal;">CONSTRAINT_NAME</span> 
		<span style="color:gray;">RIGHT</span> <span style="color:gray;">OUTER</span> <span style="color:gray;">JOIN</span> 
		<span style="color:lime;">INFORMATION_SCHEMA</span><span style="color:gray;">.</span><span style="color:lime;">COLUMNS</span> <span style="color:teal;">COL</span>
		<span style="color:blue;">ON</span> <span style="color:teal;">KCU2</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span> 
		<span style="color:gray;">AND</span> <span style="color:teal;">KCU2</span><span style="color:gray;">.</span><span style="color:teal;">COLUMN_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">COLUMN_NAME</span>
		<span style="color:blue;">WHERE</span> 
		<span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span> <span style="color:gray;">=</span> <span style="color:teal;">@TableName</span>
		<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> 
		<span style="color:teal;">COL</span><span style="color:gray;">.</span><span style="color:teal;">TABLE_NAME</span>

		<span style="color:blue;">OPEN</span> <span style="color:teal;">EntityGenerator</span>
		<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">EntityGenerator</span>
		<span style="color:blue;">INTO</span> <span style="color:teal;">@ColumnName</span><span style="color:gray;">,</span> <span style="color:teal;">@IsNullable</span><span style="color:gray;">,</span> <span style="color:teal;">@DataType</span><span style="color:gray;">,</span> <span style="color:teal;">@ConstraintType</span><span style="color:gray;">,</span> <span style="color:teal;">@ReferringTable</span>

		<span style="color:blue;">WHILE</span> <span style="color:magenta;">@@FETCH_STATUS</span> <span style="color:gray;">=</span> 0
		<span style="color:blue;">BEGIN</span> 

			<span style="color:blue;">IF</span> <span style="color:teal;">@ConstraintType</span> <span style="color:gray;">&lt;&gt;</span> <span style="color:red;">'PRIMARY KEY'</span> <span style="color:gray;">OR</span> <span style="color:teal;">@ConstraintType</span> <span style="color:gray;">IS</span> <span style="color:gray;">NULL</span>
			<span style="color:blue;">BEGIN</span>
				<span style="color:blue;">IF</span> <span style="color:teal;">@ConstraintType</span> <span style="color:gray;">=</span> <span style="color:red;">'FOREIGN KEY'</span>
				<span style="color:blue;">BEGIN</span>
					<span style="color:blue;">PRINT</span> <span style="color:red;">' public virtual '</span> <span style="color:gray;">+</span> <span style="color:teal;">dbo</span><span style="color:gray;">.</span><span style="color:teal;">Singularize</span><span style="color:gray;">(</span><span style="color:teal;">@ReferringTable</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:red;">' '</span>  <span style="color:gray;">+</span> <span style="color:teal;">dbo</span><span style="color:gray;">.</span><span style="color:teal;">Singularize</span><span style="color:gray;">(</span><span style="color:teal;">@ReferringTable</span><span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:red;">' { get; set; }'</span> 
				<span style="color:blue;">END</span>
				<span style="color:blue;">ELSE</span>
				<span style="color:blue;">BEGIN</span>

					<span style="color:green;">--SQL to .NET Data Type Mapping</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'bigint'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Int64'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'binary'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Byte[]'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'bit'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Boolean'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'char'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'date'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'DateTime'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'datetime'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'DateTime'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'datetimeoffset'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'DateTimeOffset'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'decimal'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'decimal'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'float'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Double'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'image'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Byte[]'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'int'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Int32'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'money'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Decimal'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'nchar'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'ntext'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'numeric'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Decimal'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'nvarchar'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'real'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Single'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'rowversion'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Byte[]'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'smalldatetime'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'DateTime'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'smallint'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Int16'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'smallmoney'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Decimal'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'sql_variant'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Object'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'text'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'time'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'TimeSpan'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'timestamp'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Byte[]'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'tinyint'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Byte'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'uniqueidentifier'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Guid'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'varbinary'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Byte[]'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'varchar'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'String'</span> <span style="color:blue;">END</span>
					<span style="color:blue;">IF</span> <span style="color:teal;">@DataType</span> <span style="color:gray;">=</span> <span style="color:red;">'xml'</span> <span style="color:blue;">BEGIN</span> <span style="color:blue;">SET</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">=</span> <span style="color:red;">'Xml'</span> <span style="color:blue;">END</span>

					<span style="color:blue;">IF</span> <span style="color:teal;">@IsNullable</span> <span style="color:gray;">=</span> <span style="color:red;">'YES'</span> <span style="color:gray;">AND</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">&lt;&gt;</span> <span style="color:red;">'Byte[]'</span> <span style="color:gray;">AND</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">&lt;&gt;</span> <span style="color:red;">'String'</span>
					<span style="color:blue;">BEGIN</span> 
						<span style="color:blue;">PRINT</span> <span style="color:red;">' public virtual '</span> <span style="color:gray;">+</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">+</span> <span style="color:red;">'? '</span> <span style="color:gray;">+</span> <span style="color:teal;">@ColumnName</span> <span style="color:gray;">+</span> <span style="color:red;">' { get; set; }'</span>
					<span style="color:blue;">END</span>
					<span style="color:blue;">ELSE</span>
					<span style="color:blue;">BEGIN</span>
						<span style="color:blue;">PRINT</span> <span style="color:red;">' public virtual '</span> <span style="color:gray;">+</span> <span style="color:teal;">@AppDataType</span> <span style="color:gray;">+</span> <span style="color:red;">' '</span> <span style="color:gray;">+</span> <span style="color:teal;">@ColumnName</span> <span style="color:gray;">+</span> <span style="color:red;">' { get; set; }'</span>
					<span style="color:blue;">END</span>
				<span style="color:blue;">END</span>
			<span style="color:blue;">END</span>
			<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">EntityGenerator</span>
			<span style="color:blue;">INTO</span> <span style="color:teal;">@ColumnName</span><span style="color:gray;">,</span> <span style="color:teal;">@IsNullable</span><span style="color:gray;">,</span> <span style="color:teal;">@DataType</span><span style="color:gray;">,</span> <span style="color:teal;">@ConstraintType</span><span style="color:gray;">,</span> <span style="color:teal;">@ReferringTable</span>

		<span style="color:blue;">END</span>
		<span style="color:blue;">CLOSE</span> <span style="color:teal;">EntityGenerator</span>
		<span style="color:blue;">DEALLOCATE</span> <span style="color:teal;">EntityGenerator</span>

	<span style="color:blue;">PRINT</span> <span style="color:red;">' }'</span>

	<span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> <span style="color:teal;">ClassGenerator</span>

<span style="color:blue;">INTO</span> <span style="color:teal;">@TableName</span>
<span style="color:blue;">END</span>
<span style="color:blue;">PRINT</span> <span style="color:red;">'}'</span>
<span style="color:blue;">CLOSE</span> <span style="color:teal;">ClassGenerator</span>
<span style="color:blue;">DEALLOCATE</span> <span style="color:teal;">ClassGenerator</span></pre>
<p>Now I noticed wouldn&#8217;t it be cleaner to create each entity in different class files? So I created a console application to do that which separates each entity to a file.  And here is the code:</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Collections.Generic;
<span style="color:blue;">using</span> System.Linq;
<span style="color:blue;">using</span> System.Text;
<span style="color:blue;">using</span> System.Data;
<span style="color:blue;">using</span> System.Data.SqlClient;
<span style="color:blue;">using</span> System.IO;

<span style="color:blue;">class</span> <span style="color:#2b91af;">Program</span>
{
    <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)
    {
        <span style="color:blue;">string</span> sConnString = <span style="color:#a31515;">"Data Source=&lt;&lt;&lt;YourDatabaseServer&gt;&gt;&gt;;Initial Catalog=&lt;&lt;&lt;YourDatabase&gt;&gt;;Integrated Security=SSPI"</span>;
        <span style="color:#2b91af;">SqlConnection</span> oSQLConn1 = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlConnection</span>(sConnString);
        <span style="color:#2b91af;">SqlDataReader</span> oReader = <span style="color:blue;">null</span>;
        <span style="color:blue;">string</span> sProjectName = <span style="color:#a31515;">"&lt;&lt;&lt;YourProjectName&gt;&gt;&gt;.Domain"</span>;
        <span style="color:blue;">string</span> sGeneratedCodeLocation = <span style="color:#a31515;">@"C:\&lt;&lt;&lt;YourProjectFolder&gt;&gt;&gt;"</span>;

        oSQLConn1.Open();

        <span style="color:#2b91af;">SqlCommand</span> oCommand = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlCommand</span>(<span style="color:#a31515;">"SELECT TABLE_NAME, dbo.Singularize(TABLE_NAME) as SINGULARIZED_TABLE from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME"</span>, oSQLConn1);

        oReader = oCommand.ExecuteReader();

        <span style="color:blue;">while</span> (oReader.Read())
        {
            <span style="color:blue;">string</span> sTableName = oReader[0].ToString();
            <span style="color:blue;">string</span> sSingularizedTableName = oReader[1].ToString();
            <span style="color:#2b91af;">SqlConnection</span> oSQLConn2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlConnection</span>(sConnString);
            <span style="color:#2b91af;">SqlDataReader</span> oItemReader = <span style="color:blue;">null</span>;

            oSQLConn2.Open();

            <span style="color:#2b91af;">SqlCommand</span> oItemCommand =
            <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlCommand</span>(<span style="color:#a31515;">@"SELECT </span>
<span style="color:#a31515;">     COL.COLUMN_NAME, </span>
<span style="color:#a31515;">     COL.IS_NULLABLE, </span>
<span style="color:#a31515;">     COL.DATA_TYPE, </span>
<span style="color:#a31515;">     CST.CONSTRAINT_TYPE,</span>
<span style="color:#a31515;">     dbo.Singularize(KCU.TABLE_NAME) AS REFERENTIAL_TABLE_SOURCE</span>
<span style="color:#a31515;">     FROM </span>
<span style="color:#a31515;">     INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU</span>
<span style="color:#a31515;">     INNER JOIN </span>
<span style="color:#a31515;">     INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RCN</span>
<span style="color:#a31515;">     ON KCU.CONSTRAINT_NAME = RCN.UNIQUE_CONSTRAINT_NAME </span>
<span style="color:#a31515;">     RIGHT OUTER JOIN </span>
<span style="color:#a31515;">     INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2</span>
<span style="color:#a31515;">     INNER JOIN </span>
<span style="color:#a31515;">     INFORMATION_SCHEMA.TABLE_CONSTRAINTS CST</span>
<span style="color:#a31515;">     ON KCU2.CONSTRAINT_NAME = CST.CONSTRAINT_NAME </span>
<span style="color:#a31515;">     AND KCU2.TABLE_NAME = CST.TABLE_NAME </span>
<span style="color:#a31515;">     ON RCN.CONSTRAINT_NAME = KCU2.CONSTRAINT_NAME </span>
<span style="color:#a31515;">     RIGHT OUTER JOIN </span>
<span style="color:#a31515;">     INFORMATION_SCHEMA.COLUMNS COL</span>
<span style="color:#a31515;">     ON KCU2.TABLE_NAME = COL.TABLE_NAME </span>
<span style="color:#a31515;">     AND KCU2.COLUMN_NAME = COL.COLUMN_NAME</span>
<span style="color:#a31515;">     WHERE </span>
<span style="color:#a31515;">     COL.TABLE_NAME = '"</span> + sTableName +
            <span style="color:#a31515;">@"' ORDER BY </span>
<span style="color:#a31515;">     COL.TABLE_NAME"</span>, oSQLConn2);

            oItemReader = oItemCommand.ExecuteReader();
            <span style="color:#2b91af;">TextWriter</span> oCSFile = <span style="color:blue;">new</span> <span style="color:#2b91af;">StreamWriter</span>(sGeneratedCodeLocation + sSingularizedTableName + <span style="color:#a31515;">".cs"</span>);

            oCSFile.WriteLine(<span style="color:#a31515;">"namespace "</span> + sProjectName);
            oCSFile.WriteLine(<span style="color:#a31515;">"{"</span>);
            oCSFile.WriteLine(<span style="color:#a31515;">"\tusing System;"</span>);
            oCSFile.WriteLine(<span style="color:#a31515;">"\tusing SharpArch.Domain.DomainModel;"</span>);

            oCSFile.WriteLine(<span style="color:#a31515;">"\tpublic class "</span> + sSingularizedTableName + <span style="color:#a31515;">" : Entity"</span>);
            oCSFile.WriteLine(<span style="color:#a31515;">"\t{"</span>);

            <span style="color:blue;">while</span> (oItemReader.Read())
            {
                <span style="color:blue;">string</span> sAppDataType = <span style="color:#a31515;">"String"</span>;

                <span style="color:blue;">string</span> sColumnName = oItemReader[0].ToString();
                <span style="color:blue;">string</span> sIsNullable = oItemReader[1].ToString();
                <span style="color:blue;">string</span> sDataType = oItemReader[2].ToString();
                <span style="color:blue;">string</span> sConstraintType = oItemReader[3].ToString();
                <span style="color:blue;">string</span> sReferringTable = oItemReader[4].ToString();

                <span style="color:blue;">if</span> (sConstraintType != <span style="color:#a31515;">"PRIMARY KEY"</span> || sConstraintType == <span style="color:blue;">null</span>)
                {
                    <span style="color:blue;">if</span> (sConstraintType == <span style="color:#a31515;">"FOREIGN KEY"</span>)
                    {
                        oCSFile.WriteLine(<span style="color:#a31515;">"\t\tpublic virtual "</span> + sReferringTable + <span style="color:#a31515;">" "</span> + sReferringTable + <span style="color:#a31515;">" { get; set; }"</span>);
                    }
                    <span style="color:blue;">else</span>
                    {
                        <span style="color:green;">//SQL to .NET Data Type Mapping</span>
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"bigint"</span>) sAppDataType = <span style="color:#a31515;">"Int64"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"binary"</span>) sAppDataType = <span style="color:#a31515;">"Byte[]"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"bit"</span>) sAppDataType = <span style="color:#a31515;">"Boolean"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"char"</span>) sAppDataType = <span style="color:#a31515;">"String"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"date"</span>) sAppDataType = <span style="color:#a31515;">"DateTime"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"datetime"</span>) sAppDataType = <span style="color:#a31515;">"DateTime"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"datetimeoffset"</span>) sAppDataType = <span style="color:#a31515;">"DateTimeOffset"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"decimal"</span>) sAppDataType = <span style="color:#a31515;">"decimal"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"float"</span>) sAppDataType = <span style="color:#a31515;">"Double"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"image"</span>) sAppDataType = <span style="color:#a31515;">"Byte[]"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"int"</span>) sAppDataType = <span style="color:#a31515;">"Int32"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"money"</span>) sAppDataType = <span style="color:#a31515;">"Decimal"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"nchar"</span>) sAppDataType = <span style="color:#a31515;">"String"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"ntext"</span>) sAppDataType = <span style="color:#a31515;">"String"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"numeric"</span>) sAppDataType = <span style="color:#a31515;">"Decimal"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"nvarchar"</span>) sAppDataType = <span style="color:#a31515;">"String"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"real"</span>) sAppDataType = <span style="color:#a31515;">"Single"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"rowversion"</span>) sAppDataType = <span style="color:#a31515;">"Byte[]"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"smalldatetime"</span>) sAppDataType = <span style="color:#a31515;">"DateTime"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"smallint"</span>) sAppDataType = <span style="color:#a31515;">"Int16"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"smallmoney"</span>) sAppDataType = <span style="color:#a31515;">"Decimal"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"sql_variant"</span>) sAppDataType = <span style="color:#a31515;">"Object"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"text"</span>) sAppDataType = <span style="color:#a31515;">"String"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"time"</span>) sAppDataType = <span style="color:#a31515;">"TimeSpan"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"timestamp"</span>) sAppDataType = <span style="color:#a31515;">"Byte[]"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"tinyint"</span>) sAppDataType = <span style="color:#a31515;">"Byte"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"uniqueidentifier"</span>) sAppDataType = <span style="color:#a31515;">"Guid"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"varbinary"</span>) sAppDataType = <span style="color:#a31515;">"Byte[]"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"varchar"</span>) sAppDataType = <span style="color:#a31515;">"String"</span>;
                        <span style="color:blue;">if</span> (sDataType == <span style="color:#a31515;">"xml"</span>) sAppDataType = <span style="color:#a31515;">"Xml"</span>;

                        <span style="color:blue;">if</span> (sIsNullable == <span style="color:#a31515;">"YES"</span> &amp;&amp; sAppDataType != <span style="color:#a31515;">"Byte[]"</span> &amp;&amp; sAppDataType != <span style="color:#a31515;">"String"</span>)
                        {
                            oCSFile.WriteLine(<span style="color:#a31515;">"\t\tpublic virtual "</span> + sAppDataType + <span style="color:#a31515;">"? "</span> + sColumnName + <span style="color:#a31515;">" { get; set; }"</span>);
                        }
                        <span style="color:blue;">else</span>
                        {
                            oCSFile.WriteLine(<span style="color:#a31515;">"\t\tpublic virtual "</span> + sAppDataType + <span style="color:#a31515;">" "</span> + sColumnName + <span style="color:#a31515;">" { get; set; }"</span>);
                        }
                    }

                }
            }
            oCSFile.WriteLine(<span style="color:#a31515;">"\t}"</span>);
            <span style="color:blue;">if</span> (oItemReader != <span style="color:blue;">null</span>) oItemReader.Close();
            <span style="color:blue;">if</span> (oSQLConn2 != <span style="color:blue;">null</span>) oSQLConn2.Close();

            oCSFile.WriteLine(<span style="color:#a31515;">"}"</span>);
            oCSFile.Close();
        }
        <span style="color:#2b91af;">Console</span>.ReadLine();

        <span style="color:blue;">if</span> (oReader != <span style="color:blue;">null</span>) oReader.Close();
        <span style="color:blue;">if</span> (oSQLConn1 != <span style="color:blue;">null</span>) oSQLConn1.Close();
    }
}</pre>
<p>Now lets dissect the codes, if you noticed I used a Singularize Function to Singularize Table Names I have posted a code for that which you can find here<a href="http://anyrest.wordpress.com/2011/09/13/singularize-function-in-tsql/" target="_blank"></p>
<p>http://anyrest.wordpress.com/2011/09/13/singularize-function-in-tsql/</a>.</p>
<p>Now let&#8217;s go to the queries, you will notice that there is a complex join in how we get our data regarding our data structure which can be easily extracted from the Information Schema, if you are interested on what other items can be extracted from it have a read here<a href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" target="_blank"></p>
<p>http://msdn.microsoft.com/en-us/library/ms186778.aspx</a></p>
<p>Once we have the data we need, we start to process it and it&#8217;s not that straightforward but its easy all you need to do is to gather information whether a column or field is nullable so that we can declare it as nullabe in the codes.  We also need a mapping to SQL DataTypes to .Net Data Types hence the big if conditions.</p>
<p>Also since we are using the S#arp Architecture there is a convention for PrimaryKeys so we don&#8217;t need to declare it as it is handled by the PrimaryKeyConvention class under NHibernateMaps\Convention, so if you named your PK right prefixing them with Id then this will be easy but you can always override it if you wanted to.  Below is the code that handles it</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/microsoft-visual-studio.jpg"><img class="alignnone size-full wp-image-1411" src="http://anyrest.files.wordpress.com/2011/09/microsoft-visual-studio.jpg?w=630" alt=""   /></a></p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">namespace</span> CI5.Infrastructure.NHibernateMaps.Conventions
{
<span style="color:blue;">    #region</span> Using Directives

    <span style="color:blue;">using</span> FluentNHibernate.Conventions;

<span style="color:blue;">    #endregion</span>

    <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">PrimaryKeyConvention</span> : <span style="color:#2b91af;">IIdConvention</span>
    {
        <span style="color:blue;">public</span> <span style="color:blue;">void</span> Apply(FluentNHibernate.Conventions.Instances.<span style="color:#2b91af;">IIdentityInstance</span> instance)
        {
            instance.Column(instance.EntityType.Name + <span style="color:#a31515;">"Id"</span>);
        }
    }
}</pre>
<p>Finally we need to take note of Foreign Keys and what table is it related to as we don&#8217;t declare Id&#8217;s in the Entities but the class it is related to, hence we have this line.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">if</span> (sConstraintType == <span style="color:#a31515;">"FOREIGN KEY"</span>)
{
    oCSFile.WriteLine(<span style="color:#a31515;">"\t\tpublic virtual "</span> + sReferringTable + <span style="color:#a31515;">" "</span> + sReferringTable + <span style="color:#a31515;">" { get; set; }"</span>);
}</pre>
<p>Other than that everything should be straightforward.</p>
<p>The above code is made to generate only basic stuff so if you have added validation and other lines in your entities do not use it, otherwise I hope this would make someones life easier.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1410/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1410/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1410/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1410&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/09/16/simple-tsql-and-c-entity-generator-for-sarp-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/sarp-architecture-mozilla-firefox_2011-09-14_09-59-49.jpg" medium="image" />

		<media:content url="http://anyrest.files.wordpress.com/2011/09/microsoft-visual-studio.jpg" medium="image" />
	</item>
		<item>
		<title>Singularize Function in TSQL</title>
		<link>http://anyrest.wordpress.com/2011/09/13/singularize-function-in-tsql/</link>
		<comments>http://anyrest.wordpress.com/2011/09/13/singularize-function-in-tsql/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 10:00:32 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1407</guid>
		<description><![CDATA[Today I was trying to make a code generator for one of our projects to make our lives easier, basically I was trying to list all objects on our SQL Server database and Print a C# code output which I can then save as a .cs file, all went fine until I realized that Entities [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1407&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I was trying to make a code generator for one of our projects to make our lives easier, basically I was trying to list all objects on our SQL Server database and Print a C# code output which I can then save as a .cs file, all went fine until I realized that Entities must be singularized, also we have a standard naming convention in our database where every table is pluralized.  So I started searching online whether there is a free TSQL code to Singularize strings and got no results so I resorted to making one of my own.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/microsoft-sql-server-management-studio_2011-09-13_16-33-26.jpg"><img class="alignnone size-full wp-image-1408" title="Microsoft SQL Server Management Studio_2011-09-13_16-33-26" src="http://anyrest.files.wordpress.com/2011/09/microsoft-sql-server-management-studio_2011-09-13_16-33-26.jpg?w=630" alt=""   /></a></p>
<p>To my surprise it was not that hard (unless I missed something) specially with the ever trusted Wikipedia I searched for what are the <a href="http://en.wikipedia.org/wiki/English_plural" target="_blank">different plural formats out there</a> and that is where I based my codes on.  So if you need them please feel free to use the code below and even improve it if you wanted to just share back any improvements you have made.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">CREATE</span> <span style="color:blue;">FUNCTION</span> <span style="color:teal;">Singularize</span>
<span style="color:gray;">(</span>
	<span style="color:teal;">@FieldName</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span><span style="color:magenta;">max</span><span style="color:gray;">)</span>
<span style="color:gray;">)</span>
<span style="color:blue;">RETURNS</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span><span style="color:magenta;">max</span><span style="color:gray;">)</span>
<span style="color:blue;">AS</span>
<span style="color:blue;">BEGIN</span>
	<span style="color:blue;">DECLARE</span> <span style="color:teal;">@Output</span> <span style="color:blue;">varchar</span><span style="color:gray;">(</span><span style="color:magenta;">max</span><span style="color:gray;">)</span>

	<span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">NOT</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%s'</span>
	<span style="color:green;">-- already singular</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:teal;">@FieldName</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%ss'</span>
	<span style="color:green;">-- already singular ie. mass, chess</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:teal;">@FieldName</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%ies'</span> 
	<span style="color:green;">-- ie. cherries, ladies</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> 1<span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)-</span>3<span style="color:gray;">)</span> <span style="color:gray;">+</span> <span style="color:red;">'y'</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%oes'</span> 
	<span style="color:green;">-- ie. heroes, potatoes</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> 1<span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span>2<span style="color:gray;">)</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%es'</span> <span style="color:gray;">and</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)-</span>2<span style="color:gray;">,</span> 1<span style="color:gray;">)</span> <span style="color:gray;">in</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">'a'</span><span style="color:gray;">,</span> <span style="color:red;">'e'</span><span style="color:gray;">,</span> <span style="color:red;">'i'</span><span style="color:gray;">,</span> <span style="color:red;">'o'</span><span style="color:gray;">,</span> <span style="color:red;">'u'</span><span style="color:gray;">)</span>
	<span style="color:green;">-- ie. massages, phases</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> 1<span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span>1<span style="color:gray;">)</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%es'</span> <span style="color:gray;">and</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span>2<span style="color:gray;">,</span> 1<span style="color:gray;">)</span> <span style="color:gray;">in</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">'h'</span><span style="color:gray;">)</span>
	<span style="color:green;">-- ie. witches, dishes</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> 1<span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span> 2<span style="color:gray;">)</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%es'</span> <span style="color:gray;">and</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span>2<span style="color:gray;">,</span> 1<span style="color:gray;">)</span> <span style="color:gray;">in</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:red;">'b'</span><span style="color:gray;">,</span><span style="color:red;">'c'</span><span style="color:gray;">,</span><span style="color:red;">'d'</span><span style="color:gray;">,</span><span style="color:red;">'f'</span><span style="color:gray;">,</span><span style="color:red;">'g'</span><span style="color:gray;">,</span><span style="color:red;">'j'</span><span style="color:gray;">,</span><span style="color:red;">'k'</span><span style="color:gray;">,</span><span style="color:red;">'l'</span><span style="color:gray;">,</span><span style="color:red;">'m'</span><span style="color:gray;">,</span><span style="color:red;">'n'</span><span style="color:gray;">,</span><span style="color:red;">'p'</span><span style="color:gray;">,</span><span style="color:red;">'q'</span><span style="color:gray;">,</span><span style="color:red;">'r'</span><span style="color:gray;">,</span><span style="color:red;">'s'</span><span style="color:gray;">,</span><span style="color:red;">'t'</span><span style="color:gray;">,</span><span style="color:red;">'v'</span><span style="color:gray;">,</span><span style="color:red;">'w'</span><span style="color:gray;">,</span><span style="color:red;">'x'</span><span style="color:gray;">,</span><span style="color:red;">'y'</span><span style="color:gray;">,</span><span style="color:red;">'z'</span><span style="color:gray;">)</span>
	<span style="color:green;">-- ie. kisses, judges</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> 1<span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span> 1<span style="color:gray;">)</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">ELSE</span> <span style="color:blue;">IF</span> <span style="color:teal;">@FieldName</span> <span style="color:gray;">LIKE</span> <span style="color:red;">'%s'</span>
	<span style="color:green;">-- ie. laps, clocks, boys</span>
	<span style="color:blue;">BEGIN</span>
		<span style="color:blue;">SET</span> <span style="color:teal;">@Output</span> <span style="color:gray;">=</span> <span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">,</span> 1<span style="color:gray;">,</span> <span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:teal;">@FieldName</span><span style="color:gray;">)</span> <span style="color:gray;">-</span>1<span style="color:gray;">)</span>
	<span style="color:blue;">END</span>

	<span style="color:blue;">RETURN</span> <span style="color:teal;">@Output</span>
<span style="color:blue;">END</span>
<span style="color:blue;">GO </span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1407/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1407/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1407/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1407&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/09/13/singularize-function-in-tsql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/microsoft-sql-server-management-studio_2011-09-13_16-33-26.jpg" medium="image">
			<media:title type="html">Microsoft SQL Server Management Studio_2011-09-13_16-33-26</media:title>
		</media:content>
	</item>
		<item>
		<title>Using jQuery with Sharepoint 2010</title>
		<link>http://anyrest.wordpress.com/2011/09/06/using-jquery-with-sharepoint-2010/</link>
		<comments>http://anyrest.wordpress.com/2011/09/06/using-jquery-with-sharepoint-2010/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 09:00:26 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1396</guid>
		<description><![CDATA[Several days back I was asked a question in this blog relating to using JavaScript in a Sharepoint, it ended up with multiple questions and one was using jQuery in Sharepoint, as usual I searched my own blog for answers becuase that is one of my main reasons having this is to store information like this that I may need in the future, a reference for what I had done which I may be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1396&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Several days back I was asked a question in this blog relating to using JavaScript in a Sharepoint, it ended up with multiple questions and one was using jQuery in Sharepoint, as usual I searched my own blog for answers becuase that is one of my main reasons having this is to store information like this that I may need in the future, a reference for what I had done which I may be able to use again someday.  Anyways as I was searching and I found out that I havent posted this article, I thought I already have made one before but no, so this will be my chance.</p>
<p>So how would you use jQuery in Sharepoint? You can follow this easy steps and you will be ready to go in minutes</p>
<h3><span style="color:#3366ff;">1. Download jQuery</span></h3>
<p>Download latest verion of jQuery here  <a href="http://docs.jquery.com/Downloading_jQuery">http://docs.jquery.com/Downloading_jQuery</a></p>
<h3><span style="color:#3366ff;">2. Save it in an accessible location in Sharepoint</span></h3>
<p>Using <a href="http://sharepoint.microsoft.com/en-us/product/related-technologies/pages/sharepoint-designer.aspx" target="_blank">Sharepoint Designer</a> I added a Folder called &#8220;Scripts&#8221; inside the &#8220;Style Library&#8221; of the root level of the site. I then added the latest jQuery File into that folder.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/1-jquery-sp-path.jpg"><img class="alignnone size-full wp-image-1397" title="1 jQuery SP Path" src="http://anyrest.files.wordpress.com/2011/09/1-jquery-sp-path.jpg?w=630&#038;h=267" alt="" width="630" height="267" /></a></p>
<h3><span style="color:#3366ff;">3. Place a reference on that jQuery from your code</span></h3>
<p>The best place for this will be your master page so that it can be used anywhere in your site, so open the master page (by default SharePoint 2010 uses v4.master) you are using in Advanced mode then add your javascript reference like such.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/2-master-page-in-advanced-mode.jpg"><img class="alignnone size-full wp-image-1398" title="2 Master Page in Advanced Mode" src="http://anyrest.files.wordpress.com/2011/09/2-master-page-in-advanced-mode.jpg?w=630&#038;h=353" alt="" width="630" height="353" /></a></p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;"><span style="color:blue;">&lt;</span><span style="color:maroon;">script</span> <span style="color:red;">src</span><span style="color:blue;">="/Style%20Library/Scripts/jquery-1.6.2.js"</span> <span style="color:red;">type</span><span style="color:blue;">="text/javascript"&gt;&lt;/</span><span style="color:maroon;">script</span><span style="color:blue;">&gt;</span></pre>
<p>you place that inside the <span style="color:blue;">&lt;</span><span style="color:maroon;">head</span><span style="color:blue;">&gt;</span> tag</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/3-jquery-link.jpg"><img class="alignnone size-full wp-image-1399" title="3 jQuery Link" src="http://anyrest.files.wordpress.com/2011/09/3-jquery-link.jpg?w=630&#038;h=318" alt="" width="630" height="318" /></a></p>
<p>Save that master page and you are ready to go</p>
<h3><span style="color:#3366ff;">4. Test if its working</span></h3>
<p>If you are using FireFox open up firebug or if you are using IE you can press F12.  Go to the script tab to check whether your javascript reference is there, then on the console window try wether that reference works by typing something like this.</p>
<pre style="font-family:Consolas;font-size:13px;color:black;background:white;">$("#MSO_ContentTable").text("Welcome to Sharepoint jQuery")</pre>
<p>you should get a result something like this</p>
<p><a href="http://anyrest.files.wordpress.com/2011/09/4-try-it-out.jpg"><img class="alignnone size-full wp-image-1400" title="4 Try it out" src="http://anyrest.files.wordpress.com/2011/09/4-try-it-out.jpg?w=630&#038;h=462" alt="" width="630" height="462" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1396/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1396&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/09/06/using-jquery-with-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/1-jquery-sp-path.jpg" medium="image">
			<media:title type="html">1 jQuery SP Path</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/2-master-page-in-advanced-mode.jpg" medium="image">
			<media:title type="html">2 Master Page in Advanced Mode</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/3-jquery-link.jpg" medium="image">
			<media:title type="html">3 jQuery Link</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/09/4-try-it-out.jpg" medium="image">
			<media:title type="html">4 Try it out</media:title>
		</media:content>
	</item>
		<item>
		<title>Easily Determining what is the exact Error by using Correlation ID in Sharepoint 2010</title>
		<link>http://anyrest.wordpress.com/2011/08/18/easily-determining-what-is-the-exact-error-by-using-correlation-id-in-sharepoint-2010/</link>
		<comments>http://anyrest.wordpress.com/2011/08/18/easily-determining-what-is-the-exact-error-by-using-correlation-id-in-sharepoint-2010/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 09:00:48 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Diagnostics]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1384</guid>
		<description><![CDATA[Have you encountered an error that looks like this screenshot below? Well if you develop items in Sharepoint then most probably you had seen that not just once but a lot of times unless you are a really good developer that does not commit any mistakes. Now you are presented with a GUID without any [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1384&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you encountered an error that looks like this screenshot below?</p>
<p><a href="http://anyrest.files.wordpress.com/2011/08/1-error.gif"><img class="alignnone size-full wp-image-1385" title="1 Error" src="http://anyrest.files.wordpress.com/2011/08/1-error.gif?w=630" alt=""   /></a></p>
<p>Well if you develop items in Sharepoint then most probably you had seen that not just once but a lot of times unless you are a really good developer that does not commit any mistakes.</p>
<p>Now you are presented with a GUID without any other information so you might be wondering what the hell that means, so you Googled your problem and saw some solutions on how to determine what does that Correlation ID means by going to the server and in this folder location</p>
<pre>    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS</pre>
<p>and whoalla! you see a ton of logs that Sharepoint had generated over time.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/08/2-logs.jpg"><img title="2 Logs" src="http://anyrest.files.wordpress.com/2011/08/2-logs.jpg?w=630&#038;h=321" alt="" width="630" height="321" /></a></p>
<p>You then opened the log specific to time when your issue happened and to your surprise that GUID is non existent.  To your dismay you searched other logs that are near to the time your issue happened but still there&#8217;s not luck.  Well there are other easier means in searching for what you want and that is through the <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=20022" target="_blank">Microsoft SharePoint 2010 Administration Toolkit v2.0</a>.</p>
<p>That toolkit have some of the tools you need for diagnosing issues like such plus more, it has Load Testing Toolkit, User Profile Replication, Content Management Interoperability Services and much more.  Viewing logs is never<br />
been easier as the results are presented in a much meaningful way.  So how do I use the tool to check what that correlation ID means? Well here are the steps:</p>
<p>1. Install the tool in your server or even in your local machine that can connect to that server, installation file can be downloaded <a href="http://www.microsoft.com/download/en/confirmation.aspx?id=20022" target="_blank">here</a> for free.  Once downloaded installation is straightforward.</p>
<p>2. Once installed, its time to create your project.  First find the application on your start menu then go to SharePoint 2010 Administration Toolkit -&gt; SharePoint Diagnostic Studio.  Open that then click new project, you will then be presented with a screen similar to below, put your server name on the text box then hit &#8220;Create Project&#8221;.  Be patient it will take sometime to process that.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/08/3-create-project.jpg"><img title="3 Create Project" src="http://anyrest.files.wordpress.com/2011/08/3-create-project.jpg?w=348&#038;h=280" alt="" width="348" height="280" /></a></p>
<p>3. Once finished you will be presented with this screen, with some information.  In other slower servers you might see it a bit later and it will show &#8220;building report please wait&#8230;&#8221; but don&#8217;t worry its processing at  the background also please take now if there&#8217;s no data it retains that message.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/08/4-project-view.jpg"><img class="alignnone size-full wp-image-1388" title="4 Project View" src="http://anyrest.files.wordpress.com/2011/08/4-project-view.jpg?w=630&#038;h=476" alt="" width="630" height="476" /></a></p>
<p>4. Now you can start searching for your issue by clicking the search button on top.</p>
<p>5. Populate the needed text boxes and hit OK</p>
<p><a href="http://anyrest.files.wordpress.com/2011/08/5-search-correlation.jpg"><img class="alignnone size-full wp-image-1389" title="5 Search Correlation" src="http://anyrest.files.wordpress.com/2011/08/5-search-correlation.jpg?w=630" alt=""   /></a></p>
<p>6. Now you will be presented with what you are looking for.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/08/6-results.jpg"><img class="alignnone size-full wp-image-1390" title="6 Results" src="http://anyrest.files.wordpress.com/2011/08/6-results.jpg?w=630&#038;h=450" alt="" width="630" height="450" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1384/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1384&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/08/18/easily-determining-what-is-the-exact-error-by-using-correlation-id-in-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/08/1-error.gif" medium="image">
			<media:title type="html">1 Error</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/08/2-logs.jpg" medium="image">
			<media:title type="html">2 Logs</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/08/3-create-project.jpg" medium="image">
			<media:title type="html">3 Create Project</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/08/4-project-view.jpg" medium="image">
			<media:title type="html">4 Project View</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/08/5-search-correlation.jpg" medium="image">
			<media:title type="html">5 Search Correlation</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/08/6-results.jpg" medium="image">
			<media:title type="html">6 Results</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting the default record type of a user for Event Creation in Salesforce</title>
		<link>http://anyrest.wordpress.com/2011/07/19/getting-the-default-record-type-of-a-user-for-event-creation-in-salesforce/</link>
		<comments>http://anyrest.wordpress.com/2011/07/19/getting-the-default-record-type-of-a-user-for-event-creation-in-salesforce/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 10:00:04 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SalesForce]]></category>
		<category><![CDATA[SalesForce API]]></category>
		<category><![CDATA[SOQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1375</guid>
		<description><![CDATA[I have been twisting my head around in finding a solution for this simple task and I guess its worth while to share this information to everyone as I cannot search anything on Google or even in Developer Force any solution related to my problem. Eventually it ended up in calling their premier support for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1375&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://anyrest.files.wordpress.com/2011/07/event-record-type.jpg"><img class="alignnone size-full wp-image-1376" title="Event Record Type" src="http://anyrest.files.wordpress.com/2011/07/event-record-type.jpg?w=630&#038;h=231" alt="" width="630" height="231" /></a></p>
<p>I have been twisting my head around in finding a solution for this simple task and I guess its worth while to share this information to everyone as I cannot search anything on Google or even in <a href="http://developer.force.com/" target="_blank">Developer Force</a> any solution related to my problem. Eventually it ended up in calling their premier support for assistance, then after a week of emails sent back and forth the answer was:</p>
<p><span style="color:#ff0000;font-weight:bold;">&#8220;you cannot get the default record type for user without logging to salesforce.com&#8221;<br />
</span></p>
<p>what that means is that you cannot get the default record type for a user if you are using a privileged account, you must use the users login credentials when performing the query, which defeats the purpose of having a super user account creating events for you programatically.</p>
<p>Anyway here is what I initially done to get the default record type, At first they suggested this solution in Java</p>
<pre style="font-family:consolas;">Schema.DescribeSObjectResult oSObjectResult = <span style="color:#2b91af;">Event</span>.SObjectType.getDescribe();
<span style="color:#2b91af;">List</span>RecordTypeInfo&gt; oRecTypeInfos = oSObjectResult.getRecordTypeInfos();

Schema.RecordTypeInfo has a method isDefaultRecordTypeMapping()</pre>
<p>So I used that information to grab what I want in C#.Net, so I created a method to extract that information</p>
<pre style="font-family:consolas;"><span style="color:blue;">public</span> <span style="color:#2b91af;">SforceService</span> Authenticate()
{
    <span style="color:blue;">try</span>
    {
        <span style="color:#2b91af;">SforceService</span> oSalesForceService = <span style="color:blue;">new</span> <span style="color:#2b91af;">SforceService</span>();
        oSalesForceService.Timeout = 60000;

        <span style="color:green;">// Set Proxy Details if you are using one</span>
        <span style="color:#2b91af;">WebProxy</span> oWebProxy = <span style="color:blue;">new</span> <span style="color:#2b91af;">WebProxy</span>(<span style="color:#2b91af;">WebRequest</span>.DefaultWebProxy.GetProxy(<span style="color:blue;">new</span> <span style="color:#2b91af;">Uri</span>(oSalesForceService.Url.ToString())));
        oWebProxy.Credentials = <span style="color:#2b91af;">CredentialCache</span>.DefaultCredentials;
        oWebProxy.UseDefaultCredentials = <span style="color:blue;">true</span>;
        oSalesForceService.Proxy = oWebProxy;

        <span style="color:green;">//Initialize SalesForce Service</span>
        <span style="color:#2b91af;">LoginResult</span> oLoginResult = oSalesForceService.login(sUserName, <span style="color:blue;">string</span>.Concat(sPassword, sToken));

        oSalesForceService.Url = oLoginResult.serverUrl;
        oSalesForceService.SessionHeaderValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">SessionHeader</span>();
        oSalesForceService.SessionHeaderValue.sessionId = oLoginResult.sessionId;
        <span style="color:#2b91af;">GetUserInfoResult</span> oUserInfo = oLoginResult.userInfo;

        <span style="color:blue;">return</span> oSalesForceService;
    }
    <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)
    {
        <span style="color:blue;">return</span> <span style="color:blue;">null</span>;
    }
}

<span style="color:blue;">public</span> <span style="color:blue;">string</span> GetRecordType()
{
    <span style="color:#2b91af;">SforceService</span> oSalesForceService = Authenticate();

    <span style="color:blue;">string</span> sRecordTypeId = <span style="color:#a31515;">""</span>;

    <span style="color:#2b91af;">DescribeSObjectResult</span> oObjectResult = oSalesForceService.describeSObject(<span style="color:#a31515;">"Event"</span>);
    <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">RecordTypeInfo</span>&gt; oRecTypeInfos = oObjectResult.recordTypeInfos.ToList();
    <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">RecordTypeInfo</span> oRecTypeInfo <span style="color:blue;">in</span> oRecTypeInfos)
    {
        <span style="color:blue;">if</span> (oRecTypeInfo.defaultRecordTypeMapping == <span style="color:blue;">true</span>)
        {
            sRecordTypeId = oRecTypeInfo.recordTypeId;
        }
    }
    <span style="color:blue;">return</span> sRecordTypeId;
}</pre>
<p>Using that will be an issue as it is not showing options on filtering it by Owner ID like what SalesForce have in other tables, so the result shown after I executed the method is the default record type of the privileged Account.  So I thought I can go directly to the related tables perform SOQL queries like such</p>
<pre style="font-family:consolas;">oQueryResult = oSalesForceService.query(<span style="color:#a31515;">"Select Id, Name from RecordType where SobjectType = 'Event' and IsActive = True and OwnerID = '"</span> + sOwnerID +<span style="color:#a31515;">"'"</span>);</pre>
<p>and might have some hope in there but after checking the RecordType and RecordTypeInfo there is not field for Owner ID</p>
<p><a href="http://anyrest.files.wordpress.com/2011/07/event-record-type-2.jpg"><img class="alignnone size-full wp-image-1377" title="Event Record Type 2" src="http://anyrest.files.wordpress.com/2011/07/event-record-type-2.jpg?w=630" alt=""   /></a></p>
<p><a href="http://anyrest.files.wordpress.com/2011/07/event-record-type-3.jpg"><img class="alignnone size-full wp-image-1378" title="Event Record Type 3" src="http://anyrest.files.wordpress.com/2011/07/event-record-type-3.jpg?w=630" alt=""   /></a></p>
<p>and in User class there are no fields for Default record type.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/07/event-record-type-4.jpg"><img class="alignnone size-full wp-image-1379" title="Event Record Type 4" src="http://anyrest.files.wordpress.com/2011/07/event-record-type-4.jpg?w=630" alt=""   /></a></p>
<p>So the only real solution for this as of this post date is to keep a local copy of the default record type per user or as a Global setting if you wish.   So when I create event I assign the RecordTypeId by getting it by the &#8220;Record Type Name&#8221; defaulted to a user which is stored locally.</p>
<pre style="font-family:consolas;">oEvent.RecordTypeId = GetRecordTypeId(sUserDefaultRecordType);</pre>
<pre style="font-family:consolas;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> GetRecordTypeId(<span style="color:blue;">string</span> sRecordTypeName)
{
    <span style="color:#2b91af;">SforceService</span> oSalesForceService = Authenticate();

    <span style="color:#2b91af;">QueryResult</span> oQueryResult = <span style="color:blue;">null</span>;

    oSalesForceService.QueryOptionsValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">QueryOptions</span>();
    oQueryResult = oSalesForceService.query(<span style="color:#a31515;">"Select Id from RecordType where SobjectType = 'Event' and IsActive = True and Name = '"</span> + sRecordTypeName + <span style="color:#a31515;">"'"</span>);

    <span style="color:blue;">if</span> (oQueryResult.size != 0)
    {
        <span style="color:#2b91af;">RecordType</span> oRecordType = (<span style="color:#2b91af;">RecordType</span>)oQueryResult.records[0];
        <span style="color:blue;">return</span> oRecordType.Id;
    }
    <span style="color:blue;">return</span> <span style="color:blue;">null</span>;
}</pre>
<p>I hoped I helped someone out there with a similar situation as I have</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1375/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1375&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/07/19/getting-the-default-record-type-of-a-user-for-event-creation-in-salesforce/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/07/event-record-type.jpg" medium="image">
			<media:title type="html">Event Record Type</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/07/event-record-type-2.jpg" medium="image">
			<media:title type="html">Event Record Type 2</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/07/event-record-type-3.jpg" medium="image">
			<media:title type="html">Event Record Type 3</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/07/event-record-type-4.jpg" medium="image">
			<media:title type="html">Event Record Type 4</media:title>
		</media:content>
	</item>
		<item>
		<title>Synchronizing Google Groups and Active Directory Group Members</title>
		<link>http://anyrest.wordpress.com/2011/07/12/synchronizing-google-groups-and-active-directory-group-members/</link>
		<comments>http://anyrest.wordpress.com/2011/07/12/synchronizing-google-groups-and-active-directory-group-members/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 10:00:43 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[Google Apps]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1364</guid>
		<description><![CDATA[I have been doing a lot of synchronization projects lately like SalesForce events and Google Calendar Events, Active Directory and some Payroll Application and now Google Groups and Active Directory Groups.   It may sound too complicated to develop but trust it is not as complicated as you think. For this article I will be presenting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1364&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been doing a lot of synchronization projects lately like SalesForce events and Google Calendar Events, Active Directory and some Payroll Application and now Google Groups and Active Directory Groups.   It may sound too complicated to develop but trust it is not as complicated as you think.</p>
<p>For this article I will be presenting you a working code one way group membership synchronization that you can start with.  I hope this post will show you the concepts needed to achieve our end result which is synchronizing members in Active Directory and Google Groups. Before you get started I am assuming that you have Google Apps for your domain already setup and running for your organization otherwise you need to have it first before proceeding.</p>
<p>Now let&#8217;s get started, first you need to download the Google Apps .Net Client Library which you can find <a href="http://code.google.com/p/google-gdata/downloads/list" target="_blank">here</a>.  Once downloaded set it up then were ready to roll.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/07/1-add-reference.jpg"><img class="alignnone size-full wp-image-1365" title="1 Add Reference" src="http://anyrest.files.wordpress.com/2011/07/1-add-reference.jpg?w=630&#038;h=327" alt="" width="630" height="327" /></a></p>
<p>Fire up your Visual Studio and add the following dll&#8217;s (Google.GData.Apps.dll and Google.GData.Client.dll) from the installed Google API folder, you will also need System.DirectoryServices and System.DirectoryServices.AccountManagement which can be found in the Assemblies repository.</p>
<p>First we need to declare some reusable variables which are</p>
<pre style="font-family:consolas;"><span style="color:blue;">private</span> <span style="color:blue;">string</span> sDomain = <span style="color:#a31515;">"yourcompanydomain.com"</span>;
<span style="color:blue;">private</span> <span style="color:blue;">string</span> sGoogleServiceUrl = <span style="color:#a31515;">"https://apps-apis.google.com/a/feeds/group/2.0/"</span>;
<span style="color:blue;">private</span> <span style="color:blue;">string</span> sEmailRegEx = <span style="color:#a31515;">@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"</span>;</pre>
<p>We also need a method to Initialize Google Service, take note I had also included methods for using proxy servers.</p>
<pre style="font-family:consolas;"><span style="color:gray;">/////</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> Initializes Google Service </span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;returns&gt;</span><span style="color:green;">Autheticated Session to Google Service</span><span style="color:gray;">&lt;/returns&gt;</span>
<span style="color:blue;">private</span> <span style="color:#2b91af;">GroupsService</span> GoogleServiceAuthenticate()
{
    <span style="color:green;">//Initialize Calendar Service</span>
    Google.GData.Apps.Groups.<span style="color:#2b91af;">GroupsService</span> oGService = <span style="color:blue;">new</span> <span style="color:#2b91af;">GroupsService</span>(sDomain, <span style="color:#a31515;">"your_application"</span>);
    oGService.setUserCredentials(<span style="color:#a31515;">"username"</span>, <span style="color:#a31515;">"password"</span>);

    <span style="color:green;">//If you are using proxy servers</span>
    <span style="color:#2b91af;">GDataRequestFactory</span> oRequestFactory = (<span style="color:#2b91af;">GDataRequestFactory</span>)oGService.RequestFactory;

    <span style="color:#2b91af;">WebProxy</span> oWebProxy = <span style="color:blue;">new</span> <span style="color:#2b91af;">WebProxy</span>(<span style="color:#2b91af;">WebRequest</span>.DefaultWebProxy.GetProxy(<span style="color:blue;">new</span> <span style="color:#2b91af;">Uri</span>(sGoogleServiceUrl + sDomain)));
    oWebProxy.Credentials = <span style="color:#2b91af;">CredentialCache</span>.DefaultCredentials;
    oWebProxy.UseDefaultCredentials = <span style="color:blue;">true</span>;
    oRequestFactory.Proxy = oWebProxy;

    <span style="color:blue;">return</span> oGService;
}</pre>
<p>Here are some Active Directory Methods that we will use, these are all derived from my AD Methods Library which can be found <a href="http://anyrest.wordpress.com/2010/06/28/active-directory-c/" target="_blank">here. </a></p>
<pre style="font-family:consolas;"><span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> Returns Groups with Email Addresses</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;param name="sOU"&gt;</span><span style="color:green;">The Organizational Unit To Search</span><span style="color:gray;">&lt;/param&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;returns&gt;</span><span style="color:green;">All Groups with Email Addresses on the Specified OU</span><span style="color:gray;">&lt;/returns&gt;</span>
<span style="color:blue;">private</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Principal</span>&gt; GetGroupsWithEmail(<span style="color:blue;">string</span> sOU)
{
    <span style="color:#2b91af;">PrincipalContext</span> oPrincipalContext;
    oPrincipalContext = <span style="color:blue;">new</span> <span style="color:#2b91af;">PrincipalContext</span>(<span style="color:#2b91af;">ContextType</span>.Domain, sDomain, sOU, <span style="color:#2b91af;">ContextOptions</span>.Negotiate, <span style="color:#a31515;">"youractivedirectoryuser"</span>, <span style="color:#a31515;">"password"</span>);

    <span style="color:blue;">var</span> oListGroups = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Principal</span>&gt;();

    <span style="color:#2b91af;">PrincipalSearcher</span> oPrincipalSearcher = <span style="color:blue;">new</span> <span style="color:#2b91af;">PrincipalSearcher</span>();
    <span style="color:#2b91af;">GroupPrincipalExtended</span> oGroupPrincipal = <span style="color:blue;">new</span> <span style="color:#2b91af;">GroupPrincipalExtended</span>(oPrincipalContext);

    <span style="color:green;">//Get Anything with @ that indicates an email, I'm just lazy you can filter it better</span>
    oGroupPrincipal.EmailAddress = <span style="color:#a31515;">"*@*"</span>;

    oPrincipalSearcher.QueryFilter = oGroupPrincipal;
    oListGroups = oPrincipalSearcher.FindAll().ToList&lt;<span style="color:#2b91af;">Principal</span>&gt;();

    <span style="color:blue;">return</span> oListGroups;
}</pre>
<pre style="font-family:consolas;"><span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;summary&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> Gets the Property of a Directory Entry</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;/summary&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">oPrincipal"&gt;</span><span style="color:green;">Prinicipal Object</span><span style="color:gray;">&lt;/param&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">sPropertyName"&gt;</span><span style="color:green;">The Name of the Property to get</span><span style="color:gray;">&lt;/param&gt;</span>
<span style="color:gray;">///</span><span style="color:green;"> </span><span style="color:gray;">&lt;returns&gt;</span><span style="color:green;">Value of the Propery Object</span><span style="color:gray;">&lt;/returns&gt;</span>
<span style="color:blue;">public</span> <span style="color:blue;">string</span> GetProperty(<span style="color:#2b91af;">Principal</span> oPrincipal, <span style="color:blue;">string</span> sPropertyName)
{
    <span style="color:#2b91af;">DirectoryEntry</span> oDE = (oPrincipal.GetUnderlyingObject() <span style="color:blue;">as</span> <span style="color:#2b91af;">DirectoryEntry</span>);

    <span style="color:blue;">if</span> (oDE.Properties.Contains(sPropertyName))
    {
        <span style="color:blue;">return</span> oDE.Properties[sPropertyName][0].ToString();
    }
    <span style="color:blue;">else</span>
    {
        <span style="color:blue;">return</span> <span style="color:blue;">string</span>.Empty;
    }
}</pre>
<p>You also need to extend the GroupPrincipal Class to expose the Email address field so that we can easily refer to it.   By default Email Addresses are not exposed in the GroupPrincipal and here is the full article explaining <a href="http://anyrest.wordpress.com/2010/10/14/how-to-use-ad-attributes-not-represented-in-userprincipal-groupprincipal-and-computerprincipal/" target="_blank">how to extend the GroupPrincipal Class to expose values not represented in the GroupPrincipal</a>.</p>
<pre style="font-family:consolas;">[<span style="color:#2b91af;">DirectoryObjectClass</span>(<span style="color:#a31515;">"group"</span>)]
[<span style="color:#2b91af;">DirectoryRdnPrefix</span>(<span style="color:#a31515;">"CN"</span>)]

<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">GroupPrincipalExtended</span> : <span style="color:#2b91af;">GroupPrincipal</span>
{
    <span style="color:blue;">public</span> GroupPrincipalExtended(<span style="color:#2b91af;">PrincipalContext</span> context) : <span style="color:blue;">base</span>(context) { }

    <span style="color:blue;">public</span> GroupPrincipalExtended(<span style="color:#2b91af;">PrincipalContext</span> context, <span style="color:blue;">string</span> samAccountName)
        : <span style="color:blue;">base</span>(context, samAccountName)
    {
    }

    [<span style="color:#2b91af;">DirectoryProperty</span>(<span style="color:#a31515;">"mail"</span>)]
    <span style="color:blue;">public</span> <span style="color:blue;">string</span> EmailAddress
    {
        <span style="color:blue;">get</span>
        {
            <span style="color:blue;">if</span> (ExtensionGet(<span style="color:#a31515;">"mail"</span>).Length != 1)
                <span style="color:blue;">return</span> <span style="color:blue;">null</span>;

            <span style="color:blue;">return</span> (<span style="color:blue;">string</span>)ExtensionGet(<span style="color:#a31515;">"mail"</span>)[0];

        }
        <span style="color:blue;">set</span> { <span style="color:blue;">this</span>.ExtensionSet(<span style="color:#a31515;">"mail"</span>, <span style="color:blue;">value</span>); }
    }
}</pre>
<p>Now we got everything we need let&#8217;s do the actual syncing</p>
<pre style="font-family:consolas;"><span style="color:blue;">private</span> <span style="color:blue;">void</span> Synchronize()
{
    <span style="color:green;">//Get all Active Directory Groups with Email</span>
    <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Principal</span>&gt; oGroupsWithEmail = GetGroupsWithEmail(<span style="color:#a31515;">"OU=Your Groups OU,DC=yourcompanydomain,DC=com"</span>);

    <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">GroupPrincipalExtended</span> oGroup <span style="color:blue;">in</span> oGroupsWithEmail)
    {
        <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; oMembersInGoogle = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;();
        <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; oMembersInAD = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;();

        <span style="color:blue;">try</span>
        {
            <span style="color:green;">//Instansiate Google Service</span>
            <span style="color:#2b91af;">GroupsService</span> oGService = GoogleServiceAuthenticate();

            <span style="color:green;">//Set User URI</span>
            <span style="color:#2b91af;">Uri</span> oGroupMemberUri = <span style="color:blue;">new</span> <span style="color:#2b91af;">Uri</span>(sGoogleServiceUrl + sDomain + <span style="color:#a31515;">"/"</span> + oGroup.EmailAddress + <span style="color:#a31515;">"/member/"</span>);

            <span style="color:green;">//Get Google Group Members</span>
            <span style="color:#2b91af;">AppsExtendedFeed</span> oGoogleGroupMembers = oGService.RetrieveAllMembers(oGroup.EmailAddress, <span style="color:blue;">true</span>);
            <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">AtomEntry</span> oUserFeedItem <span style="color:blue;">in</span> oGoogleGroupMembers.Entries)
            {
                <span style="color:blue;">string</span> sMemberEmail = oUserFeedItem.SelfUri.ToString().Replace(<span style="color:#a31515;">"%40"</span>, <span style="color:#a31515;">"@"</span>).Replace(oGroupMemberUri.ToString(), <span style="color:#a31515;">""</span>);
                <span style="color:green;">//Add in Google Members List for Later Comparison</span>
                oMembersInGoogle.Add(sMemberEmail.ToLower());
            }

            <span style="color:green;">//Get AD Group Members</span>
            <span style="color:#2b91af;">PrincipalSearchResult</span>&lt;<span style="color:#2b91af;">Principal</span>&gt; oPrincipals = oGroup.GetMembers();
            <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Principal</span> oPrincipal <span style="color:blue;">in</span> oPrincipals)
            {
                <span style="color:green;">//If Principal Type is User</span>
                <span style="color:blue;">if</span> (oPrincipal.StructuralObjectClass == <span style="color:#a31515;">"user"</span>)
                {
                    <span style="color:#2b91af;">UserPrincipal</span> oUserPrincipal = (<span style="color:#2b91af;">UserPrincipal</span>)oPrincipal;
                    <span style="color:blue;">if</span> (<span style="color:blue;">bool</span>.Parse(oUserPrincipal.Enabled.ToString()) &amp;&amp;
                        (oUserPrincipal.EmailAddress != <span style="color:#a31515;">""</span> &amp;&amp; oUserPrincipal.EmailAddress != <span style="color:blue;">null</span>) &amp;&amp;
                        System.Text.RegularExpressions.<span style="color:#2b91af;">Regex</span>.IsMatch(oUserPrincipal.EmailAddress, sEmailRegEx))
                    {
                        <span style="color:green;">//Add in Active Directory Email List for Later Comparison</span>
                        oMembersInAD.Add(oUserPrincipal.EmailAddress.ToLower());
                    }
                }
                <span style="color:green;">//If Principal Type is Group</span>
                <span style="color:blue;">if</span> (oPrincipal.StructuralObjectClass == <span style="color:#a31515;">"group"</span>)
                {
                    <span style="color:#2b91af;">GroupPrincipal</span> oGroupPrincipal = (<span style="color:#2b91af;">GroupPrincipal</span>)oPrincipal;
                    <span style="color:blue;">string</span> sGroupPrincipalEmail = GetProperty(oGroupPrincipal, <span style="color:#a31515;">"mail"</span>);
                    <span style="color:blue;">if</span> ((sGroupPrincipalEmail != <span style="color:#a31515;">""</span> &amp;&amp;
                        sGroupPrincipalEmail != <span style="color:blue;">null</span>) &amp;&amp;
                        System.Text.RegularExpressions.<span style="color:#2b91af;">Regex</span>.IsMatch(sGroupPrincipalEmail, sEmailRegEx))
                    {
                        <span style="color:green;">//Add in Active Directory Email List for Later Comparison</span>
                        oMembersInAD.Add(sGroupPrincipalEmail.ToLower());
                    }
                }
            }

            <span style="color:green;">//Add Members in Google</span>
            <span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> sMemberInAD <span style="color:blue;">in</span> oMembersInAD)
            {
                <span style="color:green;">//Check if Email is in Active Directory Email List but not in Google Members List</span>
                <span style="color:blue;">if</span> (!oMembersInGoogle.Contains(sMemberInAD.ToLower()))
                {
                    oGService.AddMemberToGroup(sMemberInAD, oGroup.EmailAddress);
                }
            }

            <span style="color:green;">//Remove Members in Google</span>
            <span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> sMemberInGoogle <span style="color:blue;">in</span> oMembersInGoogle)
            {
                <span style="color:blue;">if</span> (!oMembersInAD.Contains(sMemberInGoogle.ToLower()))
                {
                    <span style="color:green;">//Check if Email is in Google Members List but not in Active Directory Email List</span>
                    oGService.RemoveMemberFromGroup(sMemberInGoogle, oGroup.EmailAddress);
                }
            }
        }
        <span style="color:blue;">catch</span> (Google.GData.Apps.<span style="color:#2b91af;">AppsException</span> gEx)
        {
            <span style="color:blue;">if</span> (gEx.ErrorCode == <span style="color:#a31515;">"1301"</span>)
            {
                <span style="color:green;">//Entity Not found whihc means, Google Groups is not existing</span>
                <span style="color:green;">//Do your create group method here</span>
                CreateGoogleGroup(oGroup.EmailAddress, oGroup.DisplayName, oGroup.Description, sEmailPermission, oMembersInAD);
            }
            <span style="color:blue;">else</span>
            {
                <span style="color:green;">//Other Google Error Codes to Catch</span>
            }
        }
        <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex) 
        {
            <span style="color:green;">//Exception</span>
        }
    }
}</pre>
<pre style="font-family:consolas;"><a href="http://anyrest.files.wordpress.com/2011/07/2-google-groups1.jpg"><img class="alignnone size-full wp-image-1371" title="2 Google Groups" src="http://anyrest.files.wordpress.com/2011/07/2-google-groups1.jpg?w=630" alt=""   /></a></pre>
<p>Now the code above shows you how to synchronize Active Directory Group Members to Google Groups which includes adding and removing users.  It is performing a one way sync which treats Active Directory as the master record repository.  Also if you had noticed that there is a catch for the Google.GData.Apps.AppsException this will catch any error that might be encountered on running the sync like 1301 which means Group with that Email Address was not found.  For a full list of Error code refer to the list<br />
below (which I got <a href="http://code.google.com/googleapps/domain/gdata_provisioning_api_v2.0_reference.html#appendix_dGroup" target="_blank">here</a>)</p>
<p>1000 &#8211; Unknown Error<br />
1100 &#8211; User Deleted Recently<br />
1101 &#8211; User Suspended<br />
1200 &#8211; Domain User Limit Exceeded<br />
1201 &#8211; Domain Alias Limit Exceeded<br />
1202 &#8211; Domain Suspended<br />
1203 &#8211; Domain Feature Unavailable<br />
1300 &#8211; Entity Exists<br />
1301 &#8211; Entity Does Not Exist<br />
1302 &#8211; Entity Name Is Reserved<br />
1303 &#8211; Entity Name Not Valid<br />
1400 &#8211; Invalid Given Name<br />
1401 &#8211; Invalid Family Name<br />
1402 &#8211; Invalid Password<br />
1403 &#8211; Invalid Username<br />
1404 &#8211; Invalid Hash Function Name<br />
1405 &#8211; Invalid Hash Digest Length<br />
1406 &#8211; Invalid Email Address<br />
1407 &#8211; Invalid Query Parameter Value<br />
1500 &#8211; Too Many Recipients On Email List</p>
<p>On the Next Article I will be expanding further on that CreateGoogleGroup function.  Happy Coding!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1364/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1364&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/07/12/synchronizing-google-groups-and-active-directory-group-members/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/07/1-add-reference.jpg" medium="image">
			<media:title type="html">1 Add Reference</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/07/2-google-groups1.jpg" medium="image">
			<media:title type="html">2 Google Groups</media:title>
		</media:content>
	</item>
		<item>
		<title>Approval Moderation in Sequential Workflow Project</title>
		<link>http://anyrest.wordpress.com/2011/05/20/approval-moderation-in-sequential-workflow-project/</link>
		<comments>http://anyrest.wordpress.com/2011/05/20/approval-moderation-in-sequential-workflow-project/#comments</comments>
		<pubDate>Fri, 20 May 2011 10:30:55 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Workflows]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1342</guid>
		<description><![CDATA[Have you ever wondered how to automate Sharepoint approval process in a Sequential Workflow Project? Well look no futher this article is about that. Lets start with a scenario to be clear, say we have a product list where someone in your company approves every product listing based on thresholds, that threshold measure can be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1342&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered how to automate Sharepoint approval process in a Sequential Workflow Project? Well look no futher this article is about that.</p>
<p>Lets start with a scenario to be clear, say we have a product list where someone in your company approves every product listing based on thresholds, that threshold measure can be a price range on a certain product type. Now that person usually declines desk product types when the price indicated by the contributor is below $200 and above $500, at this point the approver will do it manually by looking whether the product have violated the known threshold, this is an easy task for 10 items but imagine managing 300 of them. That is why we will be automating that based on definitions that we will set on a separate list. That list that will be creating will define threshold for different product type so if it hits any of the barriers I will decline the list item and the ones that does not will be auto approved. Sound simple? read further.</p>
<p>You might be already thinking that we can achieve that by using the Column Validation of a List. Well not quite as there are some limitations and one important one is the support for boolean operator. So a formula like this would work</p>
<pre>=[Product Price] &lt; 500</pre>
<p>but this will not</p>
<pre>=[Product Price] &lt; 500 AND [Product Price] &gt; 500</pre>
<p><a href="http://anyrest.files.wordpress.com/2011/05/0-1-column-validation.jpg"><img class="alignnone size-full wp-image-1344" title="0.1. Column Validation" src="http://anyrest.files.wordpress.com/2011/05/0-1-column-validation.jpg?w=630" alt=""   /></a></p>
<p>Now well you might think again that we can achieve that on the Sharpoint Designer. Well not quite as well as the Sharepoint Designer will only give you 1 search condition for a list lookup. So for example you need to lookup for a price threshold which meets the condition in multiple columns like for example a product type and maximum date of validity then it will be impossible.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/0-2-lookup-validation.jpg"><img class="alignnone size-full wp-image-1345" title="0.2. Lookup Validation" src="http://anyrest.files.wordpress.com/2011/05/0-2-lookup-validation.jpg?w=630&#038;h=318" alt="" width="630" height="318" /></a></p>
<p>So our last resort is using Visual Studio! So in this post I will explain to you how this will be achieved in the simplest manner. All you need is to follow this steps and I can assue you can make more complex scenarios after this one.</p>
<h3><span style="color:#3366ff;">1. Create Lists for Product and Product Type</span></h3>
<p><strong>Product Type List</strong></p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/1-product-type-alt.jpg"><img class="alignnone size-full wp-image-1346" title="1. Product Type - ALT" src="http://anyrest.files.wordpress.com/2011/05/1-product-type-alt.jpg?w=630" alt=""   /></a></p>
<p><strong>Product List</strong></p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/2-product-list.jpg"><img class="alignnone size-full wp-image-1347" title="2. Product List" src="http://anyrest.files.wordpress.com/2011/05/2-product-list.jpg?w=630" alt=""   /></a></p>
<p>Take note that the column Product Type Column in Product List is a lookup on Product Type List</p>
<h3><span style="color:#3366ff;">2. Enable Versioning in Product</span></h3>
<p>As you can see we need to require content approval, we need to create a version each time (this ensures that you can roll back) and choose &#8220;only users who can approve items&#8221; this makes sure only the authorized can approve the items manually</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/3-list-versioning-settings.jpg"><img class="alignnone size-full wp-image-1348" title="3. List Versioning Settings" src="http://anyrest.files.wordpress.com/2011/05/3-list-versioning-settings.jpg?w=630" alt=""   /></a></p>
<h3><span style="color:#3366ff;">3. Create a Task List</span></h3>
<p>This is a library type in Sharepoint and the default one would do.</p>
<h3><span style="color:#3366ff;">4. Create a Sequential Workflow Project</span></h3>
<p>Choose Sequential Workflow under Sharepoint -&gt; 2010</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/4-sequential-workflow.jpg"><img class="alignnone size-full wp-image-1349" title="4. Sequential Workflow" src="http://anyrest.files.wordpress.com/2011/05/4-sequential-workflow.jpg?w=630&#038;h=354" alt="" width="630" height="354" /></a></p>
<p>Follow the wizard</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/5-sharepoint-customization-wizard.jpg"><img class="alignnone size-full wp-image-1350" title="5. SharePoint Customization Wizard" src="http://anyrest.files.wordpress.com/2011/05/5-sharepoint-customization-wizard.jpg?w=630&#038;h=447" alt="" width="630" height="447" /></a></p>
<p>Choose the List you will invoke the workflow from and the associated Task List and History List</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/6-sharepoint-customization-wizard.jpg"><img class="alignnone size-full wp-image-1351" title="6. SharePoint Customization Wizard" src="http://anyrest.files.wordpress.com/2011/05/6-sharepoint-customization-wizard.jpg?w=630&#038;h=447" alt="" width="630" height="447" /></a></p>
<p>Trigger it on create and update</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/7-sharepoint-customization-wizard.jpg"><img class="alignnone size-full wp-image-1352" title="7. SharePoint Customization Wizard" src="http://anyrest.files.wordpress.com/2011/05/7-sharepoint-customization-wizard.jpg?w=630&#038;h=447" alt="" width="630" height="447" /></a></p>
<p>Now you will be presented with a Workflow Diagram</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/8-workflowproject.jpg"><img class="alignnone size-full wp-image-1353" title="8. WorkflowProject" src="http://anyrest.files.wordpress.com/2011/05/8-workflowproject.jpg?w=630" alt=""   /></a></p>
<p>You can create more complex stuff by choosing items on the toolbar</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/9-workflowproject.jpg"><img class="alignnone size-full wp-image-1354" title="9. WorkflowProject" src="http://anyrest.files.wordpress.com/2011/05/9-workflowproject.jpg?w=630&#038;h=881" alt="" width="630" height="881" /></a></p>
<p>but for this instance we will be making this straightforward and do everything on the onWorkflowActivated section. Now double click that and you will be presented with a code behind. Now copy the code below.</p>
<pre style="font-family:consolas;"><span style="color:blue;">private</span> <span style="color:blue;">void</span> onWorkflowActivated1_Invoked(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">ExternalDataEventArgs</span> e)
{

    <span style="color:#2b91af;">SPLinqDataContext</span> dc = <span style="color:blue;">new</span> <span style="color:#2b91af;">SPLinqDataContext</span>(workflowProperties.SiteUrl);

    <span style="color:#2b91af;">EntityList</span>&lt;<span style="color:#2b91af;">ProductTypeItem</span>&gt; ProductType = dc.GetList&lt;<span style="color:#2b91af;">ProductTypeItem</span>&gt;(<span style="color:#a31515;">"Product Type"</span>);

    <span style="color:blue;">string</span> sProductType = GetLinkedListItemString(workflowProperties.Item[<span style="color:#a31515;">"Product Type"</span>].ToString());
    <span style="color:blue;">double</span> dPrice = <span style="color:blue;">double</span>.Parse(workflowProperties.Item[<span style="color:#a31515;">"Product Price"</span>].ToString());

    <span style="color:blue;">var</span> Result = (<span style="color:blue;">from</span> p <span style="color:blue;">in</span> ProductType
                    <span style="color:blue;">where</span> p.Title == sProductType &amp;&amp; p.MaxDateValidity &gt;= <span style="color:#2b91af;">DateTime</span>.Now
                    <span style="color:blue;">select</span> p).SingleOrDefault();

    <span style="color:blue;">if</span> (Result.MinPrice &gt; dPrice)
    {
        workflowProperties.Item.ModerationInformation.Status = <span style="color:#2b91af;">SPModerationStatusType</span>.Denied;
        workflowProperties.Item.ModerationInformation.Comment = <span style="color:#a31515;">"Price Below Threshold - Saved on Approval Comment"</span>;
        workflowProperties.Item[<span style="color:#a31515;">"Notes"</span>] = <span style="color:#a31515;">"Price Below Threshold - Saved on Notes Field"</span>;
        workflowProperties.Item.Update();
    }
    <span style="color:blue;">else</span> <span style="color:blue;">if</span> (Result.MaxPrice &lt; dPrice)
    {
        workflowProperties.Item.ModerationInformation.Status = <span style="color:#2b91af;">SPModerationStatusType</span>.Denied;
        workflowProperties.Item.ModerationInformation.Comment = <span style="color:#a31515;">"Price Above Threshold - Saved on Approval Comment"</span>;
        workflowProperties.Item[<span style="color:#a31515;">"Notes"</span>] = <span style="color:#a31515;">"Price Above Threshold - Saved on Notes Field"</span>;
        workflowProperties.Item.Update();
    }
    <span style="color:blue;">else</span>
    {
        workflowProperties.Item.ModerationInformation.Status = <span style="color:#2b91af;">SPModerationStatusType</span>.Approved;
        workflowProperties.Item.Update();
    }

}

<span style="color:blue;">private</span> <span style="color:blue;">string</span> GetLinkedListItemString(<span style="color:blue;">string</span> sItem)
{
    <span style="color:blue;">if</span> (sItem.Contains(<span style="color:#a31515;">"#"</span>))
    {
        <span style="color:blue;">return</span> sItem.Substring(sItem.LastIndexOf(<span style="color:#a31515;">"#"</span>) + 1);
    }
    <span style="color:blue;">else</span>
    {
        <span style="color:blue;">return</span> sItem;
    }
}</pre>
<p>If you notice I am uisng here LINQ to Sharepoint which you can refer to this post for a <a href="http://anyrest.wordpress.com/2011/02/10/how-to-use-linq-to-sharepoint/" target="_blank">full tutorial</a>, this makes my life easier in querying Sharepoint Lists</p>
<p>Now to explain some points on the code above:</p>
<ul>
<li>To get and set item values on the current list item that is being processed use workflowProperties.Item["Column Name"]</li>
<li>To set Approval Status use workflowProperties.Item.ModerationInformation.Status</li>
<li>Since we are querying LINQ style, the search conditions can be as complex as you want and you are just limited by your imagination</li>
<li>You can also see that we use the Approval Comment field and the Notes Custom Field we created, I just wnat to demonstrate to you how to save comments on Approval fields as well as a field in your list.</li>
<li>If you use a column that is coming from a linked list it will show in this format ID;#Value (i.e. &#8220;1;#Test&#8221;)</li>
</ul>
<p><a href="http://anyrest.files.wordpress.com/2011/05/hashed-value.jpg"><img class="alignnone size-full wp-image-1355" title="Hashed Value" src="http://anyrest.files.wordpress.com/2011/05/hashed-value.jpg?w=630&#038;h=85" alt="" width="630" height="85" /></a></p>
<p>Now other than that I guess the code above is straightforward as the only thing it does is when price is above or below threshold levels then it will decline the list entry and leave a note.</p>
<p>Run you project then put in your data.</p>
<h3><span style="color:#3366ff;">5. Run your code</span></h3>
<p>First Add items in Product Type to define thresholds</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/10-product-type-list-alt.jpg"><img class="alignnone size-full wp-image-1356" title="10. Product Type List - ALT" src="http://anyrest.files.wordpress.com/2011/05/10-product-type-list-alt.jpg?w=630" alt=""   /></a></p>
<p>Add a new product that will violate your threshold</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/11-add-new-product.jpg"><img class="alignnone size-full wp-image-1357" title="11. Add new Product" src="http://anyrest.files.wordpress.com/2011/05/11-add-new-product.jpg?w=630" alt=""   /></a></p>
<p>Now you can see the progress once its submitted</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/12-workflow-progress.jpg"><img class="alignnone size-full wp-image-1358" title="12. Workflow Progress" src="http://anyrest.files.wordpress.com/2011/05/12-workflow-progress.jpg?w=630&#038;h=258" alt="" width="630" height="258" /></a></p>
<p>Then once its finished, you will see the note and the final approval status</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/13-workflow-progress-2.jpg"><img class="alignnone size-full wp-image-1359" title="13. Workflow Progress 2" src="http://anyrest.files.wordpress.com/2011/05/13-workflow-progress-2.jpg?w=630&#038;h=256" alt="" width="630" height="256" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1342/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1342/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1342/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1342/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1342/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1342/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1342/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1342/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1342&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/05/20/approval-moderation-in-sequential-workflow-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/0-1-column-validation.jpg" medium="image">
			<media:title type="html">0.1. Column Validation</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/0-2-lookup-validation.jpg" medium="image">
			<media:title type="html">0.2. Lookup Validation</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/1-product-type-alt.jpg" medium="image">
			<media:title type="html">1. Product Type - ALT</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/2-product-list.jpg" medium="image">
			<media:title type="html">2. Product List</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/3-list-versioning-settings.jpg" medium="image">
			<media:title type="html">3. List Versioning Settings</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/4-sequential-workflow.jpg" medium="image">
			<media:title type="html">4. Sequential Workflow</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/5-sharepoint-customization-wizard.jpg" medium="image">
			<media:title type="html">5. SharePoint Customization Wizard</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/6-sharepoint-customization-wizard.jpg" medium="image">
			<media:title type="html">6. SharePoint Customization Wizard</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/7-sharepoint-customization-wizard.jpg" medium="image">
			<media:title type="html">7. SharePoint Customization Wizard</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/8-workflowproject.jpg" medium="image">
			<media:title type="html">8. WorkflowProject</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/9-workflowproject.jpg" medium="image">
			<media:title type="html">9. WorkflowProject</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/hashed-value.jpg" medium="image">
			<media:title type="html">Hashed Value</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/10-product-type-list-alt.jpg" medium="image">
			<media:title type="html">10. Product Type List - ALT</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/11-add-new-product.jpg" medium="image">
			<media:title type="html">11. Add new Product</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/12-workflow-progress.jpg" medium="image">
			<media:title type="html">12. Workflow Progress</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/13-workflow-progress-2.jpg" medium="image">
			<media:title type="html">13. Workflow Progress 2</media:title>
		</media:content>
	</item>
		<item>
		<title>Converting System.Linq.IQueryable interface to System.Data.DataSet</title>
		<link>http://anyrest.wordpress.com/2011/05/20/converting-system-linq-iqueryable-interface-to-system-data-dataset/</link>
		<comments>http://anyrest.wordpress.com/2011/05/20/converting-system-linq-iqueryable-interface-to-system-data-dataset/#comments</comments>
		<pubDate>Fri, 20 May 2011 10:00:52 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1340</guid>
		<description><![CDATA[Just a quick tip, in case you need to convert a System.Linq.IQueryable interface to System.Data.DataSet then check at the codes below private DataSet LINQToDataSet({Your LINQ Data Context} myDB, IQueryable item) {     SqlCommand cmd = myDB.GetCommand(item) as SqlCommand;     DataTable oDataTable = new DataTable();     SqlDataAdapter oDataAdapter = new SqlDataAdapter(cmd);     oDataAdapter.Fill(oDataTable);     DataSet oDataSet = new DataSet();     oDataSet.Tables.Add(oDataTable);     return oDataSet; } Usage: var oTableResult = (from p in myDB.SampleTable                 where p.SampleColumn == "Test Value"                 select p); LINQToDataSet(myDB, oTableResult);<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1340&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just a quick tip, in case you need to convert a System.Linq.IQueryable interface to System.Data.DataSet then check at the codes below</p>
<pre style="font-family:consolas;"><span style="color:blue;">private</span> <span style="color:#2b91af;">DataSet</span> LINQToDataSet(<span style="color:#cc3399;"><strong><span style="text-decoration:underline;">{Your LINQ Data Context}</span></strong></span> myDB, <span style="color:#2b91af;">IQueryable</span> item)
{
    <span style="color:#2b91af;">SqlCommand</span> cmd = myDB.GetCommand(item) <span style="color:blue;">as</span> <span style="color:#2b91af;">SqlCommand</span>;

    <span style="color:#2b91af;">DataTable</span> oDataTable = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataTable</span>();
    <span style="color:#2b91af;">SqlDataAdapter</span> oDataAdapter = <span style="color:blue;">new</span> <span style="color:#2b91af;">SqlDataAdapter</span>(cmd);
    oDataAdapter.Fill(oDataTable);

    <span style="color:#2b91af;">DataSet</span> oDataSet = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataSet</span>();
    oDataSet.Tables.Add(oDataTable);
    <span style="color:blue;">return</span> oDataSet;
}</pre>
<p>Usage:</p>
<pre style="font-family:consolas;"><span style="color:blue;">var</span> oTableResult = (<span style="color:blue;">from</span> p <span style="color:blue;">in</span> myDB.SampleTable
                <span style="color:blue;">where</span> p.SampleColumn == <span style="color:#a31515;">"Test Value"</span>
                <span style="color:blue;">select</span> p);</pre>
<pre style="font-family:consolas;">LINQToDataSet(myDB, oTableResult);</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1340/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1340&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/05/20/converting-system-linq-iqueryable-interface-to-system-data-dataset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing Google Calendar Events using .Net</title>
		<link>http://anyrest.wordpress.com/2011/05/14/adding-a-google-calendar-event-using-net/</link>
		<comments>http://anyrest.wordpress.com/2011/05/14/adding-a-google-calendar-event-using-net/#comments</comments>
		<pubDate>Sat, 14 May 2011 10:22:00 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[Google Calendar]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1326</guid>
		<description><![CDATA[Yesterday I posted an article on how to create events in SalesForce programatically now we will do the same thing for Google Calendar. The processes are nearly the same but it&#8217;s not as straightforward as the SalesForce one specially on Event Creation and Custom fields extensions. One good thing though that Google has is a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1326&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yesterday I posted an article on how to create events in SalesForce programatically now we will do the same thing for Google Calendar. The processes are nearly the same but it&#8217;s not as straightforward as the SalesForce one specially on Event Creation and Custom fields extensions. One good thing though that Google has is a native .<a href="http://code.google.com/p/google-gdata/downloads/list" target="_blank">Net API</a> that you can add to your project so you do not need to reference to the Google Calendar web service and the dll handles its for you.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/google-calendar.jpg"><img class="alignnone size-full wp-image-1327" title="Google Calendar" src="http://anyrest.files.wordpress.com/2011/05/google-calendar.jpg?w=630&#038;h=299" alt="" width="630" height="299" /></a></p>
<p>First you need to download that API and add it in your .Net Project. You need to install the Google_Data_API_Setup_{CurrentVerion}.msi and to refer to it, you need to browse to &#8220;<span style="color:#339966;">C:\Program Files (x86)\Google\Google Data API SDK\Redist</span>&#8221; on 64 bit machines and &#8220;<span style="color:#339966;">C:\Program Files\Google\Google Data API SDK\Redist</span>&#8221; on 32 bit machines by default</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/reference.jpg"><img class="alignnone size-full wp-image-1328" title="Google DLL Reference" src="http://anyrest.files.wordpress.com/2011/05/reference.jpg?w=630&#038;h=354" alt="" width="630" height="354" /></a></p>
<p>You need to reference 4 main items for this scenario and it is the following</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/google-calendar-demo.jpg"><img class="alignnone size-full wp-image-1329" title="Google References" src="http://anyrest.files.wordpress.com/2011/05/google-calendar-demo.jpg?w=630" alt=""   /></a></p>
<p>Now include that reference in your codes</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> Google.GData.Client;
<span style="color:blue;">using</span> Google.GData.Calendar;
<span style="color:blue;">using</span> Google.GData.Extensions;</pre>
<p>Then like what we had done with the <a href="http://anyrest.wordpress.com/2011/05/13/adding-a-salesforce-event-using-net/" target="_blank">SalesForce article</a> we need a method for login and here is the code snippet. Again I provided a proxy setting in case you need</p>
<pre style="font-family:consolas;"><span style="color:blue;">private</span> <span style="color:#2b91af;">CalendarService</span> GAuthenticate()
{
    <span style="color:blue;">string</span> sGoogleUserName = <span style="color:#a31515;">"username"</span>;
    <span style="color:blue;">string</span> sGooglePassword = <span style="color:#a31515;">"password"</span>;
    <span style="color:#2b91af;">Uri</span> oCalendarUri = <span style="color:blue;">new</span> <span style="color:#2b91af;">Uri</span>(<span style="color:#a31515;">"http://www.google.com/calendar/feeds/"</span> + sGoogleUserName + <span style="color:#a31515;">"/private/full"</span>);

    <span style="color:green;">//Initialize Calendar Service</span>
    <span style="color:#2b91af;">CalendarService</span> oCalendarService = <span style="color:blue;">new</span> <span style="color:#2b91af;">CalendarService</span>(<span style="color:#a31515;">"CalendarSampleApp"</span>);
    oCalendarService.setUserCredentials(sGoogleUserName, sGooglePassword);

    <span style="color:green;">//Use Proxy </span>
    <span style="color:#2b91af;">GDataRequestFactory</span> oRequestFactory = (<span style="color:#2b91af;">GDataRequestFactory</span>)oCalendarService.RequestFactory;
    <span style="color:#2b91af;">WebProxy</span> oWebProxy = <span style="color:blue;">new</span> <span style="color:#2b91af;">WebProxy</span>(<span style="color:#2b91af;">WebRequest</span>.DefaultWebProxy.GetProxy(oCalendarUri));
    oWebProxy.Credentials = <span style="color:#2b91af;">CredentialCache</span>.DefaultCredentials;
    oWebProxy.UseDefaultCredentials = <span style="color:blue;">true</span>;
    oRequestFactory.Proxy = oWebProxy;

    <span style="color:blue;">return</span> oCalendarService;
}</pre>
<p>Now lets go to the methods you need, on all of the methods discussed below you will need the GAuthenticate to execute your methods</p>
<h3><span style="color:#3366ff;">1. Creating Calendar Entries in Google programatically</span></h3>
<p>Now if you notice we used 4 different object types unlike in SalesForce where we used only 1 object which is the <strong>SalesForceService.Event</strong>. Here we have to define an <strong>Event, Where, When and ExtendedProperty</strong> if you need one. <strong>ExtendedProperty</strong> is a way of adding properties to the objects programatically , so in this case we created a property called <strong>SynchronizationID</strong> which will hold our Guid that you can use with other apps.<br />
Hint: I use this for my sync application.</p>
<pre style="font-family:consolas;"></pre>
<pre style="font-family:consolas;"><span style="color:green;">//Set Event Entry </span>
<span style="color:#2b91af;">EventEntry</span> oEventEntry = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventEntry</span>();
oEventEntry.Title.Text = <span style="color:#a31515;">"Test Calendar Entry From .Net"</span>;
oEventEntry.Content.Content = <span style="color:#a31515;">"Hurrah!!! I posted my first Google calendar event through .Net"</span>;

<span style="color:green;">//Set Event Location </span>
<span style="color:#2b91af;">Where</span> oEventLocation = <span style="color:blue;">new</span> <span style="color:#2b91af;">Where</span>();
oEventLocation.ValueString = <span style="color:#a31515;">"New Zealand"</span>;
oEventEntry.Locations.Add(oEventLocation);

<span style="color:green;">//Set Event Time</span>
<span style="color:#2b91af;">When</span> oEventTime = <span style="color:blue;">new</span> <span style="color:#2b91af;">When</span>(<span style="color:blue;">new</span> <span style="color:#2b91af;">DateTime</span>(2011, 5, 31, 9, 0, 0), <span style="color:blue;">new</span> <span style="color:#2b91af;">DateTime</span>(2011, 5, 31, 9, 0, 0).AddHours(1));
oEventEntry.Times.Add(oEventTime);

<span style="color:green;">//Set Additional Properties</span>
<span style="color:#2b91af;">ExtendedProperty</span> oExtendedProperty = <span style="color:blue;">new</span> <span style="color:#2b91af;">ExtendedProperty</span>();
oExtendedProperty.Name = <span style="color:#a31515;">"SynchronizationID"</span>;
oExtendedProperty.Value = <span style="color:#2b91af;">Guid</span>.NewGuid().ToString();
oEventEntry.ExtensionElements.Add(oExtendedProperty);

<span style="color:#2b91af;">CalendarService</span> oCalendarService = GAuthenticate();

<span style="color:green;">//Prevents This Error</span>
<span style="color:green;">//{"The remote server returned an error: (417) Expectation failed."}</span>
System.Net.<span style="color:#2b91af;">ServicePointManager</span>.Expect100Continue = <span style="color:blue;">false</span>;

<span style="color:green;">//Save Event</span>
oCalendarService.Insert(oCalendarUri, oEventEntry);</pre>
<p>You might also notice this line &#8220;System.Net.ServicePointManager.Expect100Continue = false&#8221;, that takes care of the HTTP 417 Error which is explained <a href="http://www.finalint.com/2010/04/23/google-calendar-net-api-http-417-error/" target="_blank">here</a>.</p>
<h3><span style="color:#3366ff;">2. Searching Calendar Entries in Google programatically</span></h3>
<p>Searching is also straightforward, just use the <strong>EventQuery</strong> properties to pass to the <strong>CalendarService.Query</strong> method. Set the <strong>EventQuery.Query</strong> to the same query you use when you search for the calendar in Google.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/search.jpg"><img class="alignnone size-full wp-image-1331" title="Search" src="http://anyrest.files.wordpress.com/2011/05/search.jpg?w=630&#038;h=86" alt="" width="630" height="86" /></a></p>
<p>You can also fliter by date start and end by using the StartDate and EndDate properties</p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">CalendarService</span> oCalendarService = GAuthenticate();

<span style="color:green;">//Search for Event</span>
<span style="color:#2b91af;">EventQuery</span> oEventQuery = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventQuery</span>(oCalendarUri.ToString());
oEventQuery.Query = <span style="color:#a31515;">"Query String"</span>;
oEventQuery.StartDate = <span style="color:blue;">new</span> <span style="color:#2b91af;">DateTime</span>(2011, 6, 5);
oEventQuery.EndDate = <span style="color:blue;">new</span> <span style="color:#2b91af;">DateTime</span>(2012, 6, 5);

Google.GData.Calendar.<span style="color:#2b91af;">EventFeed</span> oEventFeed = oCalendarService.Query(oEventQuery);

<span style="color:green;">//Delete Related Events</span>
<span style="color:blue;">foreach</span> (<span style="color:#2b91af;">EventEntry</span> oEventEntry <span style="color:blue;">in</span> oEventFeed.Entries)
{
    <span style="color:green;">//Do your stuff here</span>
    <span style="color:blue;">string</span> sEventTitle = oEventEntry.Title.ToString();
}</pre>
<p>But if you want to search for the <strong>ExtendedProperty</strong> like the <strong>&#8220;SynchronizationID&#8221;</strong> we created on the event creation on step 1 then you need to use the <strong>ExtraParameters</strong> property and use the <strong>extq=[YourCutomProperty:TheValue]</strong> to get what you need.</p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">CalendarService</span> oCalendarService = GAuthenticate();

<span style="color:green;">//Search for Event</span>
<span style="color:#2b91af;">EventQuery</span> oEventQuery = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventQuery</span>(oCalendarUri.ToString());
oEventQuery.ExtraParameters = <span style="color:#a31515;">"extq=[SynchronizationID:Your GUID Here]"</span>;

Google.GData.Calendar.<span style="color:#2b91af;">EventFeed</span> oEventFeed = oCalendarService.Query(oEventQuery);

<span style="color:green;">//Delete Related Events</span>
<span style="color:blue;">foreach</span> (<span style="color:#2b91af;">EventEntry</span> oEventEntry <span style="color:blue;">in</span> oEventFeed.Entries)
{
    <span style="color:green;">//Do your stuff here</span>
    <span style="color:blue;">string</span> sEventTitle = oEventEntry.Title.ToString();
}</pre>
<h3><span style="color:#3366ff;">3. Updating Calendar Entries in Google programatically</span></h3>
<p>Updating is straight forward, just search for the item then use the <strong>CalendarService.Update</strong> method</p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">CalendarService</span> oCalendarService = GAuthenticate();

<span style="color:green;">//Search for Event</span>
<span style="color:#2b91af;">EventQuery</span> oEventQuery = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventQuery</span>(oCalendarUri.ToString());
oEventQuery.ExtraParameters = <span style="color:#a31515;">"extq=[SynchronizationID:{Your GUID Here}]"</span>;

Google.GData.Calendar.<span style="color:#2b91af;">EventFeed</span> oEventFeed = oCalendarService.Query(oEventQuery);

<span style="color:green;">//Delete Related Events</span>
<span style="color:blue;">foreach</span> (<span style="color:#2b91af;">EventEntry</span> oEventEntry <span style="color:blue;">in</span> oEventFeed.Entries)
{
    <span style="color:green;">//Update Event</span>
    oEventEntry.Title.Text = <span style="color:#a31515;">"Updated Entry"</span>;

    oCalendarService.Update(oEventEntry);
    <span style="color:blue;">break</span>;
}</pre>
<h3><span style="color:#3366ff;">4. Deleting Calendar Entries in Google programatically</span></h3>
<p>Deleting is straight forward, as well, just search for the item then use the <strong>CalendarService.Delete</strong> method</p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">CalendarService</span> oCalendarService = GAuthenticate();

<span style="color:green;">//Search for Event</span>
<span style="color:#2b91af;">EventQuery</span> oEventQuery = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventQuery</span>(oCalendarUri.ToString());
oEventQuery.ExtraParameters = <span style="color:#a31515;">"extq=[SynchronizationID:{Your GUID Here}]"</span>;

Google.GData.Calendar.<span style="color:#2b91af;">EventFeed</span> oEventFeed = oCalendarService.Query(oEventQuery);

<span style="color:green;">//Delete Related Events</span>
<span style="color:blue;">foreach</span> (<span style="color:#2b91af;">EventEntry</span> oEventEntry <span style="color:blue;">in</span> oEventFeed.Entries)
{
    oEventEntry.Delete();
    <span style="color:blue;">break</span>;
}</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1326/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1326&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/05/14/adding-a-google-calendar-event-using-net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/google-calendar.jpg" medium="image">
			<media:title type="html">Google Calendar</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/reference.jpg" medium="image">
			<media:title type="html">Google DLL Reference</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/google-calendar-demo.jpg" medium="image">
			<media:title type="html">Google References</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/search.jpg" medium="image">
			<media:title type="html">Search</media:title>
		</media:content>
	</item>
		<item>
		<title>Increase 1000 Records Maximum Page Size of Active Directory</title>
		<link>http://anyrest.wordpress.com/2011/05/14/increase-1000-records-maximum-page-size-of-active-directory/</link>
		<comments>http://anyrest.wordpress.com/2011/05/14/increase-1000-records-maximum-page-size-of-active-directory/#comments</comments>
		<pubDate>Fri, 13 May 2011 14:00:44 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Active Directory]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1310</guid>
		<description><![CDATA[Just wanted to share a common question that is always asked on why Active Directory returns only 1000 results when you perform a query.  It is a limitation imposed by Active Directory by default, but it does not mean you cannot increase that values.  You can always increase it by changing the MaxPageSize setting in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1310&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just wanted to share a common question that is always asked on why Active Directory returns only 1000 results when you perform a query.  It is a limitation imposed by Active Directory by default, but it does not mean you cannot increase that values.  You can always increase it by changing the MaxPageSize setting in Active Directory but do it with proper caution as it set at 1000 for a reason.</p>
<p>Now if you know the implications of changing it like slowing down the performance of your Active Directory Operations then here is how to increse it.</p>
<p>All you need is to use Ntdsutil.exe which you can type on the run command but if you are using Windows 2008 make sure run the command prompt as an administrator</p>
<p>Once you typed it it will show you the command prompt and follow these steps to increase the limit.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/ldap-policy.jpg"><img class="alignnone size-full wp-image-1311" title="LDAP Policy" src="http://anyrest.files.wordpress.com/2011/05/ldap-policy.jpg?w=630" alt=""   /></a></p>
<ol>
<li>At the Ntdsutil.exe command prompt, type &#8220;LDAP policies&#8221;, and then hit ENTER.</li>
<li>Now type &#8220;Set MaxPageSize to 5000&#8243;. -&gt; Change 5000 to the Max results you want</li>
<li>To view the changes, type &#8220;Show Values&#8221;</li>
<li>To commit the changes, type &#8220;Commit Changes&#8221;</li>
<li>type &#8220;q&#8221; to quit</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1310/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1310&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/05/14/increase-1000-records-maximum-page-size-of-active-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/ldap-policy.jpg" medium="image">
			<media:title type="html">LDAP Policy</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing SalesForce Events using .Net</title>
		<link>http://anyrest.wordpress.com/2011/05/13/adding-a-salesforce-event-using-net/</link>
		<comments>http://anyrest.wordpress.com/2011/05/13/adding-a-salesforce-event-using-net/#comments</comments>
		<pubDate>Fri, 13 May 2011 10:00:00 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SalesForce]]></category>
		<category><![CDATA[SalesForce API]]></category>
		<category><![CDATA[SalesForce Event]]></category>
		<category><![CDATA[SOQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1314</guid>
		<description><![CDATA[I started a project lately to synchronize events between Google Calendar and Sales Force Calendar, there may be different products available out there but those are really expensive and some require you to setup an array of files in an end users machine. For the systems administrator it will be a nightmare specially when you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1314&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I started a project lately to synchronize events between Google Calendar and Sales Force Calendar, there may be different products available out there but those are really expensive and some require you to setup an array of files in an end users machine. For the systems administrator it will be a nightmare specially when you have more than 1000 users so we opted out to develop our solution using .Net and created a service on our servers to do the job. The article discussed here does not depict the way we created a solution but a reference on important aspects of the evets that you might be using on your own solution such as saving events to different calendar providers.</p>
<p>In this post will discuss some points on how to save Sales Force Events using C#.Net and suprisingly it is simple specially with the use of a well documented API for SalesForce which you can find <a href="http://www.salesforce.com/us/developer/docs/api/index.htm" target="_blank">here</a>. Now lets get started.</p>
<p>First you need a developer account at SalesForce and you can do that by signing up at <a href="http://www.developerforce.com/events/regular/registration.php" target="_blank">SalesForce</a>, dont worry it is free and easy. Take note that you should sign up as a developer otherwise some items will not be visible to you such as API references. Once you have done that can generate your Enterprise WSDL by going to Name (in my case its &#8220;Test Account&#8221;) -&gt; Setup -&gt; App Setup -&gt; Develop -&gt; API.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/api-wsdl-salesforce.jpg"><img class="alignnone size-full wp-image-1315" title="Sales Force API WSDL" src="http://anyrest.files.wordpress.com/2011/05/api-wsdl-salesforce.jpg?w=630&#038;h=380" alt="" width="630" height="380" /></a></p>
<p>Please take note that you have to do this everytime you change or update the structure of your SalesForce instance like Custom Fields. Once thats Generated, save it in a handly location as a &#8220;wsdl&#8221; file.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/wsdl.jpg"><img class="alignnone size-full wp-image-1316" title="WSDL File" src="http://anyrest.files.wordpress.com/2011/05/wsdl.jpg?w=630&#038;h=363" alt="" width="630" height="363" /></a></p>
<p>Next on the list is you need to generate a Security Token, you need this for connecting to SalesForce in your application. Now go to Name (in my case its &#8220;Test Account&#8221;) -&gt; Setup -&gt; My Personal Information -&gt; Reset Security Question</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/reset-security-token-salesforce.jpg"><img class="alignnone size-full wp-image-1317" title="Reset Security Token" src="http://anyrest.files.wordpress.com/2011/05/reset-security-token-salesforce.jpg?w=630&#038;h=380" alt="" width="630" height="380" /></a></p>
<p>Fire up Visual Studio and create a solution of your choice, add the WSDL to your project then add that WSDL as a Web Reference by right clicking the References folder and add a Service Reference.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/google-calendar-demo-microsoft-visual-studio_2011-05-13_09-49-33.jpg"><img class="alignnone size-full wp-image-1318" title="Adding a reference" src="http://anyrest.files.wordpress.com/2011/05/google-calendar-demo-microsoft-visual-studio_2011-05-13_09-49-33.jpg?w=630" alt=""   /></a></p>
<p>You need a web reference so you need to hit Advanced</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/add-service-reference_2011-05-13_09-50-05.jpg"><img class="alignnone size-full wp-image-1319" title="Adding a Reference" src="http://anyrest.files.wordpress.com/2011/05/add-service-reference_2011-05-13_09-50-05.jpg?w=630&#038;h=509" alt="" width="630" height="509" /></a></p>
<p>Then Add Web reference</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/service-reference-settings_2011-05-13_09-50-27.jpg"><img class="alignnone size-full wp-image-1320" title="Adding a reference" src="http://anyrest.files.wordpress.com/2011/05/service-reference-settings_2011-05-13_09-50-27.jpg?w=630&#038;h=585" alt="" width="630" height="585" /></a></p>
<p>Now point to your file the hit Add reference. At this point you have created your Web Reference. I named my instance &#8220;SalesForceService&#8221;. Now at this point you got everything you need to rock and roll.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/add-web-reference_2011-05-13_09-51-30.jpg"><img class="alignnone size-full wp-image-1321" title="Sforce Service Reference" src="http://anyrest.files.wordpress.com/2011/05/add-web-reference_2011-05-13_09-51-30.jpg?w=630&#038;h=438" alt="" width="630" height="438" /></a></p>
<p>To be able to access your SalesForce instance you need to authenticate by using this code. This is where you use your token that you generated earlier. You can also see that there is a proxy authentication included in case you needed it.</p>
<pre style="font-family:consolas;"><span style="color:blue;">private</span> <span style="color:#2b91af;">SforceService</span> SFAuthenticate()
{
    <span style="color:#2b91af;">SforceService</span> oSalesForceService = <span style="color:blue;">new</span> <span style="color:#2b91af;">SforceService</span>();
    oSalesForceService.Timeout = 60000;

    <span style="color:green;">// Set Proxy Details if you are using one</span>
    <span style="color:#2b91af;">WebProxy</span> oWebProxy = <span style="color:blue;">new</span> <span style="color:#2b91af;">WebProxy</span>(<span style="color:#2b91af;">WebRequest</span>.DefaultWebProxy.GetProxy(<span style="color:blue;">new</span> <span style="color:#2b91af;">Uri</span>(oSalesForceService.Url.ToString())));
    oWebProxy.Credentials = <span style="color:#2b91af;">CredentialCache</span>.DefaultCredentials;
    oWebProxy.UseDefaultCredentials = <span style="color:blue;">true</span>;
    oSalesForceService.Proxy = oWebProxy;

    <span style="color:green;">//Initialize SalesForce Service</span>
    <span style="color:blue;">string</span> username = <span style="color:#a31515;">"you@company.com"</span>;
    <span style="color:blue;">string</span> password = <span style="color:blue;">string</span>.Concat(<span style="color:#a31515;">"password"</span>, <span style="color:#a31515;">"token"</span>);

    <span style="color:#2b91af;">LoginResult</span> oLoginResult = oSalesForceService.login(username, password);

    oSalesForceService.Url = oLoginResult.serverUrl;
    oSalesForceService.SessionHeaderValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">SessionHeader</span>();
    oSalesForceService.SessionHeaderValue.sessionId = oLoginResult.sessionId;
    <span style="color:#2b91af;">GetUserInfoResult</span> oUserInfo = oLoginResult.userInfo;

    <span style="color:blue;">return</span> oSalesForceService;
}</pre>
<p>Now lets go to the methods you need, on all of the methods discussed below you will need the SFAuthenticate to execute your methods</p>
<h3><span style="color:#3366ff;">1. Creating Events to SalesForce programatically</span></h3>
<p>This is a stratight-forward approch as each <strong>Event</strong> properties are exposed though the <strong>Event</strong> Class, also one of the custom fields I created called UniquKey was also exposed by the generated WSDL file. Once the event is created just pass it as a parameter on the SforceService.create method, also if you notice it accepts an array of events so you can pass multiple events in one call.</p>
<p>Hint: this is what I used for synchronizing events on different calendar providers.</p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">SforceService</span> oSalesForceService = SFAuthenticate();
SalesForceService.<span style="color:#2b91af;">Event</span> oCalendarEvent = <span style="color:blue;">new</span> SalesForceService.<span style="color:#2b91af;">Event</span>();

oCalendarEvent.Subject = <span style="color:#a31515;">"Test SF Calendar Entry From .Net"</span>;
oCalendarEvent.Description = <span style="color:#a31515;">"Hurrah! I posted my first sales force calendar event through .Net"</span>;
oCalendarEvent.Location = <span style="color:#a31515;">"New Zealand"</span>;
oCalendarEvent.StartDateTimeSpecified = <span style="color:blue;">true</span>;
oCalendarEvent.StartDateTime = <span style="color:blue;">new</span> <span style="color:#2b91af;">DateTime</span>(2011, 5, 31, 9, 0, 0);
oCalendarEvent.EndDateTimeSpecified = <span style="color:blue;">true</span>;
oCalendarEvent.EndDateTime = <span style="color:blue;">new</span> <span style="color:#2b91af;">DateTime</span>(2011, 5, 31, 9, 0, 0).AddHours(1);

oCalendarEvent.UniqueKey__c = <span style="color:#2b91af;">Guid</span>.NewGuid().ToString();

<span style="color:#2b91af;">SaveResult</span>[] oResults = oSalesForceService.create(<span style="color:blue;">new</span> <span style="color:#2b91af;">Event</span>[] { oCalendarEvent });

oSalesForceService.logout();</pre>
<p><a href="http://anyrest.files.wordpress.com/2011/05/calendar-event-test-sf-calendar-entry-from-net-salesforce.jpg"><img class="alignnone size-full wp-image-1322" title="Calendar Event Test SalesForce" src="http://anyrest.files.wordpress.com/2011/05/calendar-event-test-sf-calendar-entry-from-net-salesforce.jpg?w=630&#038;h=345" alt="" width="630" height="345" /></a></p>
<h3><span style="color:#3366ff;">2. Searching Events in SaleForce programatically</span></h3>
<p>Searching is also easy all you need is to create a <a href="http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql.htm" target="_blank">SOQL Query</a> (its not mispelled, its really SOQL and it is the Salesforce Object Query Language similar to SQL) to get the fields you need. Take note as well that you cannot use * (select all fields), this makes sure that your codes are always optimized for what you only need. The result of that query is then outputted as a <strong>QueryResult</strong> and you can parse it as an <strong>Event</strong>. Only the fields you selected are the only fields that are populated on the <strong>Event</strong> object. To get the list of what are the available fields for the <strong>Event</strong> Object, check the <strong>Event</strong> properties.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/05/event.jpg"><img class="alignnone size-full wp-image-1323" style="border:1px solid black;" title="Event Properties" src="http://anyrest.files.wordpress.com/2011/05/event.jpg?w=630" alt=""   /></a></p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">SforceService</span> oSalesForceService = SFAuthenticate();

<span style="color:#2b91af;">QueryResult</span> oQueryResult = <span style="color:blue;">null</span>;
oSalesForceService.QueryOptionsValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">QueryOptions</span>();

oQueryResult = oSalesForceService.query(<span style="color:#a31515;">"SELECT Id, Subject, Description, Location, StartDateTime, EndDateTime FROM Event WHERE UniqueKey__c = '{Your GUID Here}'"</span>);
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i &lt; oQueryResult.size; i++)
{
    <span style="color:#2b91af;">Event</span> oCalendarEvent = oQueryResult.records[i] <span style="color:blue;">as</span> <span style="color:#2b91af;">Event</span>;
}

oSalesForceService.logout();</pre>
<h3><span style="color:#3366ff;">3. Updating Events in SalesForce programatically</span></h3>
<p>For updating you just Set the <strong>Event</strong> properties to the values you want and pass it as a parameter on the <strong>SforceService.update</strong> method</p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">SforceService</span> oSalesForceService = SFAuthenticate();

<span style="color:#2b91af;">QueryResult</span> oQueryResult = <span style="color:blue;">null</span>;
oSalesForceService.QueryOptionsValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">QueryOptions</span>();

oQueryResult = oSalesForceService.query(<span style="color:#a31515;">"SELECT Id FROM Event WHERE UniqueKey__c = '{Your GUID Here}'"</span>);
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i &lt; oQueryResult.size; i++)
{
    <span style="color:#2b91af;">Event</span> oCalendarEvent = oQueryResult.records[i] <span style="color:blue;">as</span> <span style="color:#2b91af;">Event</span>;
    oCalendarEvent.Description = <span style="color:#a31515;">"Updated Sales Force Event from .Net"</span>;
    <span style="color:#2b91af;">SaveResult</span>[] oSaveResults = oSalesForceService.update(<span style="color:blue;">new</span> <span style="color:#2b91af;">Event</span>[] { oCalendarEvent });
}

oSalesForceService.logout();</pre>
<h3><span style="color:#3366ff;">4. Deleting events in SalesForce programatically</span></h3>
<p>For deleting you pass the Event as the parameter on the <strong>SforceService.delete method</strong></p>
<pre style="font-family:consolas;"><span style="color:#2b91af;">SforceService</span> oSalesForceService = SFAuthenticate();

<span style="color:#2b91af;">QueryResult</span> oQueryResult = <span style="color:blue;">null</span>;
oSalesForceService.QueryOptionsValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">QueryOptions</span>();

oQueryResult = oSalesForceService.query(<span style="color:#a31515;">"select Id from Event where UniqueKey__c = '{Your GUID Here}'"</span>);
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i &lt; oQueryResult.size; i++)
{
    <span style="color:#2b91af;">Event</span> oCalendarEvent = oQueryResult.records[i] <span style="color:blue;">as</span> <span style="color:#2b91af;">Event</span>;

    <span style="color:#2b91af;">DeleteResult</span>[] oSaveResults = oSalesForceService.delete(<span style="color:blue;">new</span> <span style="color:blue;">string</span>[] { oCalendarEvent.Id.ToString() });
}

oSalesForceService.logout();</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1314/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1314&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/05/13/adding-a-salesforce-event-using-net/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/api-wsdl-salesforce.jpg" medium="image">
			<media:title type="html">Sales Force API WSDL</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/wsdl.jpg" medium="image">
			<media:title type="html">WSDL File</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/reset-security-token-salesforce.jpg" medium="image">
			<media:title type="html">Reset Security Token</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/google-calendar-demo-microsoft-visual-studio_2011-05-13_09-49-33.jpg" medium="image">
			<media:title type="html">Adding a reference</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/add-service-reference_2011-05-13_09-50-05.jpg" medium="image">
			<media:title type="html">Adding a Reference</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/service-reference-settings_2011-05-13_09-50-27.jpg" medium="image">
			<media:title type="html">Adding a reference</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/add-web-reference_2011-05-13_09-51-30.jpg" medium="image">
			<media:title type="html">Sforce Service Reference</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/calendar-event-test-sf-calendar-entry-from-net-salesforce.jpg" medium="image">
			<media:title type="html">Calendar Event Test SalesForce</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/05/event.jpg" medium="image">
			<media:title type="html">Event Properties</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Visual Web Part Properties</title>
		<link>http://anyrest.wordpress.com/2011/04/27/creating-visual-web-part-properties/</link>
		<comments>http://anyrest.wordpress.com/2011/04/27/creating-visual-web-part-properties/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 10:00:43 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>
		<category><![CDATA[Web Part]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1301</guid>
		<description><![CDATA[Before reading this article I assume you already know how to create your own web parts in Sharepoint and is looking for some ways to create a property for that webpart that you can easily Set and Get from your application like this one on the image below. but if not please read this post [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1301&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Before reading this article I assume you already know how to create your own web parts in Sharepoint and is looking for some ways to create a property for that webpart that you can easily Set and Get from your application like this one on the image below.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/property-sample.jpg"><img class="alignnone size-full wp-image-1302" title="Property Sample" src="http://anyrest.files.wordpress.com/2011/04/property-sample.jpg?w=630" alt=""   /></a></p>
<p>but if not please read this post regarding <a href="http://anyrest.wordpress.com/2010/12/15/developing-web-parts-for-sharepoint-2010/">Developing Web Parts</a> first to avoid confusion.  But if you already know what to do then continue on.</p>
<p>First on your Web Part Class (yellow highlight) and not the User Control Class (green highlight) define your properties</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/elements.jpg"><img class="alignnone size-full wp-image-1303" title="Elements" src="http://anyrest.files.wordpress.com/2011/04/elements.jpg?w=630" alt=""   /></a></p>
<p>For this example I will show you how to define a text, boolean, integer, date time and enummeration property</p>
<pre style="font-family:consolas;">[<span style="color:#2b91af;">ToolboxItemAttribute</span>(<span style="color:blue;">false</span>)]
<span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Sample_Web_Part</span> : <span style="color:#2b91af;">WebPart</span>
{
    <span style="color:green;">// Visual Studio might automatically update this path when you change the Visual Web Part project item.</span>
    <span style="color:blue;">private</span> <span style="color:blue;">const</span> <span style="color:blue;">string</span> _ascxPath = <span style="color:#a31515;">@"~/_CONTROLTEMPLATES/Sample_Project/Sample Web Part/Sample Web PartUserControl.ascx"</span>;

    <span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> CreateChildControls()
    {
        <span style="color:#2b91af;">Control</span> control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }

    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">Boolean</span> SampleBoolean;
    [<span style="color:#2b91af;">Category</span>(<span style="color:#a31515;">"Extended Settings"</span>),
    <span style="color:#2b91af;">Personalizable</span>(<span style="color:#2b91af;">PersonalizationScope</span>.Shared),
    <span style="color:#2b91af;">WebBrowsable</span>(<span style="color:blue;">true</span>),
    <span style="color:#2b91af;">WebDisplayName</span>(<span style="color:#a31515;">"Sample Boolean"</span>),
    <span style="color:#2b91af;">WebDescription</span>(<span style="color:#a31515;">"Please Choose a Sample Boolean"</span>)]
    <span style="color:blue;">public</span> <span style="color:#2b91af;">Boolean</span> _SampleBoolean
    {
        <span style="color:blue;">get</span> { <span style="color:blue;">return</span> SampleBoolean; }
        <span style="color:blue;">set</span> { SampleBoolean = <span style="color:blue;">value</span>; }
    }

    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span> SampleText;
    [<span style="color:#2b91af;">Category</span>(<span style="color:#a31515;">"Extended Settings"</span>),
    <span style="color:#2b91af;">Personalizable</span>(<span style="color:#2b91af;">PersonalizationScope</span>.Shared),
    <span style="color:#2b91af;">WebBrowsable</span>(<span style="color:blue;">true</span>),
    <span style="color:#2b91af;">WebDisplayName</span>(<span style="color:#a31515;">"Sample Text"</span>),
    <span style="color:#2b91af;">WebDescription</span>(<span style="color:#a31515;">"Please Enter a Sample Text"</span>)]
    <span style="color:blue;">public</span> <span style="color:blue;">string</span> _SampleText
    {
        <span style="color:blue;">get</span> { <span style="color:blue;">return</span> SampleText; }
        <span style="color:blue;">set</span>
        {
            <span style="color:green;">// Sample Validation</span>
            <span style="color:#2b91af;">Regex</span> oRegEx = <span style="color:blue;">new</span> <span style="color:#2b91af;">Regex</span>(<span style="color:#a31515;">"[a-zA-Z]+"</span>);
            <span style="color:blue;">if</span> (!oRegEx.IsMatch(<span style="color:blue;">value</span>))
                <span style="color:blue;">throw</span> <span style="color:blue;">new</span> Microsoft.SharePoint.WebPartPages.<span style="color:#2b91af;">WebPartPageUserException</span>(<span style="color:#a31515;">"Please enter alphabeth characters only"</span>);
            SampleText = <span style="color:blue;">value</span>;
        }
    }

    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">int</span> SampleNumber;
    [<span style="color:#2b91af;">Category</span>(<span style="color:#a31515;">"Extended Settings"</span>),
    <span style="color:#2b91af;">Personalizable</span>(<span style="color:#2b91af;">PersonalizationScope</span>.Shared),
    <span style="color:#2b91af;">WebBrowsable</span>(<span style="color:blue;">true</span>),
    <span style="color:#2b91af;">WebDisplayName</span>(<span style="color:#a31515;">"Sample Number"</span>),
    <span style="color:#2b91af;">WebDescription</span>(<span style="color:#a31515;">"Please Enter a Sample Number"</span>)]
    <span style="color:blue;">public</span> <span style="color:blue;">int</span> _SampleNumber
    {
        <span style="color:blue;">get</span> { <span style="color:blue;">return</span> SampleNumber; }
        <span style="color:blue;">set</span> { SampleNumber = <span style="color:blue;">value</span>; }
    }

    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">DateTime</span> SampleDate;
    [<span style="color:#2b91af;">Category</span>(<span style="color:#a31515;">"Extended Settings"</span>),
    <span style="color:#2b91af;">Personalizable</span>(<span style="color:#2b91af;">PersonalizationScope</span>.Shared),
    <span style="color:#2b91af;">WebBrowsable</span>(<span style="color:blue;">true</span>),
    <span style="color:#2b91af;">WebDisplayName</span>(<span style="color:#a31515;">"Sample Date"</span>),
    <span style="color:#2b91af;">WebDescription</span>(<span style="color:#a31515;">"Please Enter a Sample Date"</span>)]
    <span style="color:blue;">public</span> <span style="color:#2b91af;">DateTime</span> _SampleDate
    {
        <span style="color:blue;">get</span> { <span style="color:blue;">return</span> SampleDate; }
        <span style="color:blue;">set</span> { SampleDate = <span style="color:blue;">value</span>; }
    }

    <span style="color:blue;">public</span> <span style="color:blue;">enum</span> <span style="color:#2b91af;">CityEnum</span> { Manila, Berlin, Auckland, Zurich };
    <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">CityEnum</span> SampleDropDown;
    [<span style="color:#2b91af;">Category</span>(<span style="color:#a31515;">"Extended Settings"</span>),
    <span style="color:#2b91af;">Personalizable</span>(<span style="color:#2b91af;">PersonalizationScope</span>.Shared),
    <span style="color:#2b91af;">WebBrowsable</span>(<span style="color:blue;">true</span>),
    <span style="color:#2b91af;">WebDisplayName</span>(<span style="color:#a31515;">"Sample Drop Down"</span>),
    <span style="color:#2b91af;">WebDescription</span>(<span style="color:#a31515;">"Please Choose a Sample DropDown"</span>)]
    <span style="color:blue;">public</span> <span style="color:#2b91af;">CityEnum</span> _SampleDropDown
    {
        <span style="color:blue;">get</span> { <span style="color:blue;">return</span> SampleDropDown; }
        <span style="color:blue;">set</span> { SampleDropDown = <span style="color:blue;">value</span>; }
    }
}</pre>
<p>If you noticed each property you created have attributes and here is what it means</p>
<ul>
<li><span style="color:#3366ff;"><strong>Category</strong></span> &#8211; This will group your property according to category, If not declared &#8220;Miscellaneous&#8221; will be used as a default.</li>
<li><span style="color:#3366ff;"><strong>Personalizable</strong></span> – How the web part is configured which can be per-user (PersonalizationScope.User) basis or for all everyone (PersonalizationScope.Shared). For this example, we have chosen all users.</li>
<li><span style="color:#3366ff;"><strong>WebBrowsable</strong></span> – This will hide or show the property on the tool pane.</li>
<li><span style="color:#3366ff;"><strong>WebDisplayName</strong></span> – Label for the property.</li>
<li><span style="color:#3366ff;"><strong>WebDescription</strong></span> – Description for the property.</li>
</ul>
<p>You might also notice that we have validation on the codes.  You can add your own like the one on the <strong>SampleText</strong> property where we implemented a Regular Expression or even in <strong>SampleNumber</strong> it is by default validated by its type so you cannot save once it has an illegal values</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/validation.jpg"><img class="alignnone size-full wp-image-1304" title="Validation" src="http://anyrest.files.wordpress.com/2011/04/validation.jpg?w=630" alt=""   /></a></p>
<p>So the final step is conusming that property in your WebPart, you can do it easily by doing like such.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/referencing.jpg"><img class="alignnone size-full wp-image-1305" title="Referencing" src="http://anyrest.files.wordpress.com/2011/04/referencing.jpg?w=630" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1301/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1301&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/04/27/creating-visual-web-part-properties/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/property-sample.jpg" medium="image">
			<media:title type="html">Property Sample</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/elements.jpg" medium="image">
			<media:title type="html">Elements</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/validation.jpg" medium="image">
			<media:title type="html">Validation</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/referencing.jpg" medium="image">
			<media:title type="html">Referencing</media:title>
		</media:content>
	</item>
		<item>
		<title>Sending messages to workstations using Socket Programming and WCF</title>
		<link>http://anyrest.wordpress.com/2011/04/03/sending-messages-to-workstations-using-socket-programming-and-wcf/</link>
		<comments>http://anyrest.wordpress.com/2011/04/03/sending-messages-to-workstations-using-socket-programming-and-wcf/#comments</comments>
		<pubDate>Sun, 03 Apr 2011 06:58:21 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Socket Programming]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Windows Communication Foundation]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1295</guid>
		<description><![CDATA[I am in the process of developing an application which sends information to client machines from another application (some sort of an Internal Messaging System or Desktop Alerts), I initially I had thought of using the old school NETSEND command but there are issues with it, and the main hindrance for me not using it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1295&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am in the process of developing an application which sends information to client machines from another application (some sort of an Internal Messaging System or Desktop Alerts), I initially I had thought of using the old school NETSEND command but there are issues with it, and the main hindrance for me not using it is that it is not in the newer versions of Windows and you cannot really control how it looks and feels so I ended up developing my own.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/sample.jpg"><img class="alignnone size-full wp-image-1299" title="Sample" src="http://anyrest.files.wordpress.com/2011/04/sample.jpg?w=630&#038;h=327" alt="" width="630" height="327" /></a></p>
<p>There are a lot of options before I started like Message Queuing, Remoting and others but I ended up with two methods of delivering my message.  This post will show you  the basics on how to do it in those two methods.</p>
<h2><span style="color:#3366ff;">Socket Programming</span></h2>
<p>First we go to Socket Programming, with this method you will be requiring a lot of work and testing as this is the most Raw as you can get.  Having said that this will be a lot faster as you develop things that you only need, so using this method you will have to create your own protocol, serialization (if you need), security features, etc.</p>
<p>Now for our sample we need only 2 component the Receiver and Sender, we will be developing it using Windows forms and here is how it goes.</p>
<p><span style="color:#3366ff;"><strong>Receiver Form</strong></span> -  This will be responsible for receiving messages from the sender, so this should always run on the background an listens in full-time so any messages sent are received instantaneously</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Windows.Forms;
<span style="color:blue;">using</span> System.Net;
<span style="color:blue;">using</span> System.Net.Sockets;
<span style="color:blue;">namespace</span> Receiver
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Form1</span> : <span style="color:#2b91af;">Form</span>
    {
        <span style="color:blue;">public</span> <span style="color:#2b91af;">AsyncCallback</span> oWorkerCallBack;
        <span style="color:blue;">public</span> <span style="color:#2b91af;">Socket</span> oSocketListener;
        <span style="color:blue;">public</span> <span style="color:#2b91af;">Socket</span> oSocketWorker;
        <span style="color:blue;">public</span> <span style="color:blue;">int</span> ListeningPort = 8000;

        <span style="color:blue;">public</span> Form1()
        {
            InitializeComponent();
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> Form1_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            Listen();
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> Listen()
        {
            <span style="color:blue;">try</span>
            {
                <span style="color:green;">//Declare Socket Listener</span>
                oSocketListener = <span style="color:blue;">new</span> <span style="color:#2b91af;">Socket</span>(<span style="color:#2b91af;">AddressFamily</span>.InterNetwork, <span style="color:#2b91af;">SocketType</span>.Stream, <span style="color:#2b91af;">ProtocolType</span>.Tcp);
                <span style="color:#2b91af;">IPEndPoint</span> oLocalIP = <span style="color:blue;">new</span> <span style="color:#2b91af;">IPEndPoint</span>(<span style="color:#2b91af;">IPAddress</span>.Any, ListeningPort);

                <span style="color:green;">//Local IP Address Binding</span>
                oSocketListener.Bind(oLocalIP);

                <span style="color:green;">//Start Listening</span>
                oSocketListener.Listen(10);

                <span style="color:green;">//Create Callback for Client Connections</span>
                oSocketListener.BeginAccept(<span style="color:blue;">new</span> <span style="color:#2b91af;">AsyncCallback</span>(OnConnect), <span style="color:blue;">null</span>);
                lblApplicationMessage.Text = <span style="color:#a31515;">"Listening on Port : "</span> + ListeningPort;
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">SocketException</span> ex)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(ex.Message);
            }
        }
        <span style="color:blue;">public</span> <span style="color:blue;">void</span> OnConnect(<span style="color:#2b91af;">IAsyncResult</span> oAsyncResult)
        {
            <span style="color:blue;">try</span>
            {
                oSocketWorker = oSocketListener.EndAccept(oAsyncResult);
                WaitForData(oSocketWorker);
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">ObjectDisposedException</span>)
            {
                System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Log(0, <span style="color:#a31515;">"1"</span>, <span style="color:#a31515;">"\n OnConnect: Socket has been closed\n"</span>);
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">SocketException</span> ex)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(ex.Message);
            }
        }
        <span style="color:blue;">public</span> <span style="color:blue;">void</span> WaitForData(System.Net.Sockets.<span style="color:#2b91af;">Socket</span> oSocket)
        {
            <span style="color:blue;">try</span>
            {
                <span style="color:blue;">if</span> (oWorkerCallBack == <span style="color:blue;">null</span>)
                    oWorkerCallBack = <span style="color:blue;">new</span> <span style="color:#2b91af;">AsyncCallback</span>(OnReceive);

                <span style="color:#2b91af;">SocketPacket</span> oSocketPacket = <span style="color:blue;">new</span> <span style="color:#2b91af;">SocketPacket</span>();
                oSocketPacket.oSocket = oSocket;

                <span style="color:green;">// Start Listening for any Data</span>
                oSocket.BeginReceive(oSocketPacket.bDataBuffer, 0, oSocketPacket.bDataBuffer.Length, <span style="color:#2b91af;">SocketFlags</span>.None, oWorkerCallBack, oSocketPacket);
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">SocketException</span> ex)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(ex.Message);
            }
        }
        <span style="color:blue;">public</span> <span style="color:blue;">void</span> OnReceive(<span style="color:#2b91af;">IAsyncResult</span> oAsyncResult)
        {
            <span style="color:blue;">try</span>
            {
                <span style="color:#2b91af;">SocketPacket</span> oSocketID = (<span style="color:#2b91af;">SocketPacket</span>)oAsyncResult.AsyncState;

                <span style="color:green;">//Stop Recieve</span>
                <span style="color:blue;">int</span> iRecieve = 0;
                iRecieve = oSocketID.oSocket.EndReceive(oAsyncResult);

                <span style="color:green;">//Build the message</span>
                <span style="color:blue;">char</span>[] chars = <span style="color:blue;">new</span> <span style="color:blue;">char</span>[iRecieve + 1];
                System.Text.<span style="color:#2b91af;">Decoder</span> d = System.Text.<span style="color:#2b91af;">Encoding</span>.UTF8.GetDecoder();
                <span style="color:blue;">int</span> iCharLen = d.GetChars(oSocketID.bDataBuffer, 0, iRecieve, chars, 0);
                <span style="color:blue;">string</span> sData = <span style="color:blue;">new</span> <span style="color:blue;">string</span>(chars);
                txtMessageRecieved.Text = txtMessageRecieved.Text + sData;

                <span style="color:green;">//Wait again</span>
                WaitForData(oSocketWorker);
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">ObjectDisposedException</span>)
            {
                System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Log(0, <span style="color:#a31515;">"1"</span>, <span style="color:#a31515;">"\n OnReceive: Socket has been closed\n"</span>);
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">SocketException</span> ex)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(ex.Message);
            }
        }
        <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">SocketPacket</span>
        {
            <span style="color:blue;">public</span> System.Net.Sockets.<span style="color:#2b91af;">Socket</span> oSocket;
            <span style="color:blue;">public</span> <span style="color:blue;">byte</span>[] bDataBuffer = <span style="color:blue;">new</span> <span style="color:blue;">byte</span>[1];
        }
    }
}</pre>
<p><span style="color:#3366ff;"><strong>Sender Form</strong></span> &#8211; This will be the one sending the messages to the receiver to this is only used when needed.</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Windows.Forms;
<span style="color:blue;">using</span> System.Net.Sockets;

<span style="color:blue;">namespace</span> Sender
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Form1</span> : <span style="color:#2b91af;">Form</span>
    {
        <span style="color:#2b91af;">Socket</span> oSocket = <span style="color:blue;">null</span>;
        <span style="color:blue;">public</span> <span style="color:blue;">string</span> sRecieverIP = <span style="color:#a31515;">"10.10.10.10"</span>;
        <span style="color:blue;">public</span> <span style="color:blue;">int</span> iRecieverPort = 8000;
        <span style="color:blue;">public</span> Form1()
        {
            InitializeComponent();
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> Form1_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            Connect();
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> btnSend_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            <span style="color:blue;">try</span>
            {
                <span style="color:#2b91af;">Object</span> oData = txtMessage.Text;
                <span style="color:blue;">byte</span>[] bData = System.Text.<span style="color:#2b91af;">Encoding</span>.ASCII.GetBytes(oData.ToString());
                oSocket.Send(bData);
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">SocketException</span> se)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(se.Message);
            }
        }
        <span style="color:blue;">private</span> <span style="color:#2b91af;">Socket</span> Connect()
        {
            <span style="color:blue;">try</span>
            {
                <span style="color:green;">//Creating a Client Socket</span>
                oSocket = <span style="color:blue;">new</span> <span style="color:#2b91af;">Socket</span>(<span style="color:#2b91af;">AddressFamily</span>.InterNetwork, <span style="color:#2b91af;">SocketType</span>.Stream, <span style="color:#2b91af;">ProtocolType</span>.Tcp);

                System.Net.<span style="color:#2b91af;">IPAddress</span> oIPAddress = System.Net.<span style="color:#2b91af;">IPAddress</span>.Parse(sRecieverIP);
                System.Net.<span style="color:#2b91af;">IPEndPoint</span> oEndPoint = <span style="color:blue;">new</span> System.Net.<span style="color:#2b91af;">IPEndPoint</span>(oIPAddress, iRecieverPort);
                oSocket.Connect(oEndPoint);

                <span style="color:blue;">return</span> oSocket;
            }
            <span style="color:blue;">catch</span> (<span style="color:#2b91af;">SocketException</span> se)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(se.Message);
                <span style="color:blue;">return</span> <span style="color:blue;">null</span>;
            }
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> Disconnect(<span style="color:#2b91af;">Socket</span> oSocket)
        {
            <span style="color:green;">//Close socket on both sender and receiver</span>
            oSocket.Shutdown(<span style="color:#2b91af;">SocketShutdown</span>.Both);
            oSocket.Close();
        }
    }
}
</pre>
<h2><span style="color:#3366ff;">WCF or Windows Communication Foundation</span></h2>
<p>Now let&#8217;s go to WCF or Windows Communication Foundation, it is an application programming interface in the .NET Framework for building connected, service-oriented applications like the one we want to achieve.  We also need to know that the base of WCF is also Socket programming that&#8217;s why if you use this approach most of the work is done for you and you will see how easy it is to implement from the codes below.  With WCF you get plumbing, serialization, protocols, security features, etc and all you need is to choose<br />
the features you need and configure.  Now with that overhead this will be slower than the first method.</p>
<p>We will be creating the same 2 forms in this method but we need an additional one to implement a service and we also need an Application configuration to define the endpoint configuration.</p>
<p><span style="color:#3366ff;"><strong>WCF Main Receiver Form</strong></span> &#8211; This is used for service implementation, I used windows form for ease of use in debugging but you can implement it as a service, console or any way you want the invoke the receiver form.</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Windows.Forms;
<span style="color:blue;">using</span> System.ServiceModel;
<span style="color:blue;">using</span> System.ServiceProcess;
<span style="color:blue;">namespace</span> WCFReceiver
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Form1</span> : <span style="color:#2b91af;">Form</span>
    {
        <span style="color:blue;">public</span> <span style="color:#2b91af;">ServiceHost</span> oServiceHost = <span style="color:blue;">null</span>;

        <span style="color:blue;">public</span> Form1()
        {
            InitializeComponent();
        }

        <span style="color:blue;">private</span> <span style="color:blue;">void</span> Form1_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            <span style="color:blue;">if</span> (oServiceHost != <span style="color:blue;">null</span>)
            {
                oServiceHost.Close();
            }
            <span style="color:green;">// Create a ServiceHost for the Service type and provide the base address.</span>
            oServiceHost = <span style="color:blue;">new</span> <span style="color:#2b91af;">ServiceHost</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">NotificationService</span>));

            <span style="color:green;">// Open the ServiceHostBase to create listeners and start listening for messages.</span>
            oServiceHost.Open();
        }

    }

    <span style="color:green;">// Define a Service Contract.</span>
    [<span style="color:#2b91af;">ServiceContract</span>(Namespace = <span style="color:#a31515;">"http://WCFReceiver"</span>)]
    <span style="color:blue;">public</span> <span style="color:blue;">interface</span> <span style="color:#2b91af;">INotifyClient</span>
    {
        [<span style="color:#2b91af;">OperationContract</span>]
        <span style="color:blue;">bool</span> SetPopUp(<span style="color:blue;">string</span> sMessage);
    }

    <span style="color:green;">// Implement the Service Contract in a Service Class.</span>
    <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">NotificationService</span> : <span style="color:#2b91af;">INotifyClient</span>
    {
        <span style="color:blue;">public</span> <span style="color:blue;">bool</span> SetPopUp(<span style="color:blue;">string</span> sMessage)
        {
            <span style="color:blue;">try</span>
            {
                <span style="color:#2b91af;">FormMsgReciever</span> oPopup = <span style="color:blue;">new</span> <span style="color:#2b91af;">FormMsgReciever</span>(sMessage);
                oPopup.Show();
                <span style="color:blue;">return</span> <span style="color:blue;">true</span>;
            }
            <span style="color:blue;">catch</span>
            {
                <span style="color:blue;">return</span> <span style="color:blue;">false</span>;
            }
        }
    }
}</pre>
<p><span style="color:#3366ff;"><strong>Pop Up Form </strong></span>- This is the receiver form to show the messages coming from the sender.</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Windows.Forms;

<span style="color:blue;">namespace</span> WCFReceiver
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">FormMsgReciever</span> : <span style="color:#2b91af;">Form</span>
    {
        <span style="color:blue;">private</span> <span style="color:blue;">string</span> sRecievedMessage;
        <span style="color:blue;">public</span> FormMsgReciever(<span style="color:blue;">string</span> sMessage)
        {
            InitializeComponent();
            sRecievedMessage = sMessage;
        }

        <span style="color:blue;">private</span> <span style="color:blue;">void</span> FormMsgReciever_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            txtMessageRecieved.Text = sRecievedMessage;
        }
    }
}</pre>
<p><span style="color:#3366ff;"><strong>Application Config</strong></span> -  This is the configuration file to define endpoints</p>
<pre style="font-family:consolas;"><span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>"<span style="color:blue;">1.0</span>"<span style="color:blue;"> </span><span style="color:red;">encoding</span><span style="color:blue;">=</span>"<span style="color:blue;">utf-8</span>"<span style="color:blue;"> ?&gt;</span>
<span style="color:blue;">&lt;</span><span style="color:#a31515;">configuration</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">system.serviceModel</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">    &lt;</span><span style="color:#a31515;">services</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">      &lt;!--</span><span style="color:green;"> This section is optional with the new configuration model introduced in .NET Framework 4. </span><span style="color:blue;">--&gt;</span>
<span style="color:blue;">      &lt;</span><span style="color:#a31515;">service</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">WCFReceiver.NotificationService</span>"<span style="color:blue;"> </span><span style="color:red;">behaviorConfiguration</span><span style="color:blue;">=</span>"<span style="color:blue;">NotificationServiceBehavior</span>"<span style="color:blue;">&gt;</span>
<span style="color:blue;">        &lt;</span><span style="color:#a31515;">host</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">          &lt;</span><span style="color:#a31515;">baseAddresses</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">            &lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">baseAddress</span><span style="color:blue;">=</span>"<span style="color:blue;">http://localhost:8005/Notifier/service</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">          &lt;/</span><span style="color:#a31515;">baseAddresses</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">        &lt;/</span><span style="color:#a31515;">host</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">        &lt;</span><span style="color:#a31515;">endpoint</span><span style="color:blue;"> </span><span style="color:red;">address</span><span style="color:blue;">=</span>""<span style="color:blue;"> </span><span style="color:red;">binding</span><span style="color:blue;">=</span>"<span style="color:blue;">wsHttpBinding</span>"<span style="color:blue;"> </span><span style="color:red;">contract</span><span style="color:blue;">=</span>"<span style="color:blue;">WCFReceiver.INotifyClient</span>"<span style="color:blue;"> /&gt;</span>
<span style="color:blue;">        &lt;</span><span style="color:#a31515;">endpoint</span><span style="color:blue;"> </span><span style="color:red;">address</span><span style="color:blue;">=</span>"<span style="color:blue;">mex</span>"<span style="color:blue;"> </span><span style="color:red;">binding</span><span style="color:blue;">=</span>"<span style="color:blue;">mexHttpBinding</span>"<span style="color:blue;"> </span><span style="color:red;">contract</span><span style="color:blue;">=</span>"<span style="color:blue;">IMetadataExchange</span>"<span style="color:blue;"> /&gt;</span>
<span style="color:blue;">      &lt;/</span><span style="color:#a31515;">service</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">    &lt;/</span><span style="color:#a31515;">services</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">    &lt;</span><span style="color:#a31515;">behaviors</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">      &lt;</span><span style="color:#a31515;">serviceBehaviors</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">        &lt;</span><span style="color:#a31515;">behavior</span><span style="color:blue;"> </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">NotificationServiceBehavior</span>"<span style="color:blue;">&gt;</span>
<span style="color:blue;">          &lt;</span><span style="color:#a31515;">serviceMetadata</span><span style="color:blue;"> </span><span style="color:red;">httpGetEnabled</span><span style="color:blue;">=</span>"<span style="color:blue;">true</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">          &lt;</span><span style="color:#a31515;">serviceDebug</span><span style="color:blue;"> </span><span style="color:red;">includeExceptionDetailInFaults</span><span style="color:blue;">=</span>"<span style="color:blue;">False</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">        &lt;/</span><span style="color:#a31515;">behavior</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">      &lt;/</span><span style="color:#a31515;">serviceBehaviors</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">    &lt;/</span><span style="color:#a31515;">behaviors</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">  &lt;/</span><span style="color:#a31515;">system.serviceModel</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">&lt;/</span><span style="color:#a31515;">configuration</span><span style="color:blue;">&gt;</span></pre>
<p>Once you have all of the 3 items above ready, build it then execute it so you can set a reference for the service reference your sender application.</p>
<p>Make sure you run it as an administrator</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/wcf-run-as-administrator.jpg"><img class="alignnone size-full wp-image-1296" title="WCF Run as Administrator" src="http://anyrest.files.wordpress.com/2011/04/wcf-run-as-administrator.jpg?w=630" alt=""   /></a></p>
<p>Otherwise you will encounter this message &#8220;<span style="color:#ff0000;">HTTP could not register URL  <a href="http://+:portnumber/Directory/">http://+:portnumber/Directory/</a>.  Your process does not have access rights to this namespace</span>&#8220;</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/wcf-run-not-as-administrator-error.jpg"><img class="alignnone size-full wp-image-1297" title="WCF Run not as Administrator Error" src="http://anyrest.files.wordpress.com/2011/04/wcf-run-not-as-administrator-error.jpg?w=630" alt=""   /></a></p>
<p>Once all OK add it as a reference to your Sender project</p>
<p><a href="http://anyrest.files.wordpress.com/2011/04/wcf-service-reference.jpg"><img class="alignnone size-full wp-image-1298" title="WCF Service Reference" src="http://anyrest.files.wordpress.com/2011/04/wcf-service-reference.jpg?w=630&#038;h=509" alt="" width="630" height="509" /></a></p>
<p><span style="color:#3366ff;"><strong>WCF Sender</strong></span> &#8211; This will be the form used in sending the messages</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Windows.Forms;

<span style="color:blue;">namespace</span> WCFSender
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Form1</span> : <span style="color:#2b91af;">Form</span>
    {
        <span style="color:blue;">public</span> <span style="color:blue;">string</span> sRecieverIP = <span style="color:#a31515;">"10.10.10.10"</span>;
        <span style="color:blue;">public</span> <span style="color:blue;">int</span> iRecieverPort = 8000;
        <span style="color:blue;">public</span> <span style="color:blue;">string</span> sEndpointConfigName = <span style="color:#a31515;">"WSHttpBinding_INotifyClient"</span>;

        <span style="color:blue;">public</span> Form1()
        {
            InitializeComponent();
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> btnSend_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            <span style="color:blue;">string</span> sServiceAddress = <span style="color:#a31515;">"http://"</span> + sRecieverIP + <span style="color:#a31515;">":"</span> + 8000 + <span style="color:#a31515;">"/Notifier/service"</span>;

            ServiceReference1.<span style="color:#2b91af;">NotifyClientClient</span> oNotify = <span style="color:blue;">new</span> ServiceReference1.<span style="color:#2b91af;">NotifyClientClient</span>(sEndpointConfigName, sServiceAddress);

            <span style="color:blue;">bool</span> bSuccess = oNotify.SetPopUp(txtMessage.Text);
            <span style="color:blue;">if</span> (!bSuccess)
            {
                <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Error in sending message"</span>);
            }
            <span style="color:#2b91af;">Application</span>.Exit();
        }
    }
}</pre>
<p>Now for my choice of method, it&#8217;s all up to you what suits best as I had given the pros and cons of each method, you just have to weigh which best works in your situation.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1295/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1295&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/04/03/sending-messages-to-workstations-using-socket-programming-and-wcf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/sample.jpg" medium="image">
			<media:title type="html">Sample</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/wcf-run-as-administrator.jpg" medium="image">
			<media:title type="html">WCF Run as Administrator</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/wcf-run-not-as-administrator-error.jpg" medium="image">
			<media:title type="html">WCF Run not as Administrator Error</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/04/wcf-service-reference.jpg" medium="image">
			<media:title type="html">WCF Service Reference</media:title>
		</media:content>
	</item>
		<item>
		<title>Guide to Creating Dynamic LINQ Queries</title>
		<link>http://anyrest.wordpress.com/2011/03/15/guide-to-creating-dynamic-linq-queries/</link>
		<comments>http://anyrest.wordpress.com/2011/03/15/guide-to-creating-dynamic-linq-queries/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 10:00:23 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dynamic LINQ]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1285</guid>
		<description><![CDATA[Are you new to LINQ and trying to figure out how to create dynamic LINQ queries like the same way you do with ADO.Net?  Well it’s not that hard at all and all you need to do is use a combination of the &#8220;Where&#8221; Extension method and &#8220;IQueryable&#8221; interface. So for example we have a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1285&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Are you new to LINQ and trying to figure out how to create dynamic LINQ queries like the same way you do with ADO.Net?  Well it’s not that hard at all and all you need to do is use a combination of the &#8220;<a href="http://msdn.microsoft.com/en-us/library/bb311043.aspx" target="_blank">Where</a>&#8221; Extension method and &#8220;<a href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx" target="_blank">IQueryable</a>&#8221; interface.</p>
<p>So for example we have a Customer Table where we want to filter it by Customer ID, First Name, Last Name, Customer Type, and Active.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/03/dynamic-linq.jpg"><img class="alignnone size-full wp-image-1286" title="Dynamic LINQ" src="http://anyrest.files.wordpress.com/2011/03/dynamic-linq.jpg?w=630&#038;h=200" alt="" width="630" height="200" /></a></p>
<p>Now below is a piece of code that will generate the dynamic LINQ queries</p>
<pre style="font-family:consolas;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Customer</span>&gt; GetFilteredData(<span style="color:blue;">int</span>? CustomerID, <span style="color:blue;">string</span> FirstName, <span style="color:blue;">string</span> LastName, <span style="color:blue;">string</span> CustomerType, <span style="color:blue;">bool</span> Active)
{
    <span style="color:#2b91af;">DataClasses1DataContext</span> oDB = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataClasses1DataContext</span>();
    <span style="color:#2b91af;">IQueryable</span>&lt;<span style="color:#2b91af;">Customer</span>&gt; oDataQuery = oDB.Customers;

    <span style="color:green;">//Filter by Integer</span>
    <span style="color:blue;">if</span> (CustomerID != <span style="color:blue;">null</span>)
    {
        oDataQuery = oDataQuery.Where(a =&gt; a.CustomerID == CustomerID);
    }

    <span style="color:green;">//Filter by containing words</span>
    <span style="color:blue;">if</span> (FirstName.Trim().Length &gt; 0)
    {
        oDataQuery = oDataQuery.Where(a =&gt; a.FirstName.Contains(FirstName));
    }

    <span style="color:green;">//Filter by containing words</span>
    <span style="color:blue;">if</span> (LastName.Trim().Length &gt; 0)
    {
        oDataQuery = oDataQuery.Where(a =&gt; a.LastName.Contains(LastName));
    }

    <span style="color:green;">//Filter by a Foreign Key Relationship</span>
    <span style="color:blue;">if</span> (CustomerType.Trim().Length &gt; 0)
    {
        oDataQuery = oDataQuery.Where(a =&gt; a.CustomerTypeID == (<span style="color:blue;">from</span> b <span style="color:blue;">in</span> oDB.CustomerTypes <span style="color:blue;">where</span> b.CustomerType1 == CustomerType <span style="color:blue;">select</span> b.CustomerTypeID).FirstOrDefault());
    }

    <span style="color:green;">//Filter by boolean</span>
    oDataQuery = oDataQuery.Where(a =&gt; a.Active == Active);

    <span style="color:blue;">return</span> oDataQuery.ToList();
}</pre>
<p>Now let’s dissect what just happened:</p>
<p>1. First you had created your LINQ to SQL Data Context; in our sample we just simply call it <span style="color:#3366ff;"><strong>oDB</strong></span>.</p>
<p>2. Next we declared an IQueryable interface called <strong><span style="color:#3366ff;">oDataQuery </span></strong>and initialized it with your table properties in the Database; in this case we had named it Customers.  At this point nothing had happened yet and no data is being queried, you are just initializing the object type.</p>
<p>3. Now let’s create the where clause dynamically by filtering every step of the way starting from Customer ID to Active Columns.  At this point nothing has been queried on the database and you are just creating a dynamic query on the background. This is the big difference between the two interfaces &#8220;IQueryable&#8221; and &#8220;IEnumerable&#8221;.<br />
In &#8220;IEnumerable&#8221; it gets all of the records first in the associated table then it is the application side that filters it out.</p>
<p>To get a better understanding here are the queries that are generated on the background every step of the way.</p>
<p>At this line</p>
<pre>oDataQuery = oDataQuery.Where(a =&gt; a.CustomerID == CustomerID);</pre>
<p>This query gets generated</p>
<pre style="font-family:consolas;"><span style="color:blue;">SELECT</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerID]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[LastName]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[FirstName]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerTypeID]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Active]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Birthdate]</span>
<span style="color:blue;">FROM</span> <span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[Customers]</span> <span style="color:blue;">AS</span> <span style="color:teal;">[t0]</span>
<span style="color:blue;">WHERE </span><span style="color:gray;">(</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerID]</span><span style="color:gray;">)</span> <span style="color:gray;">=</span> <span style="color:teal;">@p0</span></pre>
<p>Then at this line</p>
<pre>oDataQuery = oDataQuery.Where(a =&gt; a.FirstName.Contains(FirstName));</pre>
<p>This query gets generated</p>
<pre style="font-family:consolas;"><span style="color:blue;">SELECT</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerID]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[LastName]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[FirstName]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerTypeID]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Active]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Birthdate]</span>
<span style="color:blue;">FROM</span> <span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[Customers]</span> <span style="color:blue;">AS</span> <span style="color:teal;">[t0]</span>
<span style="color:blue;">WHERE </span><span style="color:gray;">(</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[FirstName]</span> <span style="color:gray;">LIKE</span> <span style="color:teal;">@p0</span><span style="color:gray;">)</span> <span style="color:gray;">AND</span><span style="color:blue;"> </span><span style="color:gray;">((</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerID]</span><span style="color:gray;">)</span> <span style="color:gray;">=</span> <span style="color:teal;">@p1</span><span style="color:gray;">)</span></pre>
<p>So you see how the next condition is appenededon the where clause.  This goes on until you had finalized the whole query and in our sample it will look like this</p>
<pre style="font-family:consolas;"><span style="color:blue;">SELECT</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerID]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[LastName]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[FirstName]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerTypeID]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Active]</span><span style="color:gray;">,</span> <span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Birthdate]</span>
<span style="color:blue;">FROM</span> <span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[Customers]</span> <span style="color:blue;">AS</span> <span style="color:teal;">[t0]</span>
<span style="color:blue;">WHERE </span><span style="color:gray;">(</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[Active]</span> <span style="color:gray;">=</span> 1<span style="color:gray;">)</span> <span style="color:gray;">AND</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerTypeID]</span> <span style="color:gray;">=</span><span style="color:blue;"> </span><span style="color:gray;">((</span>
    <span style="color:blue;">SELECT</span> <span style="color:blue;">TOP </span><span style="color:gray;">(</span>1<span style="color:gray;">)</span> <span style="color:teal;">[t1]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerTypeID]</span>
    <span style="color:blue;">FROM</span> <span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerTypes]</span> <span style="color:blue;">AS</span> <span style="color:teal;">[t1]</span>
    <span style="color:blue;">WHERE</span> <span style="color:teal;">[t1]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerType]</span> <span style="color:gray;">=</span> <span style="color:teal;">@p0</span>
    <span style="color:gray;">)))</span> <span style="color:gray;">AND</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[LastName]</span> <span style="color:gray;">LIKE</span> <span style="color:teal;">@p1</span><span style="color:gray;">)</span> <span style="color:gray;">AND</span><span style="color:blue;"> </span><span style="color:gray;">(</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[FirstName]</span> <span style="color:gray;">LIKE</span> <span style="color:teal;">@p2</span><span style="color:gray;">)</span> <span style="color:gray;">AND</span><span style="color:blue;"> </span><span style="color:gray;">((</span><span style="color:teal;">[t0]</span><span style="color:gray;">.</span><span style="color:teal;">[CustomerID]</span><span style="color:gray;">)</span> <span style="color:gray;">=</span> <span style="color:teal;">@p3</span></pre>
<p>It is your <span style="color:#3366ff;"><strong>[if] </strong></span>statement that controls whether a new where clause will be added</p>
<p>4. Finally you have to output it as something like a <span style="color:#3366ff;"><strong>list </strong></span>so we will use the .<span style="color:#3366ff;"><strong>ToList()</strong></span> method to get your desired query results.</p>
<p>Still not convinced, I had run a profiler at the background to see what queries are performed and it only shows the final query that was generated by LINQ</p>
<p><a href="http://anyrest.files.wordpress.com/2011/03/dynamic-linq-profiler.jpg"><img class="alignnone size-full wp-image-1288" title="Dynamic LINQ Profiler" src="http://anyrest.files.wordpress.com/2011/03/dynamic-linq-profiler.jpg?w=630&#038;h=272" alt="" width="630" height="272" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1285/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1285/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1285/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1285&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/03/15/guide-to-creating-dynamic-linq-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/03/dynamic-linq.jpg" medium="image">
			<media:title type="html">Dynamic LINQ</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/03/dynamic-linq-profiler.jpg" medium="image">
			<media:title type="html">Dynamic LINQ Profiler</media:title>
		</media:content>
	</item>
		<item>
		<title>How to use LINQ to Sharepoint</title>
		<link>http://anyrest.wordpress.com/2011/02/10/how-to-use-linq-to-sharepoint/</link>
		<comments>http://anyrest.wordpress.com/2011/02/10/how-to-use-linq-to-sharepoint/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 09:30:30 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>
		<category><![CDATA[SPMetal]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1274</guid>
		<description><![CDATA[LINQ or Language Integrated Query is a very useful language which reduces the complexity of accessing and integrating information that is not natively defined using OO technologies, with LINQ it adds native data querying capabilities to .NET languages which makes a developers life really easy.  Since it was launched I started using it as it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1274&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>LINQ or Language Integrated Query is a very useful language which reduces the complexity of accessing and integrating information that is not natively defined using OO technologies, with LINQ it adds native data querying capabilities to .NET languages which makes a developers life really easy.  Since it was launched I started using it as it saves me a lot of time in coding my applications.</p>
<p>Now a part of what I am doing is developing custom web parts for Sharepoint and sometimes you need to access data to present that information to your users and it would be good if LINQ can be used to query what you want to make life easier.  Well that is possible and can be implemented really easily all you have to do is reference LINQ library from your project and generate entity classes, which provide an object oriented interface to the Microsoft SharePoint Foundation content databases which can be done by using <a href="http://msdn.microsoft.com/en-us/library/ee538255.aspx" target="_blank">spmetal</a>.</p>
<p>Now I will give you a step by step example on how to achieve this.</p>
<p>First definitely you need to have your list to play with, so in this example I will have Employees, Department and Position.  I guess it’s logical how you would relate this lists together.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/4-my-lists.jpg"><img class="alignnone size-full wp-image-1266" title="4 My Lists" src="http://anyrest.files.wordpress.com/2011/02/4-my-lists.jpg?w=630" alt=""   /></a></p>
<p>For the Employees list we have the following columns shown in the image below, take note that the Last Name column is the Title column which was renamed.  Position is a lookup in the Positions list.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/5-employees.jpg"><img class="alignnone size-full wp-image-1268" title="5 Employees" src="http://anyrest.files.wordpress.com/2011/02/5-employees.jpg?w=630" alt=""   /></a></p>
<p>For Position, same thing we renamed title to Position Title and Department is a Lookup in Department list.<a href="http://anyrest.files.wordpress.com/2011/02/5-positions.jpg"><img class="alignnone size-full wp-image-1269" title="5 Positions" src="http://anyrest.files.wordpress.com/2011/02/5-positions.jpg?w=630" alt=""   /></a></p>
<p>For Department, again same thing we renamed title to Department Name.<a href="http://anyrest.files.wordpress.com/2011/02/5-departments.jpg"><img class="alignnone size-full wp-image-1267" title="5 Departments" src="http://anyrest.files.wordpress.com/2011/02/5-departments.jpg?w=630" alt=""   /></a></p>
<p>Now you have the list we need, and all we need to do next is to generate the Entity reference code using SPMetal.  You can find it in the installation of your Sharepoint in the folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN.  Now to generate the Entities you need you can run this command spmetal.exe /web:http://YourSharepointURL/YourTeamSite /namespace:YourProjectNamespace /code:FileNameYouWant.cs.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/1-sp-metal.jpg"><img class="alignnone size-full wp-image-1270" title="1 SP Metal" src="http://anyrest.files.wordpress.com/2011/02/1-sp-metal.jpg?w=630&#038;h=77" alt="" width="630" height="77" /></a></p>
<p>Once it’s finished you will see the file generated in the bin folder of Sharepoint.  And now you can add that to your project.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/2-splinq.jpg"><img class="alignnone size-full wp-image-1271" title="2 SPLinq" src="http://anyrest.files.wordpress.com/2011/02/2-splinq.jpg?w=630&#038;h=342" alt="" width="630" height="342" /></a></p>
<p>Now you also need to add a reference to Microsoft.SharePoint.Linq.dll  which is in C:\Program Files\Common Files\Microsoft Shared\Web Server  Extensions\14\ISAPI folder.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/3-sp-linq-reference.jpg"><img class="alignnone size-full wp-image-1272" title="3 SP Linq reference" src="http://anyrest.files.wordpress.com/2011/02/3-sp-linq-reference.jpg?w=630&#038;h=450" alt="" width="630" height="450" /></a></p>
<p>Now you are ready to code.  For this sample we will use a GridView to show the data you need so here is a sample.</p>
<pre style="font-family:consolas;"><span style="color:blue;">&lt;</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">GridView</span> <span style="color:red;">ID</span><span style="color:blue;">="GridView1"</span> <span style="color:red;">runat</span><span style="color:blue;">="server"</span> <span style="color:red;">AutoGenerateColumns</span><span style="color:blue;">="False"</span>
    <span style="color:red;">EnableModelValidation</span><span style="color:blue;">="True"&gt;</span>
    <span style="color:blue;">&lt;</span><span style="color:maroon;">Columns</span><span style="color:blue;">&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">BoundField</span> <span style="color:red;">DataField</span><span style="color:blue;">="Title"</span> <span style="color:red;">HeaderText</span><span style="color:blue;">="Last Name"/&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">BoundField</span> <span style="color:red;">DataField</span><span style="color:blue;">="FirstName"</span> <span style="color:red;">HeaderText</span><span style="color:blue;">="First Name"/&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">BoundField</span> <span style="color:red;">DataField</span><span style="color:blue;">="Position"</span> <span style="color:red;">HeaderText</span><span style="color:blue;">="Position"</span> <span style="color:blue;">/&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">BoundField</span> <span style="color:red;">DataField</span><span style="color:blue;">="PositionDescription"</span>
            <span style="color:red;">HeaderText</span><span style="color:blue;">="Position Description"</span> <span style="color:blue;">/&gt;</span>
        <span style="color:blue;">&lt;</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">BoundField</span> <span style="color:red;">DataField</span><span style="color:blue;">="Department"</span> <span style="color:red;">HeaderText</span><span style="color:blue;">="Department"</span> <span style="color:blue;">/&gt;</span>
    <span style="color:blue;">&lt;/</span><span style="color:maroon;">Columns</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">&lt;/</span><span style="color:maroon;">asp</span><span style="color:blue;">:</span><span style="color:maroon;">GridView</span><span style="color:blue;">&gt;</span></pre>
<p>And for the code behind we use this.</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Web.UI;
<span style="color:blue;">using</span> System.Web.UI.WebControls;
<span style="color:blue;">using</span> System.Web.UI.WebControls.WebParts;
<span style="color:blue;">using</span> System.Linq;
<span style="color:blue;">using</span> Microsoft.SharePoint;
<span style="color:blue;">using</span> Microsoft.SharePoint.Linq;

<span style="color:blue;">namespace</span> Demo.VisualWebPart1
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">VisualWebPart1UserControl</span> : <span style="color:#2b91af;">UserControl</span>
    {
        <span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
        {
            <span style="color:blue;">if</span> (!IsPostBack)
            {
                BindGrid();
            }
        }

        <span style="color:blue;">private</span> <span style="color:blue;">void</span> BindGrid()
        {
            <span style="color:#2b91af;">SPLinqDataContext</span> dc = <span style="color:blue;">new</span> <span style="color:#2b91af;">SPLinqDataContext</span>(<span style="color:#2b91af;">SPContext</span>.Current.Web.Url);
            <span style="color:#2b91af;">EntityList</span>&lt;<span style="color:#2b91af;">EmployeesItem</span>&gt; Employees = dc.GetList&lt;<span style="color:#2b91af;">EmployeesItem</span>&gt;(<span style="color:#a31515;">"Employees"</span>);

            <span style="color:blue;">var</span> EmployeeQuery = <span style="color:blue;">from</span> e <span style="color:blue;">in</span> Employees.ToList()
                                <span style="color:blue;">select</span> <span style="color:blue;">new</span>
                                {
                                    e.Title,
                                    e.FirstName,
                                    Position = e.Position.Title,
                                    PositionDescription = e.Position.Description,
                                    Department = e.Position.Department.Title
                                };
            GridView1.DataSource = EmployeeQuery;
            GridView1.DataBind();
        }
    }
}</pre>
<p>If you notice we use .Title a lot, this is because when we reused and  renamed the Title column that SharePoint defaulted to, Sharepoint still  retains the name internally it is still referred to as Title instead of  the renamed column name.  And if you notice becuase of LINQ its easy to drill down to the sub lists that the primary list is related to, so rather than creating a join to the other list we just use this for example</p>
<pre>e.Position.Description</pre>
<p>also you notice there is a lot of Title Colums, its easy to create an alias for it by doing this</p>
<pre>Department = e.Position.Department.Title</pre>
<p>and finally if you have been using LINQ for some time you might ask why we used</p>
<pre>Employees.ToList()
</pre>
<p>rather than just using</p>
<pre>Employees</pre>
<p>Then here is a really good explanation about it -&gt; <a href="http://blog.sharepointsite.co.uk/2010/06/inefficient-queries-spmetal-join-issue.html" target="_blank">http://blog.sharepointsite.co.uk/2010/06/inefficient-queries-spmetal-join-issue.html </a></p>
<p>Happy coding!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1274/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1274&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/02/10/how-to-use-linq-to-sharepoint/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/4-my-lists.jpg" medium="image">
			<media:title type="html">4 My Lists</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/5-employees.jpg" medium="image">
			<media:title type="html">5 Employees</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/5-positions.jpg" medium="image">
			<media:title type="html">5 Positions</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/5-departments.jpg" medium="image">
			<media:title type="html">5 Departments</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/1-sp-metal.jpg" medium="image">
			<media:title type="html">1 SP Metal</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/2-splinq.jpg" medium="image">
			<media:title type="html">2 SPLinq</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/3-sp-linq-reference.jpg" medium="image">
			<media:title type="html">3 SP Linq reference</media:title>
		</media:content>
	</item>
		<item>
		<title>How to sort View List DropDown in Sharepoint 2010</title>
		<link>http://anyrest.wordpress.com/2011/02/07/how-to-sort-view-list-dropdown-in-sharepoint-2010/</link>
		<comments>http://anyrest.wordpress.com/2011/02/07/how-to-sort-view-list-dropdown-in-sharepoint-2010/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 09:36:24 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1246</guid>
		<description><![CDATA[Have you ever wondered where is the setting to sort the List Views you created in Sharepoint?  That drop down beside the list name that shows all the views available for that list.  Well if you are looking for it then you can search and search forever until the new version of Sharepoint comes out [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1246&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered where is the setting to sort the List Views you created in Sharepoint?  That drop down beside the list name that shows all the views available for that list.  Well if you are looking for it then you can search and search forever until the new version of Sharepoint comes out and still wont find it, because there is no such setting available and it will always default to creation date.  If you don&#8217;t know what I am talking about look at the image below and I guess that would explain.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/1-original-state.jpg"><img class="alignnone size-full wp-image-1247" title="1 Original State" src="http://anyrest.files.wordpress.com/2011/02/1-original-state.jpg?w=630" alt=""   /></a></p>
<p>Based on that image, I had created the following in Chronological Order, but I want to sort it out alphabetically which makes more sense to the users.  But how would I do that? First the quick and dirty way.</p>
<p>All you have to do is change the creation date of that View in SQL.  So get the List ID first by going to the list settings and you will see it either in the Address bar or on the Mobile section of the list properties.  Take note of that.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/2-list-id.jpg"><img class="alignnone size-full wp-image-1248" title="2 List ID" src="http://anyrest.files.wordpress.com/2011/02/2-list-id.jpg?w=630&#038;h=325" alt="" width="630" height="325" /></a></p>
<p>Now you have the list ID, all you have to do is to go to the database server for your Sharepoint instance and go to WSS_Content database and choose AllWebParts table.  You can select using this query</p>
<pre style="font-family:consolas;"><span style="color:blue;">SELECT</span> <span style="color:gray;">*</span>
<span style="color:blue;">FROM</span> <span style="color:teal;">[WSS_Content]</span><span style="color:gray;">.</span><span style="color:teal;">[dbo]</span><span style="color:gray;">.</span><span style="color:teal;">[AllWebParts]</span> 
<span style="color:blue;">WHERE</span> <span style="color:teal;">tp_ListId</span> <span style="color:gray;">=</span> <span style="color:red;">'Your List ID'</span>
<span style="color:gray;">AND</span> <span style="color:teal;">tp_Deleted</span> <span style="color:gray;">=</span> 0
<span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> <span style="color:teal;">tp_CreationTime</span></pre>
<p>Now this will show you all the views on that list.  And you will notice it has the same order in that drop down.  From here you can edit the dates to your liking to show the Alphabetical sorting that you need.  This is a bit of hack and a dirty version of solving the problem, it may also not be supported by Microsoft.  But if you want the proper solution then move on.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/3-database-view.jpg"><img class="alignnone size-full wp-image-1249" title="3 Database View" src="http://anyrest.files.wordpress.com/2011/02/3-database-view.jpg?w=630" alt=""   /></a></p>
<p>Now since you moved on, I guess  you are interested in getting the right solution which is by using Sharepoint Powershell.  So we will be doing the same thing above but in a proper manner as we wont go editing dates one by one but we will be cloning each View, sort them, then delete the old items and recreate according to sort order.  So in the database it&#8217;s still sorted by Date Created but since we are creating it in an alphabetical fashion then it will show as such.  Now I guess you will be using this a lot so we have to make this like an executable file so you can reuse on any site / team sites and lists that you may have.  So lets start by creating out powershell script, copy and paste that code below and save it as Sort.ps1.</p>
<pre><span style="color:#339966;">#Make the Script will Stop on errors</span>
$ErrorActionPreference = "<span style="color:#999999;">Stop</span>"

<span style="color:#339966;">#Get the variables needed by the following prompts</span>
$TeamSitePrompt = <span style="color:#993366;">Read-Host</span> "<span style="color:#999999;">What is the Team Site URL (i.e. http://test.com/teamsite)</span>"
$ListNamePrompt = <span style="color:#993366;">Read-Host</span> "<span style="color:#999999;">What is the List Name you want to sort</span>" 

<span style="color:#339966;">#Create a Sorted List to store the Views you want to Sort</span>
$SortedList = <span style="color:#993366;">New-Object </span>System.Collections.SortedList

$Web = Get-SPWeb $TeamSitePrompt
$List = $Web.Lists[$ListNamePrompt]
$Views = $List.Views

<span style="color:#339966;">#Loop through the Views on the List and Save then on the Sorted List </span>
<span style="color:#339966;">#This will be automatically sorted as the Object is System.Collections.SortedList</span>
<span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">Saving the List </span>" $ListNamePrompt "<span style="color:#999999;"> to a Sorted Collection</span>"
<span style="color:#0000ff;">foreach </span>($CurrentViewItem in $List.Views)
{
 $SortedList.Add($CurrentViewItem.Title, $CurrentViewItem.Title)
}
<span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">---------------------------------------</span>"

<span style="color:#339966;">#Now the List is Sorted we Delete the View Item and Create it Again</span>
<span style="color:#339966;">#This makes it look like the Views are listed alphabetically but they are still listed by Creation Date</span>
<span style="color:#339966;">#Were just recreating them in alphabetical manner</span>
<span style="color:#0000ff;">foreach </span>($CurrentKeyItem in $SortedList.Keys)
{
 <span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">Processing Current View </span>" $ViewToDelete.Title
 $ViewToDelete = $List.Views[$CurrentKeyItem]

<span style="color:#339966;"> #Dulpicate the Current View</span>
 <span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">Duplicating Please wait ...</span>"
 $NewVew = $ViewToDelete.Clone($ViewToDelete.Title, $ViewToDelete.RowLimit, $ViewToDelete.Paged, $ViewToDelete.DefaultView)

 <span style="color:#339966;">#Delete the Old View, the New View will have a New ID so we use ID to Delete</span>
 <span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">Deleting the old View</span>"
 $List.Views.Delete($ViewToDelete.ID)

 <span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">---------------------------------------</span>"
 }

<span style="color:#339966;">#Set $ErrorActionPreference back to "Continue"</span>
$ErrorActionPreference = "<span style="color:#999999;">Continue</span>"
<span style="color:#993366;">Write-Host</span> "<span style="color:#999999;">------- F I N I S H E D --------</span>"</pre>
<p>Now you created the script copy it over to you Sharepoint server and run the powershell script by going to Sharepoint 2010 Management Shell.  Follow the prompts.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/4-run-script.jpg"><img class="alignnone size-full wp-image-1250" title="4 Run Script" src="http://anyrest.files.wordpress.com/2011/02/4-run-script.jpg?w=630&#038;h=424" alt="" width="630" height="424" /></a></p>
<p>Once that&#8217;s done then all your views are sorted, to double-check run again your SQL Query above and see whats recorded on the database.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/5-database-view-new.jpg"><img class="alignnone size-full wp-image-1251" title="5 Database View New" src="http://anyrest.files.wordpress.com/2011/02/5-database-view-new.jpg?w=630" alt=""   /></a></p>
<p>You can also see you views are sorted alphabetically</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/6-sorted.jpg"><img class="alignnone size-full wp-image-1252" title="6 Sorted" src="http://anyrest.files.wordpress.com/2011/02/6-sorted.jpg?w=630" alt=""   /></a></p>
<p>But wait, you might see the &#8220;Webpage cannot be found&#8221; page, don&#8217;t worry your list was not corrupted, this happens when you have a lot of views configured and Sharepoint is still processing this at the background, wait for it or have a cup of coffee and when you  go back hopefully it shows <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/7-not-found.jpg"><img class="alignnone size-full wp-image-1253" title="7 Not Found" src="http://anyrest.files.wordpress.com/2011/02/7-not-found.jpg?w=630&#038;h=298" alt="" width="630" height="298" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1246/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1246/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1246/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1246&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/02/07/how-to-sort-view-list-dropdown-in-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/1-original-state.jpg" medium="image">
			<media:title type="html">1 Original State</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/2-list-id.jpg" medium="image">
			<media:title type="html">2 List ID</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/3-database-view.jpg" medium="image">
			<media:title type="html">3 Database View</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/4-run-script.jpg" medium="image">
			<media:title type="html">4 Run Script</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/5-database-view-new.jpg" medium="image">
			<media:title type="html">5 Database View New</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/6-sorted.jpg" medium="image">
			<media:title type="html">6 Sorted</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/7-not-found.jpg" medium="image">
			<media:title type="html">7 Not Found</media:title>
		</media:content>
	</item>
		<item>
		<title>Proper way of Developing and Setting up a Windows Service Project (Step by Step)</title>
		<link>http://anyrest.wordpress.com/2011/02/06/proper-way-of-developing-and-setting-up-a-windows-service-project-step-by-step/</link>
		<comments>http://anyrest.wordpress.com/2011/02/06/proper-way-of-developing-and-setting-up-a-windows-service-project-step-by-step/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 03:00:45 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Setup]]></category>
		<category><![CDATA[Windows Service]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1219</guid>
		<description><![CDATA[Creating a windows service is easy but it might be a bit tricky specially for those who is new in developing such project, like me when I developed my first windows service before I have to use installutil by invoking such command C:\WINDOWS\Microsoft.NET\Framework\{FrameworkVersion}\InstallUtil.exe C:\MyDemoService.exe Also the Setup project wizard in Visual Studio is not that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1219&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Creating a windows service is easy but it might be a bit tricky specially for those who is new in developing such project, like me when I developed my first windows service before I have to use installutil by invoking such command</p>
<pre>C:\WINDOWS\Microsoft.NET\Framework\{FrameworkVersion}\InstallUtil.exe C:\MyDemoService.exe</pre>
<p>Also the Setup project wizard in Visual Studio is not that straightforward, so if you are looking for a step by step walk-through on how to achieve this task then look no further as i will explain everything from development to set up of project in the least amount of steps possible.</p>
<p>Now lets start by creating a Windows Service Project</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/1-new-project.jpg"><img class="alignnone size-full wp-image-1220" title="1 New Project" src="http://anyrest.files.wordpress.com/2011/02/1-new-project.jpg?w=630&#038;h=435" alt="" width="630" height="435" /></a></p>
<p>Once it created, you can now do the coding by switching to code view</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/2-do-your-coding.jpg"><img class="alignnone size-full wp-image-1221" title="2 Do your Coding" src="http://anyrest.files.wordpress.com/2011/02/2-do-your-coding.jpg?w=630&#038;h=404" alt="" width="630" height="404" /></a></p>
<p>Now for our sample we will write items on event log every 5 seconds, this makes sure that we know that the service is running as expected.  So copy the code below to your project.</p>
<pre style="font-family:consolas;"><span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Collections.Generic;
<span style="color:blue;">using</span> System.ComponentModel;
<span style="color:blue;">using</span> System.Data;
<span style="color:blue;">using</span> System.Diagnostics;
<span style="color:blue;">using</span> System.Linq;
<span style="color:blue;">using</span> System.ServiceProcess;
<span style="color:blue;">using</span> System.Text;
<span style="color:blue;">using</span> System.Timers;

<span style="color:blue;">namespace</span> Demo_Windows_Service
{
    <span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Service1</span> : <span style="color:#2b91af;">ServiceBase</span>
    {
        <span style="color:#2b91af;">Timer</span> myTimer = <span style="color:blue;">new</span> <span style="color:#2b91af;">Timer</span>();
        <span style="color:blue;">public</span> Service1()
        {
            InitializeComponent();
        }

        <span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> OnStart(<span style="color:blue;">string</span>[] args)
        {
            <span style="color:green;">//Starting </span>

            <span style="color:green;">//Handle Elapsed Event</span>
            myTimer.Elapsed += <span style="color:blue;">new</span> <span style="color:#2b91af;">ElapsedEventHandler</span>(OnElapsedTime);

            <span style="color:green;">//Set Interval to 1 Second </span>
            myTimer.Interval = 5000;

            <span style="color:green;">//Enable the Timer</span>
            myTimer.Enabled = <span style="color:blue;">true</span>;
        }

        <span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> OnStop()
        {
        }
        <span style="color:blue;">private</span> <span style="color:blue;">void</span> OnElapsedTime(<span style="color:blue;">object</span> source, <span style="color:#2b91af;">ElapsedEventArgs</span> e)
        {
            myTimer.Enabled = <span style="color:blue;">false</span>;

            <span style="color:green;">//Perform Processing Here</span>
            DoSomething();

            myTimer.Enabled = <span style="color:blue;">true</span>;
        }

        <span style="color:blue;">private</span> <span style="color:blue;">void</span> DoSomething()
        {
            <span style="color:blue;">string</span> sSource;
            <span style="color:blue;">string</span> sLog;
            <span style="color:blue;">string</span> sEvent;

            sSource = <span style="color:#a31515;">"Demo Windows Service"</span>;
            sLog = <span style="color:#a31515;">"Log Something : "</span> + <span style="color:#2b91af;">DateTime</span>.Now.ToString();
            sEvent = <span style="color:#a31515;">"Event Fired"</span>;

            <span style="color:blue;">if</span> (!<span style="color:#2b91af;">EventLog</span>.SourceExists(sSource))  <span style="color:#2b91af;">EventLog</span>.CreateEventSource(sSource, sLog);

            <span style="color:#2b91af;">EventLog</span>.WriteEntry(sSource, sEvent);
            <span style="color:#2b91af;">EventLog</span>.WriteEntry(sSource, sEvent, <span style="color:#2b91af;">EventLogEntryType</span>.Information, 100);

        }
    }
}</pre>
<p>Once finished add an installer in your service, this piece of configuration will be invoked by the setup project.  So go to the Service1.cs [Design] and right-click on the open area, then choose &#8220;Add Installer&#8221;</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/3-add-installer.jpg"><img class="alignnone size-full wp-image-1222" title="3 Add Installer" src="http://anyrest.files.wordpress.com/2011/02/3-add-installer.jpg?w=630&#038;h=404" alt="" width="630" height="404" /></a></p>
<p>Once done you will hae two objects to configure, which are serviceProcessInstaller1 and serviceInstaller1</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/4-configure-installer.jpg"><img class="alignnone size-full wp-image-1223" title="4 Configure Installer" src="http://anyrest.files.wordpress.com/2011/02/4-configure-installer.jpg?w=630&#038;h=404" alt="" width="630" height="404" /></a></p>
<p>Now lets configure serviceProcessInstaller1, this defines how the service will run under as.  These are the things you see in the Logon tab of the service property.  Now you can choose whether you want it to be User, LocalSystem, NetworkService or LocalService.</p>
<p>If you don&#8217;t know the difference between each of them then here is a description of all:</p>
<ul>
<li><strong><span style="color:#3366ff;">User </span></strong>- This can be a Domain User or a Local user Account, these are non built-in accounts that are given rights by an administrator, so access rights might vary so check with your administrator.</li>
<li><strong><span style="color:#3366ff;">Local System or &#8220;NT AUTHORITY\SYSTEM&#8221;</span></strong> &#8211; This is a built-in account with a very high-privilege on the local system and it acts as the computer on the network.</li>
<li><span style="color:#3366ff;"><strong>Local Service or &#8220;NT AUTHORITY\LOCAL SERVICE&#8221;</strong></span> &#8211; This is a built-in account that has the same privileges like the members of the Users group, which means it has limited access.  This account accesses the network resources as a null session without credentials.</li>
<li><span style="color:#3366ff;"><strong>Network Service or &#8220;NT AUTHORITY\NETWORK SERVICE&#8221; </strong></span>- This is a built-in account that has more privileges than the members of the Users group.  This account accesses the network resources by using the credentials of the computer account</li>
</ul>
<p><a href="http://anyrest.files.wordpress.com/2011/02/5-configure-service-process-installer.jpg"><img class="alignnone size-full wp-image-1224" title="5 Configure Service Process Installer" src="http://anyrest.files.wordpress.com/2011/02/5-configure-service-process-installer.jpg?w=630" alt=""   /></a></p>
<p>Now lets configure serviceInstaller1, this section defines how the service will behave and it will also contain the description and name of the service you are setting up.  These are the things you see in the General tab of the service property.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/6-configure-service-installer.jpg"><img class="alignnone size-full wp-image-1225" title="6 Configure Service Installer" src="http://anyrest.files.wordpress.com/2011/02/6-configure-service-installer.jpg?w=630" alt=""   /></a></p>
<p>Now you are done with your installer, Next thing to do now is your Setup Project, do that by adding a new project in your solution</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/7-add-a-new-setup-project-1.jpg"><img class="alignnone size-full wp-image-1226" title="7 Add a New Setup Project 1" src="http://anyrest.files.wordpress.com/2011/02/7-add-a-new-setup-project-1.jpg?w=630&#038;h=368" alt="" width="630" height="368" /></a></p>
<p>Choose a&#8221; setup project&#8221; under &#8220;other project types&#8221;</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/8-add-a-new-setup-project-2.jpg"><img class="alignnone size-full wp-image-1227" title="8 Add a New Setup Project 2" src="http://anyrest.files.wordpress.com/2011/02/8-add-a-new-setup-project-2.jpg?w=630&#038;h=435" alt="" width="630" height="435" /></a></p>
<p>Once chosen, you will be directed to file system editor, if it does not happen you can always right-click the project then choose &#8220;View&#8221; -&gt; &#8220;File System&#8221;.  Once in the File System Properties choose Application Folder and right-click on it to add a &#8220;Project Output&#8221; this is where you define what is the output of your project which can be an Executable File, Class Library or even third-party Dll&#8217;s, In our case it the class library.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/9-configure-setup-project.jpg"><img class="alignnone size-full wp-image-1228" title="9 Configure Setup Project" src="http://anyrest.files.wordpress.com/2011/02/9-configure-setup-project.jpg?w=630&#038;h=177" alt="" width="630" height="177" /></a></p>
<p>Just choose the Service Project the Primary Output, click OK</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/10-configure-setup-project-2.jpg"><img class="alignnone size-full wp-image-1229" title="10 Configure Setup Project 2" src="http://anyrest.files.wordpress.com/2011/02/10-configure-setup-project-2.jpg?w=630" alt=""   /></a></p>
<p>Now it will display all outputs plus dependencies if you have any on your Windows Service Project</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/11-configure-setup-project-3.jpg"><img class="alignnone size-full wp-image-1230" title="11 Configure Setup Project 3" src="http://anyrest.files.wordpress.com/2011/02/11-configure-setup-project-3.jpg?w=630&#038;h=142" alt="" width="630" height="142" /></a></p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/12-configure-setup-project-4.jpg"><img class="alignnone size-full wp-image-1231" title="12 Configure Setup Project 4" src="http://anyrest.files.wordpress.com/2011/02/12-configure-setup-project-4.jpg?w=630" alt=""   /></a></p>
<p>Now choose custom actions, this defines what is copied on Install, Commit, Rollback and Uninstall.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/13-configure-setup-project-5.jpg"><img class="alignnone size-full wp-image-1232" title="13 Configure Setup Project 5" src="http://anyrest.files.wordpress.com/2011/02/13-configure-setup-project-5.jpg?w=630" alt=""   /></a></p>
<p>Choose the items you had defined on the File System Properties by going to &#8220;Application Folder&#8221;</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/14-configure-setup-project-6.jpg"><img class="alignnone size-full wp-image-1233" title="14 Configure Setup Project 6" src="http://anyrest.files.wordpress.com/2011/02/14-configure-setup-project-6.jpg?w=630" alt=""   /></a></p>
<p>Then choose the Primary Output of your Service</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/15-configure-setup-project-7.jpg"><img class="alignnone size-full wp-image-1234" title="15 Configure Setup Project 7" src="http://anyrest.files.wordpress.com/2011/02/15-configure-setup-project-7.jpg?w=630" alt=""   /></a></p>
<p>Now it will show you that they are now configured on Install, Commit, Rollback and Uninstall</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/16-configure-setup-project-8.jpg"><img class="alignnone size-full wp-image-1235" title="16 Configure Setup Project 8" src="http://anyrest.files.wordpress.com/2011/02/16-configure-setup-project-8.jpg?w=630" alt=""   /></a></p>
<p>Now you are left with the properties, you can place you Installer description, author, manufacturer, installer behaviour etc.  This is the one shown on the add remove programs.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/17-setup-properties.jpg"><img class="alignnone size-full wp-image-1236" title="17 Setup Properties" src="http://anyrest.files.wordpress.com/2011/02/17-setup-properties.jpg?w=630" alt=""   /></a></p>
<p>Now build your solution, then build your setup project.  Once done go to the Setup project folder and copy your setup to where you want to install your Windows service.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/18-setup-file.jpg"><img class="alignnone size-full wp-image-1237" title="18 Setup File" src="http://anyrest.files.wordpress.com/2011/02/18-setup-file.jpg?w=630" alt=""   /></a></p>
<p>Now run the setup and just follow the wizard</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/19-setup.jpg"><img class="alignnone size-full wp-image-1238" title="19 Setup" src="http://anyrest.files.wordpress.com/2011/02/19-setup.jpg?w=630" alt=""   /></a></p>
<p>Once finished you are all good to go, type services.msc on the run command to see if your service have successfully installed.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/20-service-list.jpg"><img class="alignnone size-full wp-image-1239" title="20 Service List" src="http://anyrest.files.wordpress.com/2011/02/20-service-list.jpg?w=630&#038;h=300" alt="" width="630" height="300" /></a></p>
<p>You can also see programs and features, you can uninstall it from here.</p>
<p><a href="http://anyrest.files.wordpress.com/2011/02/21-uninstall.jpg"><img class="alignnone size-full wp-image-1240" title="21 Uninstall" src="http://anyrest.files.wordpress.com/2011/02/21-uninstall.jpg?w=630&#038;h=325" alt="" width="630" height="325" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1219/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1219&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2011/02/06/proper-way-of-developing-and-setting-up-a-windows-service-project-step-by-step/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/1-new-project.jpg" medium="image">
			<media:title type="html">1 New Project</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/2-do-your-coding.jpg" medium="image">
			<media:title type="html">2 Do your Coding</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/3-add-installer.jpg" medium="image">
			<media:title type="html">3 Add Installer</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/4-configure-installer.jpg" medium="image">
			<media:title type="html">4 Configure Installer</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/5-configure-service-process-installer.jpg" medium="image">
			<media:title type="html">5 Configure Service Process Installer</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/6-configure-service-installer.jpg" medium="image">
			<media:title type="html">6 Configure Service Installer</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/7-add-a-new-setup-project-1.jpg" medium="image">
			<media:title type="html">7 Add a New Setup Project 1</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/8-add-a-new-setup-project-2.jpg" medium="image">
			<media:title type="html">8 Add a New Setup Project 2</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/9-configure-setup-project.jpg" medium="image">
			<media:title type="html">9 Configure Setup Project</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/10-configure-setup-project-2.jpg" medium="image">
			<media:title type="html">10 Configure Setup Project 2</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/11-configure-setup-project-3.jpg" medium="image">
			<media:title type="html">11 Configure Setup Project 3</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/12-configure-setup-project-4.jpg" medium="image">
			<media:title type="html">12 Configure Setup Project 4</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/13-configure-setup-project-5.jpg" medium="image">
			<media:title type="html">13 Configure Setup Project 5</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/14-configure-setup-project-6.jpg" medium="image">
			<media:title type="html">14 Configure Setup Project 6</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/15-configure-setup-project-7.jpg" medium="image">
			<media:title type="html">15 Configure Setup Project 7</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/16-configure-setup-project-8.jpg" medium="image">
			<media:title type="html">16 Configure Setup Project 8</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/17-setup-properties.jpg" medium="image">
			<media:title type="html">17 Setup Properties</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/18-setup-file.jpg" medium="image">
			<media:title type="html">18 Setup File</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/19-setup.jpg" medium="image">
			<media:title type="html">19 Setup</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/20-service-list.jpg" medium="image">
			<media:title type="html">20 Service List</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2011/02/21-uninstall.jpg" medium="image">
			<media:title type="html">21 Uninstall</media:title>
		</media:content>
	</item>
		<item>
		<title>How to edit the Welcome drop-down menu in Sharepoint 2010</title>
		<link>http://anyrest.wordpress.com/2010/12/21/how-to-edit-the-welcome-drop-down-menu-in-sharepoint-2010/</link>
		<comments>http://anyrest.wordpress.com/2010/12/21/how-to-edit-the-welcome-drop-down-menu-in-sharepoint-2010/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 09:38:35 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1199</guid>
		<description><![CDATA[Did you ever wanted to extend that user menu in Sharepoint, the one that states your name on the top right corner and drops down items such as &#8220;My Settings&#8221;, &#8220;Sign in as a Different User&#8221; and &#8220;Sign Out&#8221;? Well search no more as I will show you step by step on how to do [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1199&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Did you ever wanted to extend that user menu in Sharepoint, the one that states your name on the top right corner and drops down items such as &#8220;My Settings&#8221;, &#8220;Sign in as a Different User&#8221; and &#8220;Sign Out&#8221;?  Well search no more as I will show you step by step on how to do that in Sharepoint Foundation 2010, this also works in older versions of Sharepoint (just take note, that where ever a folder location is stated refer to the folder version you are in).  So lets start.</p>
<h2><strong><span style="color:#3366ff;">1. Create an Empty Sharepoint Project</span></strong></h2>
<p><a href="http://anyrest.files.wordpress.com/2010/12/01-new-empty-sharepoint-project.jpg"><img class="alignnone size-full wp-image-1200" title="01 New Empty Sharepoint Project" src="http://anyrest.files.wordpress.com/2010/12/01-new-empty-sharepoint-project.jpg?w=630&#038;h=354" alt="" width="630" height="354" /></a></p>
<p>You need Visual Studio 2010 to start with as well as well as the SDK for Sharepoint 2010, for a guide on how to install Sharepoint 2010 in a non server environment such as your development workstation please refer <a href="http://anyrest.wordpress.com/2010/12/14/install-sharepoint-2010-in-a-non-server-os/" target="_blank">here</a>.  Once you have it properly installed go and create an empty Sharepoint Project.</p>
<h2><span style="color:#3366ff;"><strong>2. Updating the Solution Feature</strong></span></h2>
<p><a href="http://anyrest.files.wordpress.com/2010/12/02-update-feature.jpg"><img class="alignnone size-full wp-image-1201" title="02 Update Feature" src="http://anyrest.files.wordpress.com/2010/12/02-update-feature.jpg?w=630" alt=""   /></a></p>
<p>Now You will see all proper files are created for you to get started</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/03-feature-properties.jpg"><img class="alignnone size-full wp-image-1202" title="03 Feature Properties" src="http://anyrest.files.wordpress.com/2010/12/03-feature-properties.jpg?w=630" alt=""   /></a></p>
<p>We are interested on the Features section so double-click Feature1 and update the properties, make sure you change the scope to &#8220;<strong>WebApplication</strong>&#8220;</p>
<h2><span style="color:#3366ff;"><strong>3. Create an Element </strong></span></h2>
<p><a href="http://anyrest.files.wordpress.com/2010/12/04-create-empty-element.jpg"><img class="alignnone size-full wp-image-1203" title="04 Create Empty Element" src="http://anyrest.files.wordpress.com/2010/12/04-create-empty-element.jpg?w=630&#038;h=354" alt="" width="630" height="354" /></a></p>
<p>Now you need to create an element to attach to your feature, to do that right-click on the project and add a new item, then choose &#8220;<strong>Empty Element</strong>&#8220;.  Dont forget to give it a Name.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/05-verify-element.jpg"><img class="alignnone size-full wp-image-1204" title="05 Verify Element" src="http://anyrest.files.wordpress.com/2010/12/05-verify-element.jpg?w=630&#038;h=373" alt="" width="630" height="373" /></a></p>
<p>Once added verify by going to the Feature design or Manifest whether it had attached to the feature.  If all is OK, edit your element to reflect what you needed (The additional menu item on the drop down menu), here is a sample code to create a &#8220;My Information&#8221; link which goes to a page in Sharepoint called &#8220;/SitePages/My%20Information.aspx&#8221;</p>
<pre style="font-family:consolas;"><span style="color:blue;">&lt;?</span><span style="color:#a31515;">xml</span><span style="color:blue;"> </span><span style="color:red;">version</span><span style="color:blue;">=</span>"<span style="color:blue;">1.0</span>"<span style="color:blue;"> </span><span style="color:red;">encoding</span><span style="color:blue;">=</span>"<span style="color:blue;">utf-8</span>"<span style="color:blue;">?&gt;</span>
<span style="color:blue;">&lt;</span><span style="color:#a31515;">Elements</span><span style="color:blue;"> </span><span style="color:red;">xmlns</span><span style="color:blue;">=</span>"<span style="color:blue;">http://schemas.microsoft.com/sharepoint/</span>"<span style="color:blue;">&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">CustomAction</span>
<span style="color:blue;">        </span><span style="color:red;">Id</span><span style="color:blue;"> =</span>"<span style="color:blue;">CustomMenu_MyInformation</span>"
<span style="color:blue;">        </span><span style="color:red;">GroupId</span><span style="color:blue;">=</span>"<span style="color:blue;">PersonalActions</span>"
<span style="color:blue;">        </span><span style="color:red;">Location</span><span style="color:blue;">=</span>"<span style="color:blue;">Microsoft.SharePoint.StandardMenu</span>"
<span style="color:blue;">        </span><span style="color:red;">Sequence</span><span style="color:blue;">=</span>"<span style="color:blue;">1000</span>"
<span style="color:blue;">        </span><span style="color:red;">Title</span><span style="color:blue;">=</span>"<span style="color:blue;">My Information</span>"
<span style="color:blue;">        </span><span style="color:red;">Description</span><span style="color:blue;">=</span>"<span style="color:blue;">View and Update my Information</span>"<span style="color:blue;"> </span>
<span style="color:blue;">        </span><span style="color:red;">ImageUrl</span><span style="color:blue;">=</span>"<span style="color:blue;">_layouts/images/icon_contactlist.gif</span>"<span style="color:blue;">&gt;</span>
<span style="color:blue;">    &lt;</span><span style="color:#a31515;">UrlAction</span><span style="color:blue;"> </span><span style="color:red;">Url</span><span style="color:blue;">=</span>"<span style="color:blue;">/SitePages/My%20Information.aspx</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">  &lt;/</span><span style="color:#a31515;">CustomAction</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Elements</span><span style="color:blue;">&gt;</span>
</pre>
<p>Now for a bit of explanation how did we get that code and what&#8217;s in there.  In case you have been tinkering on how to edit the welcome drop-down menu item then you might have encountered  posts in the net like from <a href="http://miss-sharepoint.blogspot.com/2009/01/edit-welcome-dropdown-menu.html" target="_blank">Ms Sharepoint Blog</a> which talks about the file in <strong><em>C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\Welcome.ascx</em></strong>, that is the file that controls how that drop down is rendered.  If you open that file you might had noticed that there is a SharePoint:FeatureMenuTemplate Control under , and thats the control for the drop-down menu.  You can directly add items to it but there&#8217;s no point as you cannot manually add a Navigate URL Element Directly, or even if you can once you upgraded Sharepoint then your customization will not be remembered.</p>
<p>Now in that SharePoint:FeatureMenuTemplate control there is a GroupId which is &#8220;<strong>PersonalActions</strong>&#8220;, that&#8217;s the GroupId that we need to override and create custom action hence the code above is pointed to the same GroupId, we also gave it the same Location as the SharePoint:FeatureMenuTemplate, and for the others I guess its self-explanatory.</p>
<h2><span style="color:#3366ff;"><strong>4. Package your Sharepoint Solution</strong></span></h2>
<p>Once all satisfied on your custom menu all you need to do now is to package it.  Right click on the Project or on the Menu bar Choose Build-&gt;Package.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/06-package-solution.jpg"><img class="alignnone size-full wp-image-1205" title="06 Package Solution" src="http://anyrest.files.wordpress.com/2010/12/06-package-solution.jpg?w=630" alt=""   /></a></p>
<p>Take note where the package is created on your Output window</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/07-output-location.jpg"><img class="alignnone size-full wp-image-1206" title="07 Output Location" src="http://anyrest.files.wordpress.com/2010/12/07-output-location.jpg?w=630&#038;h=228" alt="" width="630" height="228" /></a></p>
<p>Now copy that and place it on your Sharepoint Servers file system, I placed mine in <em><strong>C:\Package\</strong></em></p>
<h2><span style="color:#3366ff;"><strong>5. Add and Install your Solution to Sharepoint</strong></span></h2>
<p><a href="http://anyrest.files.wordpress.com/2010/12/08-add-solution.jpg"><img class="alignnone size-full wp-image-1207" title="08 Add Solution" src="http://anyrest.files.wordpress.com/2010/12/08-add-solution.jpg?w=630&#038;h=94" alt="" width="630" height="94" /></a></p>
<p>First go to SharePoint 2010 Management Shell and type</p>
<pre>Add-SPSolution C:\YourFolder\YourPackageName.wsp</pre>
<p><a href="http://anyrest.files.wordpress.com/2010/12/09-deploy-solution.jpg"><img class="alignnone size-full wp-image-1208" title="09 Deploy Solution" src="http://anyrest.files.wordpress.com/2010/12/09-deploy-solution.jpg?w=630&#038;h=215" alt="" width="630" height="215" /></a></p>
<p>Then verify whether it is uploaded correctly by going to &#8220;SharePoint 2010 Central Administration&#8221;.    Navigate to System Settings -&gt; Manage Farm Solutions</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/10-deploy-solution-2.jpg"><img class="alignnone size-full wp-image-1209" title="10 Deploy Solution 2" src="http://anyrest.files.wordpress.com/2010/12/10-deploy-solution-2.jpg?w=630&#038;h=198" alt="" width="630" height="198" /></a></p>
<p>If your Package is there then it uploaded correctly, now click your package</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/11-deploy-solution-3.jpg"><img class="alignnone size-full wp-image-1210" title="11 Deploy Solution 3" src="http://anyrest.files.wordpress.com/2010/12/11-deploy-solution-3.jpg?w=630&#038;h=189" alt="" width="630" height="189" /></a></p>
<p>Then Deploy Solution</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/12-deploy-solution-4.jpg"><img class="alignnone size-full wp-image-1211" title="12 Deploy Solution 4" src="http://anyrest.files.wordpress.com/2010/12/12-deploy-solution-4.jpg?w=630&#038;h=206" alt="" width="630" height="206" /></a></p>
<p>It will warn you like such, just hit OK.<br />
Another option is through Powershell you can type this command (doing this eliminates the last 3 steps)</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/12b-deploy-solution-4.jpg"><img class="alignnone size-full wp-image-1212" title="12b Deploy Solution 4" src="http://anyrest.files.wordpress.com/2010/12/12b-deploy-solution-4.jpg?w=630&#038;h=129" alt="" width="630" height="129" /></a></p>
<pre>Install-SPSolution -identity YourPackageName.wsp -WebApplication http://yoursharepointurl -GACDeployment</pre>
<h2><span style="color:#3366ff;"><strong>6.  Now your menu item is ready, check it out</strong></span></h2>
<p><a href="http://anyrest.files.wordpress.com/2010/12/13-result.jpg"><img class="alignnone size-full wp-image-1213" title="13 Result" src="http://anyrest.files.wordpress.com/2010/12/13-result.jpg?w=630" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1199/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1199&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2010/12/21/how-to-edit-the-welcome-drop-down-menu-in-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/01-new-empty-sharepoint-project.jpg" medium="image">
			<media:title type="html">01 New Empty Sharepoint Project</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/02-update-feature.jpg" medium="image">
			<media:title type="html">02 Update Feature</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/03-feature-properties.jpg" medium="image">
			<media:title type="html">03 Feature Properties</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/04-create-empty-element.jpg" medium="image">
			<media:title type="html">04 Create Empty Element</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/05-verify-element.jpg" medium="image">
			<media:title type="html">05 Verify Element</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/06-package-solution.jpg" medium="image">
			<media:title type="html">06 Package Solution</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/07-output-location.jpg" medium="image">
			<media:title type="html">07 Output Location</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/08-add-solution.jpg" medium="image">
			<media:title type="html">08 Add Solution</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/09-deploy-solution.jpg" medium="image">
			<media:title type="html">09 Deploy Solution</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/10-deploy-solution-2.jpg" medium="image">
			<media:title type="html">10 Deploy Solution 2</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/11-deploy-solution-3.jpg" medium="image">
			<media:title type="html">11 Deploy Solution 3</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/12-deploy-solution-4.jpg" medium="image">
			<media:title type="html">12 Deploy Solution 4</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/12b-deploy-solution-4.jpg" medium="image">
			<media:title type="html">12b Deploy Solution 4</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/13-result.jpg" medium="image">
			<media:title type="html">13 Result</media:title>
		</media:content>
	</item>
		<item>
		<title>Developing Web Parts for Sharepoint 2010</title>
		<link>http://anyrest.wordpress.com/2010/12/15/developing-web-parts-for-sharepoint-2010/</link>
		<comments>http://anyrest.wordpress.com/2010/12/15/developing-web-parts-for-sharepoint-2010/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 09:00:28 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>
		<category><![CDATA[Web Part]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1178</guid>
		<description><![CDATA[Developing Web Parts for Sharepoint have never been easier with Visual Studio 2010, by installing the Sharepoint 2010 SDK you will have templates ready for you in your Visual Studio 2010 environment. But before you start you should have installed Sharepoint 2010 on your Development Workstation and if you are having some difficulties on how [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1178&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Developing Web Parts for Sharepoint have never been easier with Visual Studio 2010, by installing the <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=f0c9daf3-4c54-45ed-9bde-7b4d83a8f26f" target="_blank">Sharepoint 2010 SDK</a> you will have templates ready for you in your Visual Studio 2010 environment.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/sharepoint-projects.jpg"><img class="alignnone size-full wp-image-1179" title="Sharepoint Projects" src="http://anyrest.files.wordpress.com/2010/12/sharepoint-projects.jpg?w=630" alt=""   /></a></p>
<p>But before you start you should have installed Sharepoint 2010 on your Development Workstation and if you are having some difficulties on how to achieve that here is a quick guide on how to install <a title="Install Sharepoint 2010 in a non Server OS" href="http://anyrest.wordpress.com/2010/12/14/install-sharepoint-2010-in-a-non-server-os/" target="_blank">Sharepoint in a non server environment</a>.</p>
<p>Now lets start!</p>
<p>1. Open Visual Studio 2010 as an Administrator then create a new Visual Web Part project, give it a name.  In this sample we call it SampleWebPart.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/1-new-project.jpg"><img class="alignnone size-full wp-image-1180" title="1 New Project" src="http://anyrest.files.wordpress.com/2010/12/1-new-project.jpg?w=630&#038;h=354" alt="" width="630" height="354" /></a></p>
<p>If you did not run as an Administrator then it will prompt you to restart using a different credential</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/2-restart-under-different-credentials.jpg"><img class="alignnone size-full wp-image-1181" title="2 Restart Under Different Credentials" src="http://anyrest.files.wordpress.com/2010/12/2-restart-under-different-credentials.jpg?w=630" alt=""   /></a></p>
<p>2. Specify the local Sharepoint Server, and note that you cannot use a Sharepoint instance outside your workstation.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/3-specify-server.jpg"><img class="alignnone size-full wp-image-1182" title="3 Specify Server" src="http://anyrest.files.wordpress.com/2010/12/3-specify-server.jpg?w=630&#038;h=447" alt="" width="630" height="447" /></a></p>
<p>3. All the necessary files have been created you can now start developing</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/4-project-view.jpg"><img class="alignnone size-full wp-image-1183" title="4 Project View" src="http://anyrest.files.wordpress.com/2010/12/4-project-view.jpg?w=630" alt=""   /></a></p>
<p>You will be using VisualWebPart1UserControl.ascx.cs and VisualWebPart1UserControl.ascx, these are the controls that will be rendered on your Sharepoint instance.</p>
<p>4. Now start developing, if you are familiar with web development in Visual Studio it will be similar, you can drag and drop controls and add events to it.</p>
<p>For this sample we will put 2 controls, 1 button and 1 label.  Then put an event on the button to change the text on Label1 to &#8220;Hello World!&#8221;</p>
<pre style="font-family:consolas;"><span style="color:blue;">protected</span> <span style="color:blue;">void</span> Button1_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)
{
    Label1.Text = <span style="color:#a31515;">"Hello World!"</span>;
}</pre>
<p><a href="http://anyrest.files.wordpress.com/2010/12/5-design-view.jpg"><img class="alignnone size-full wp-image-1184" title="5 Design View" src="http://anyrest.files.wordpress.com/2010/12/5-design-view.jpg?w=630" alt=""   /></a></p>
<p>5. Now build and run your project.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/6-build.jpg"><img class="alignnone size-full wp-image-1185" title="6 Build" src="http://anyrest.files.wordpress.com/2010/12/6-build.jpg?w=630&#038;h=382" alt="" width="630" height="382" /></a></p>
<p>Now if you notice in the output screen you will also see that a package was created on your bin folder, this is what you will be using later to deploy on your server.  You can also package your solution anytime by right clicking the project and choose package.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/7-package.jpg"><img class="alignnone size-full wp-image-1186" title="7 Package" src="http://anyrest.files.wordpress.com/2010/12/7-package.jpg?w=630&#038;h=111" alt="" width="630" height="111" /></a></p>
<p>6. When the project is running your default browser will run opening your local Sharepoint instance, your control cannot be seen yet at this point and you have to add it you your page as a web part to do that, go to a page that you can edit and add your Sample Web Part, once added you can now test if the events are firing.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/8-run-project.jpg"><img class="alignnone size-full wp-image-1187" title="8 Run Project" src="http://anyrest.files.wordpress.com/2010/12/8-run-project.jpg?w=630&#038;h=449" alt="" width="630" height="449" /></a></p>
<p>7. Once you stopped the project, it will deactivate, retract and delete the solution that was imported to the local Sharepoint instance so don&#8217;t try to find it on your local Sharepoint installation afterwards as you will not find it.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/9-unloaded.jpg"><img class="alignnone size-full wp-image-1188" title="9 Unloaded" src="http://anyrest.files.wordpress.com/2010/12/9-unloaded.jpg?w=630" alt=""   /></a></p>
<p>8. Now if all is working fine you are ready to deploy.  Go to your bin folder and copy the package to your servers file system.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/10-package-location.jpg"><img class="alignnone size-full wp-image-1189" title="10 Package Location" src="http://anyrest.files.wordpress.com/2010/12/10-package-location.jpg?w=630&#038;h=356" alt="" width="630" height="356" /></a></p>
<p>9. Once copied you can run powershell scripts to install your package to your Sharepoint Server.  To do that go to SharePoint 2010 Management Shell</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/11-powershell.jpg"><img class="alignnone size-full wp-image-1190" title="11 Powershell" src="http://anyrest.files.wordpress.com/2010/12/11-powershell.jpg?w=630" alt=""   /></a></p>
<p><strong><span style="color:#3366ff;">To Add the Solution in Sharepoint</span></strong></p>
<pre>Add-SPSolution C:\YourFolder\SampleWebPart.wsp</pre>
<p><span style="color:#3366ff;"><strong>To Install the Solution to your Sharepoint</strong></span></p>
<pre>Install-SPSolution -identity SampleWebPart.wsp -WebApplication http://yoursharepointserver -GACDeployment</pre>
<p><span style="color:#3366ff;"><strong>To Update your Solution in Sharepoint</strong></span><br />
you will need this if you want to update the Webpart that is already installed</p>
<pre>Update-SPSolution –Identity SampleWebPart.wsp –LiteralPath C:\YourFolder\SampleWebPart.wsp –GACDeployment</pre>
<p>Now if something went horribly wrong or you just want to uninstall it you have to run this two commands</p>
<p><span style="color:#3366ff;"><strong>To Uninstall a Solution in Sharepoint</strong></span></p>
<pre>Uninstall-SPSolution –Identity SampleWebPart.wsp –WebApplication http://yoursharepointserver</pre>
<p>then</p>
<p><strong><span style="color:#3366ff;">Remove a solution from Sharepoint</span></strong></p>
<pre>Remove-SPSolution –Identity SampleWebPart.wsp</pre>
<p>10. At this point your webparts in now available for usage, all you have to do is to Import it into your Sharepoint Server gallery.</p>
<p>Go to Site settings (of the topmost site) then under Galleries you will see &#8220;Webparts&#8221; click on it.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/12-upload-webpart.jpg"><img class="alignnone size-full wp-image-1194" title="12 Upload Webpart" src="http://anyrest.files.wordpress.com/2010/12/12-upload-webpart.jpg?w=630" alt=""   /></a></p>
<p>Then import your new Web Part to the Gallery, click New Document</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/13-upload-webpart.jpg"><img class="alignnone size-full wp-image-1195" title="13 Upload Webpart" src="http://anyrest.files.wordpress.com/2010/12/13-upload-webpart.jpg?w=630" alt=""   /></a></p>
<p>Then you will be presented with Web Parts available for import</p>
<p><a href="http://anyrest.files.wordpress.com/2010/12/14-upload-webpart.jpg"><img class="alignnone size-full wp-image-1196" title="14 Upload Webpart" src="http://anyrest.files.wordpress.com/2010/12/14-upload-webpart.jpg?w=630&#038;h=224" alt="" width="630" height="224" /></a></p>
<p>Choose the webpart you just installed the click populate gallery, at this point your web part now is available on the Sharepoint Ribbon&#8217;s Editing Tool.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1178&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2010/12/15/developing-web-parts-for-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/sharepoint-projects.jpg" medium="image">
			<media:title type="html">Sharepoint Projects</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/1-new-project.jpg" medium="image">
			<media:title type="html">1 New Project</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/2-restart-under-different-credentials.jpg" medium="image">
			<media:title type="html">2 Restart Under Different Credentials</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/3-specify-server.jpg" medium="image">
			<media:title type="html">3 Specify Server</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/4-project-view.jpg" medium="image">
			<media:title type="html">4 Project View</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/5-design-view.jpg" medium="image">
			<media:title type="html">5 Design View</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/6-build.jpg" medium="image">
			<media:title type="html">6 Build</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/7-package.jpg" medium="image">
			<media:title type="html">7 Package</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/8-run-project.jpg" medium="image">
			<media:title type="html">8 Run Project</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/9-unloaded.jpg" medium="image">
			<media:title type="html">9 Unloaded</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/10-package-location.jpg" medium="image">
			<media:title type="html">10 Package Location</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/11-powershell.jpg" medium="image">
			<media:title type="html">11 Powershell</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/12-upload-webpart.jpg" medium="image">
			<media:title type="html">12 Upload Webpart</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/13-upload-webpart.jpg" medium="image">
			<media:title type="html">13 Upload Webpart</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/14-upload-webpart.jpg" medium="image">
			<media:title type="html">14 Upload Webpart</media:title>
		</media:content>
	</item>
		<item>
		<title>Install Sharepoint 2010 in a non Server OS</title>
		<link>http://anyrest.wordpress.com/2010/12/14/install-sharepoint-2010-in-a-non-server-os/</link>
		<comments>http://anyrest.wordpress.com/2010/12/14/install-sharepoint-2010-in-a-non-server-os/#comments</comments>
		<pubDate>Tue, 14 Dec 2010 09:00:42 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1172</guid>
		<description><![CDATA[Do you want to develop Visual Web Parts for Sharepoint 2010 but you want it on your Development machine not the Server.  Currently the Installer that you download from Microsoft is set to be used for Windows Servers but with a little change on the installation files you can enable Sharepoint to run on your [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1172&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://anyrest.files.wordpress.com/2010/12/sharepoint-foundation_2010-12-14_10-44-11.jpg"><img class="alignnone size-full wp-image-1173" title="SharePoint Foundation_2010-12-14_10-44-11" src="http://anyrest.files.wordpress.com/2010/12/sharepoint-foundation_2010-12-14_10-44-11.jpg?w=630&#038;h=472" alt="" width="630" height="472" /></a>Do you want to develop Visual Web Parts for Sharepoint 2010 but you want it on your Development machine not the Server.  Currently the Installer that you download from Microsoft is set to be used for Windows Servers but with a little change on the installation files you can enable Sharepoint to run on your Windows 7 desktop.</p>
<p>To enable that you have to follow the steps below</p>
<p>1. Once you download the SharepointFoundation.exe you can extract the installation files by running this on the command prompt</p>
<pre>c:\YourDownloadFolder\SharepointFoundation /extract:c:\SharepointFoundationFiles</pre>
<p>Note do not use other extractor such as 7-Zip beacause you will get this error</p>
<p><span style="color:#ff0000;">&#8220;The language of this installation package is not supported by your system&#8221;</span></p>
<p><span style="color:#ff0000;"><a href="http://anyrest.files.wordpress.com/2010/12/setup-error_2010-12-14_11-28-40.jpg"><img class="alignnone size-full wp-image-1174" title="Setup Error_2010-12-14_11-28-40" src="http://anyrest.files.wordpress.com/2010/12/setup-error_2010-12-14_11-28-40.jpg?w=630" alt=""   /></a><br />
</span></p>
<p>This happens as the folder structure will not be extracted correctly.</p>
<p>2. Now edit the config.xml in  c:\SharepointFoundationFiles\files\Setup\config.xml and add this line</p>
<pre style="font-family:consolas;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">Setting</span><span style="color:blue;"> </span><span style="color:red;">Id</span><span style="color:blue;">=</span>"<span style="color:blue;">AllowWindowsClientInstall</span>"<span style="color:blue;"> </span><span style="color:red;">Value</span><span style="color:blue;">=</span>"<span style="color:blue;">True</span>"<span style="color:blue;">/&gt;</span></pre>
<p>inside the</p>
<pre style="font-family:consolas;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">Configuration</span><span style="color:blue;">&gt;</span> <span style="color:blue;">
</span></pre>
<p>tag:</p>
<p>So the who file will now look like</p>
<pre style="font-family:consolas;"><span style="color:blue;">&lt;</span><span style="color:#a31515;">Configuration</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">Package</span><span style="color:blue;"> </span><span style="color:red;">Id</span><span style="color:blue;">=</span>"<span style="color:blue;">sts</span>"<span style="color:blue;">&gt;</span>
<span style="color:blue;">    &lt;</span><span style="color:#a31515;">Setting</span><span style="color:blue;"> </span><span style="color:red;">Id</span><span style="color:blue;">=</span>"<span style="color:blue;">SETUPTYPE</span>"<span style="color:blue;"> </span><span style="color:red;">Value</span><span style="color:blue;">=</span>"<span style="color:blue;">CLEAN_INSTALL</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">  &lt;/</span><span style="color:#a31515;">Package</span><span style="color:blue;">&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">DATADIR</span><span style="color:blue;"> </span><span style="color:red;">Value</span><span style="color:blue;">=</span>"<span style="color:blue;">%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\Data</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">Logging</span><span style="color:blue;"> </span><span style="color:red;">Type</span><span style="color:blue;">=</span>"<span style="color:blue;">verbose</span>"<span style="color:blue;"> </span><span style="color:red;">Path</span><span style="color:blue;">=</span>"<span style="color:blue;">%temp%</span>"<span style="color:blue;"> </span><span style="color:red;">Template</span><span style="color:blue;">=</span>"<span style="color:blue;">Microsoft SharePoint Foundation 2010 Setup *.log</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">Setting</span><span style="color:blue;"> </span><span style="color:red;">Id</span><span style="color:blue;">=</span>"<span style="color:blue;">UsingUIInstallMode</span>"<span style="color:blue;"> </span><span style="color:red;">Value</span><span style="color:blue;">=</span>"<span style="color:blue;">1</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">Setting</span><span style="color:blue;"> </span><span style="color:red;">Id</span><span style="color:blue;">=</span>"<span style="color:blue;">SETUP_REBOOT</span>"<span style="color:blue;"> </span><span style="color:red;">Value</span><span style="color:blue;">=</span>"<span style="color:blue;">Never</span>"<span style="color:blue;"> /&gt;</span>
<span style="color:blue;">  &lt;</span><span style="color:#a31515;">Setting</span><span style="color:blue;"> </span><span style="color:red;">Id</span><span style="color:blue;">=</span>"<span style="color:blue;">AllowWindowsClientInstall</span>"<span style="color:blue;"> </span><span style="color:red;">Value</span><span style="color:blue;">=</span>"<span style="color:blue;">True</span>"<span style="color:blue;">/&gt;</span>
<span style="color:blue;">&lt;/</span><span style="color:#a31515;">Configuration</span><span style="color:blue;">&gt;</span></pre>
<p>3. Save the configuration file and your good to go.  All you need to do is install the prerequisites if you havent and run the c:\SharepointFoundationFiles\Setup.exe</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1172&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2010/12/14/install-sharepoint-2010-in-a-non-server-os/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/sharepoint-foundation_2010-12-14_10-44-11.jpg" medium="image">
			<media:title type="html">SharePoint Foundation_2010-12-14_10-44-11</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/setup-error_2010-12-14_11-28-40.jpg" medium="image">
			<media:title type="html">Setup Error_2010-12-14_11-28-40</media:title>
		</media:content>
	</item>
		<item>
		<title>Guide in developing Initiation Form on Sharepoint Workflows</title>
		<link>http://anyrest.wordpress.com/2010/12/03/guide-in-developing-initiation-form-on-sharepoint-workflows/</link>
		<comments>http://anyrest.wordpress.com/2010/12/03/guide-in-developing-initiation-form-on-sharepoint-workflows/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 10:00:44 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint Foundation 2010]]></category>
		<category><![CDATA[Workflows]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1144</guid>
		<description><![CDATA[Have you ever wondered whats the use of Initiation Forms on Sharepoint? Did you ever thought of creating a list item based on an item on another list? Do you want to extend the 2 buttons in Manual Workflow Initiation to have input forms?  Well that&#8217;s the use of the Initiation Forms and I will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1144&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered whats the use of Initiation Forms on Sharepoint? Did you ever thought of creating a list item based on an item on another list? Do you want to extend the 2 buttons in Manual Workflow Initiation to have input forms?  Well that&#8217;s the use of the Initiation Forms and I will run you though a sample that will work for you.</p>
<p>So what is an Initiation form?  It simply a page that is presented when you start a workflow manually, the page where you see two buttons for &#8220;Start&#8221; and &#8220;Cancel&#8221;.  By default you have those buttons but you can extend this page by adding form fields and use the value on those as variables that you can consume for your workflow process.</p>
<p>So lets start with a sample, lets say you have the requirement to have an Office Supply list where your team can place an order on a specific item and that invokes a workflow on the backed, making a task list grabbing important details like Name of requester, quantity and the Item requested.  Here are the steps to achieve it.</p>
<p>1. Create an &#8220;Office Supplies&#8221; List<br />
<a href="http://anyrest.files.wordpress.com/2010/12/01.jpg"><img class="alignnone size-full wp-image-1145" title="01" src="http://anyrest.files.wordpress.com/2010/12/01.jpg?w=630&#038;h=346" alt="" width="630" height="346" /></a></p>
<p>2. Now using Sharepoint Designer, go to your list and create a workflow.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/02.jpg"><img class="alignnone size-full wp-image-1146" title="02" src="http://anyrest.files.wordpress.com/2010/12/02.jpg?w=630&#038;h=377" alt="" width="630" height="377" /></a></p>
<p>3. Name your workflow, I called it &#8220;Supply Request Workflow&#8221;<br />
<a href="http://anyrest.files.wordpress.com/2010/12/03.jpg"><img class="alignnone size-full wp-image-1147" title="03" src="http://anyrest.files.wordpress.com/2010/12/03.jpg?w=630" alt=""   /></a></p>
<p>4. Now Save and Publish your workflow and make sure it created a Task List and History List<br />
<a href="http://anyrest.files.wordpress.com/2010/12/04.jpg"><img class="alignnone size-full wp-image-1148" title="04" src="http://anyrest.files.wordpress.com/2010/12/04.jpg?w=630" alt=""   /></a></p>
<p>5. Double check if you have the Task List by going to your Sharepoint instance<br />
<a href="http://anyrest.files.wordpress.com/2010/12/05.jpg"><img class="alignnone size-full wp-image-1149" title="05" src="http://anyrest.files.wordpress.com/2010/12/05.jpg?w=630" alt=""   /></a></p>
<p>6. Now update that task list to cater for the following new fields. Item Requested, Quantity and Requested By.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/06.jpg"><img class="alignnone size-full wp-image-1150" title="06" src="http://anyrest.files.wordpress.com/2010/12/06.jpg?w=630&#038;h=395" alt="" width="630" height="395" /></a></p>
<p>7. Back to Sharepoint Designer and edit again your workflow<br />
<a href="http://anyrest.files.wordpress.com/2010/12/07.jpg"><img class="alignnone size-full wp-image-1151" title="07" src="http://anyrest.files.wordpress.com/2010/12/07.jpg?w=630" alt=""   /></a></p>
<p>8. Now create an Initiation Form by clicking the Initiation Form Parameters<br />
<a href="http://anyrest.files.wordpress.com/2010/12/08.jpg"><img class="alignnone size-full wp-image-1152" title="08" src="http://anyrest.files.wordpress.com/2010/12/08.jpg?w=630&#038;h=184" alt="" width="630" height="184" /></a></p>
<p>9. Now add the field, this is the fields that will populate your Task List as well on step 6, so create the same fields and its format you have on the said step.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/09.jpg"><img class="alignnone size-full wp-image-1153" title="09" src="http://anyrest.files.wordpress.com/2010/12/09.jpg?w=630" alt=""   /></a></p>
<p>10. Once done, time for you to set up an Action on Step 1, and we need to create a &#8220;Create List Item&#8221; action<br />
<a href="http://anyrest.files.wordpress.com/2010/12/10.jpg"><img class="alignnone size-full wp-image-1154" title="10" src="http://anyrest.files.wordpress.com/2010/12/10.jpg?w=630" alt=""   /></a></p>
<p>11. Now choose the property &#8220;this list&#8221;.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/11.jpg"><img class="alignnone size-full wp-image-1155" title="11" src="http://anyrest.files.wordpress.com/2010/12/11.jpg?w=630" alt=""   /></a></p>
<p>12. Now choose the &#8220;Association: Task List&#8221;<br />
<a href="http://anyrest.files.wordpress.com/2010/12/12.jpg"><img class="alignnone size-full wp-image-1156" title="12" src="http://anyrest.files.wordpress.com/2010/12/12.jpg?w=630" alt=""   /></a></p>
<p>13. Modify the Value sent to those fields<br />
<a href="http://anyrest.files.wordpress.com/2010/12/13.jpg"><img class="alignnone size-full wp-image-1157" title="13" src="http://anyrest.files.wordpress.com/2010/12/13.jpg?w=630" alt=""   /></a></p>
<p>14. For example in the Title Field (&#8220;of the Task List&#8221;) you want to use the Current Item which is the &#8220;Office Supply List&#8221; Name.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/14.jpg"><img class="alignnone size-full wp-image-1158" title="14" src="http://anyrest.files.wordpress.com/2010/12/14.jpg?w=630" alt=""   /></a></p>
<p>15. You can add more strings of your choice if you want, so since this is a new request we make the title &#8220;New Item Requested : Your Item&#8221;.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/15.jpg"><img class="alignnone size-full wp-image-1159" title="15" src="http://anyrest.files.wordpress.com/2010/12/15.jpg?w=630" alt=""   /></a></p>
<p>16. Now for other fields like Quantity you can grab it from the initiation form you created in Step 8.  To do that just choose &#8220;Workflow Variables and Parameters&#8221; in Data Source and you will see the fields you created of on the &#8220;Field from source&#8221;.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/16.jpg"><img class="alignnone size-full wp-image-1160" title="16" src="http://anyrest.files.wordpress.com/2010/12/16.jpg?w=630" alt=""   /></a></p>
<p>17. Once all values are properly assigned click OK.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/17.jpg"><img class="alignnone size-full wp-image-1161" title="17" src="http://anyrest.files.wordpress.com/2010/12/17.jpg?w=630" alt=""   /></a></p>
<p>18. Now Save and Publish again.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/18.jpg"><img class="alignnone size-full wp-image-1162" title="18" src="http://anyrest.files.wordpress.com/2010/12/18.jpg?w=630&#038;h=228" alt="" width="630" height="228" /></a></p>
<p>19. Now usually manual initiation workflow starts by hitting the Workflows Button then choosing the appropriate workflow to run.  To make it a bit user-friendly I guess we will have to create a button on the Display Form Ribbon of an item.  Do that by clicking Custom Action and choose &#8220;Display Form Ribbon&#8221;.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/19.jpg"><img class="alignnone size-full wp-image-1163" title="19" src="http://anyrest.files.wordpress.com/2010/12/19.jpg?w=630&#038;h=330" alt="" width="630" height="330" /></a></p>
<p>20. Now name you button, I called it Request now.  And set it to initiate your &#8220;Supply Request Workflow&#8221;.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/20.jpg"><img class="alignnone size-full wp-image-1164" title="20" src="http://anyrest.files.wordpress.com/2010/12/20.jpg?w=630" alt=""   /></a></p>
<p>21. Add your icons.  Then your done.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/21.jpg"><img class="alignnone size-full wp-image-1165" title="21" src="http://anyrest.files.wordpress.com/2010/12/21.jpg?w=630" alt=""   /></a></p>
<p>22. Open now a list item on &#8220;Office Supplies&#8221; list, then you will see your &#8220;Request Now&#8221; button.  Click that.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/22.jpg"><img class="alignnone size-full wp-image-1166" title="22" src="http://anyrest.files.wordpress.com/2010/12/22.jpg?w=630&#038;h=310" alt="" width="630" height="310" /></a></p>
<p>23. Now you see your &#8220;Supply Request Workflow&#8221; with the input forms you had defined.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/23.jpg"><img class="alignnone size-full wp-image-1167" title="23" src="http://anyrest.files.wordpress.com/2010/12/23.jpg?w=630" alt=""   /></a></p>
<p>24. Click start to initiate the request, and you will see it on the &#8220;Task List&#8221;<br />
<a href="http://anyrest.files.wordpress.com/2010/12/24.jpg"><img class="alignnone size-full wp-image-1168" title="24" src="http://anyrest.files.wordpress.com/2010/12/24.jpg?w=630&#038;h=150" alt="" width="630" height="150" /></a></p>
<p>25. With the fields populated as configured.<br />
<a href="http://anyrest.files.wordpress.com/2010/12/25.jpg"><img class="alignnone size-full wp-image-1169" title="25" src="http://anyrest.files.wordpress.com/2010/12/25.jpg?w=630&#038;h=560" alt="" width="630" height="560" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1144&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2010/12/03/guide-in-developing-initiation-form-on-sharepoint-workflows/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/01.jpg" medium="image">
			<media:title type="html">01</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/02.jpg" medium="image">
			<media:title type="html">02</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/03.jpg" medium="image">
			<media:title type="html">03</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/04.jpg" medium="image">
			<media:title type="html">04</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/05.jpg" medium="image">
			<media:title type="html">05</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/06.jpg" medium="image">
			<media:title type="html">06</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/07.jpg" medium="image">
			<media:title type="html">07</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/08.jpg" medium="image">
			<media:title type="html">08</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/09.jpg" medium="image">
			<media:title type="html">09</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/10.jpg" medium="image">
			<media:title type="html">10</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/11.jpg" medium="image">
			<media:title type="html">11</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/12.jpg" medium="image">
			<media:title type="html">12</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/13.jpg" medium="image">
			<media:title type="html">13</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/14.jpg" medium="image">
			<media:title type="html">14</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/15.jpg" medium="image">
			<media:title type="html">15</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/16.jpg" medium="image">
			<media:title type="html">16</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/17.jpg" medium="image">
			<media:title type="html">17</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/18.jpg" medium="image">
			<media:title type="html">18</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/19.jpg" medium="image">
			<media:title type="html">19</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/20.jpg" medium="image">
			<media:title type="html">20</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/21.jpg" medium="image">
			<media:title type="html">21</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/22.jpg" medium="image">
			<media:title type="html">22</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/23.jpg" medium="image">
			<media:title type="html">23</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/24.jpg" medium="image">
			<media:title type="html">24</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/12/25.jpg" medium="image">
			<media:title type="html">25</media:title>
		</media:content>
	</item>
		<item>
		<title>Create Alerts for SQL Server Mirroring Failovers</title>
		<link>http://anyrest.wordpress.com/2010/11/25/create-alerts-for-sql-server-mirroring-failovers/</link>
		<comments>http://anyrest.wordpress.com/2010/11/25/create-alerts-for-sql-server-mirroring-failovers/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 09:00:47 +0000</pubDate>
		<dc:creator>rsmacaalay</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Alerts]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://anyrest.wordpress.com/?p=1132</guid>
		<description><![CDATA[You might have SQL Mirroring enabled in your Enterprise but might not have alerting enabled or you don&#8217;t know how to enable it.  If you want to enable that functionality just follow this 2 easy steps. 1. Creating SQL Alerts in SQL Server Agent Go to your SQL Server Agent and on the Alert section, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1132&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You might have SQL Mirroring enabled in your Enterprise but might not have alerting enabled or you don&#8217;t know how to enable it.  If you want to enable that functionality just follow this 2 easy steps.</p>
<h2><span style="color:#3366ff;">1. Creating SQL Alerts in SQL Server Agent</span></h2>
<p>Go to your SQL Server Agent and on the Alert section, right-click on it and choose New Alert.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/1-create-alert.jpg"><img class="alignnone size-full wp-image-1133" title="1 Create Alert" src="http://anyrest.files.wordpress.com/2010/11/1-create-alert.jpg?w=630" alt=""   /></a></p>
<p>Now you are presented with the alert properties and just fill it up with the proper information<br />
<strong>Name</strong> :  Choose a name for this Alert<br />
<strong>Type </strong>:  There are 3 different types and since were interested in Database Mirroring State Change we need to query WMI so choose WMI event alert<br />
<strong>Namespace </strong>: The namespace it auto populated to the namespace you needed so don&#8217;t change it<br />
<strong>Query </strong>: Now write your WMI Query.  We are looking for the DATABASE_MIRRORING_STATE_CHANGE class so your query goes like this.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/2-create-alert.jpg"><img class="alignnone size-full wp-image-1134" title="2 Create Alert" src="http://anyrest.files.wordpress.com/2010/11/2-create-alert.jpg?w=630&#038;h=277" alt="" width="630" height="277" /></a></p>
<pre>SELECT * FROM DATABASE_MIRRORING_STATE_CHANGE</pre>
<p>Now there 19 Properties for that class and you are mainly interested on DatabaseName or DatabaseID and State.  DatabaseName or DatabaseID will be the database you are monitoring and State would be Mirroring State of the Database and here are the different values.</p>
<ul>
<li>0 = Null Notification</li>
<li>1 = Synchronized Principal with Witness</li>
<li>2 = Synchronized Principal without Witness</li>
<li>3 = Synchronized Mirror with Witness</li>
<li>4 = Synchronized Mirror without Witness</li>
<li>5 = Connection with Principal Lost</li>
<li>6 = Connection with Mirror Lost</li>
<li>7 = Manual Failover</li>
<li>8 = Automatic Failover</li>
<li>9 = Mirroring Suspended</li>
<li>10 = No Quorum</li>
<li>11 = Synchronizing Mirror</li>
<li>12 = Principal Running Exposed</li>
</ul>
<p><a href="http://anyrest.files.wordpress.com/2010/11/2-wmi.jpg"><img class="alignnone size-full wp-image-1135" title="2 WMI" src="http://anyrest.files.wordpress.com/2010/11/2-wmi.jpg?w=630" alt=""   /></a></p>
<p>Having that in mind you can now filter out alerts by those two properties, so for example I want to have an alert for &#8220;SAMPLE&#8221; Database where the mirroring Failed Over my query would look like this</p>
<pre>SELECT * FROM DATABASE_MIRRORING_STATE_CHANGE WHERE DatabaseName = 'SAMPLE' AND State = 8</pre>
<p>Now you have an alert you need now to define notifications for operators, you can do that by going to the Response tab and choose who do you want to notify and what notifications they want to receive.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/3-create-alert.jpg"><img class="alignnone size-full wp-image-1136" title="3 Create Alert" src="http://anyrest.files.wordpress.com/2010/11/3-create-alert.jpg?w=630&#038;h=286" alt="" width="630" height="286" /></a></p>
<p>Now you define additional options such as message you want to send to those operator</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/4-create-alert.jpg"><img class="alignnone size-full wp-image-1137" title="4 Create Alert" src="http://anyrest.files.wordpress.com/2010/11/4-create-alert.jpg?w=630&#038;h=288" alt="" width="630" height="288" /></a></p>
<p>At this stage you have a working alert.  But it might not work as by default the SQL Server Agent Alert System Mail Profile is disabled</p>
<h2><span style="color:#3366ff;">2. Activating SQL Server Agent Alert System Mail Profile.</span></h2>
<p>Go to SQL Server Agents properties</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/5-verify-agent.jpg"><img class="alignnone size-full wp-image-1138" title="5 Verify Agent" src="http://anyrest.files.wordpress.com/2010/11/5-verify-agent.jpg?w=630" alt=""   /></a></p>
<p>Then go to Alert System Tab then tick Enable mail profile and choose the Mail system installed on your server as well as its profile</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/6-verify-agent.jpg"><img class="alignnone size-full wp-image-1139" title="6 Verify Agent" src="http://anyrest.files.wordpress.com/2010/11/6-verify-agent.jpg?w=630&#038;h=304" alt="" width="630" height="304" /></a></p>
<p>Now your done, all you have to do is test, You will get a message similar to this once it&#8217;s activated.</p>
<p><a href="http://anyrest.files.wordpress.com/2010/11/7-email.jpg"><img class="alignnone size-full wp-image-1140" title="7 Email" src="http://anyrest.files.wordpress.com/2010/11/7-email.jpg?w=630" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anyrest.wordpress.com/1132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anyrest.wordpress.com/1132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anyrest.wordpress.com/1132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anyrest.wordpress.com/1132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anyrest.wordpress.com/1132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anyrest.wordpress.com/1132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anyrest.wordpress.com/1132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anyrest.wordpress.com/1132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anyrest.wordpress.com&amp;blog=11536026&amp;post=1132&amp;subd=anyrest&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anyrest.wordpress.com/2010/11/25/create-alerts-for-sql-server-mirroring-failovers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7cac4a8f1c5c069fb5a42526fc8a634c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rsmacaalay</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/1-create-alert.jpg" medium="image">
			<media:title type="html">1 Create Alert</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/2-create-alert.jpg" medium="image">
			<media:title type="html">2 Create Alert</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/2-wmi.jpg" medium="image">
			<media:title type="html">2 WMI</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/3-create-alert.jpg" medium="image">
			<media:title type="html">3 Create Alert</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/4-create-alert.jpg" medium="image">
			<media:title type="html">4 Create Alert</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/5-verify-agent.jpg" medium="image">
			<media:title type="html">5 Verify Agent</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/6-verify-agent.jpg" medium="image">
			<media:title type="html">6 Verify Agent</media:title>
		</media:content>

		<media:content url="http://anyrest.files.wordpress.com/2010/11/7-email.jpg" medium="image">
			<media:title type="html">7 Email</media:title>
		</media:content>
	</item>
	</channel>
</rss>
