<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Haytham speaks from Microsoft</title><link>http://blogs.msdn.com/b/haythamalaa/</link><description>Technical posts about Sharepoint and/or Microsoft in general</description><dc:language>en-US</dc:language><generator>Telligent Community 5.6.583.14036 (Build: 5.6.583.14036)</generator><item><title>Writing proper async code</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2011/06/12/writing-proper-async-code.aspx</link><pubDate>Sun, 12 Jun 2011 23:03:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10173774</guid><dc:creator>Kasparov</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=10173774</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=10173774</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2011/06/12/writing-proper-async-code.aspx#comments</comments><description>&lt;p&gt;Alsalam alikom wa ra7mat Allah wa barakatoh (aka peace upon you)&lt;/p&gt;  &lt;p&gt;Asynchronous calls have always been there for more efficient use of CPU time. You run something asynchronously when you know it’ll take some time to execute and you don’t want to block your current thread while waiting for that code (In some cases you actually do want to block the thread but then spanning another thread is, in this case, more or less a waste of resources In My Humble Opinion).&lt;/p&gt;  &lt;p&gt;For the purpose of this post, let’s take as an example downloading a remote file, and printing the contents of the file to screen.&lt;/p&gt;  &lt;p&gt;I would imagine this as one way to implement it:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;TestSync()
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Starting Test() &amp;quot; &lt;/span&gt;+ GetCurrentContext());
    WriteLinePageTitle(DownloadStringSlowNetworkSync(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://sample.com&amp;quot;&lt;/span&gt;)));
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Test() is done with big delay &amp;quot; &lt;/span&gt;+ GetCurrentContext());
}

&lt;span style="color: blue"&gt;private string &lt;/span&gt;DownloadStringSlowNetworkSync(&lt;span style="color: #2b91af"&gt;Uri &lt;/span&gt;uri)
{
    &lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(500);
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebClient&lt;/span&gt;().DownloadString(uri);
}&lt;/pre&gt;&lt;/span&gt;
This is an example for a synchronous operation. TestSync() will not return till the string is downloaded. Here is a run:&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/5658.image_5F00_64F66A02.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/6758.image_5F00_thumb_5F00_51690A61.png" width="459" height="129" /&gt;&lt;/a&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;As you can see, it all ran on the same thread.&lt;/pre&gt;

&lt;pre class="code"&gt;If a UI component is calling TestSync() this means execution will block UI. And that’s the typical use of of Asynchronous method.&lt;/pre&gt;

&lt;pre class="code"&gt;Here is the typical way to implement async:&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;TestAsync1()
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Starting Test() &amp;quot; &lt;/span&gt;+ GetCurrentContext());
    BeginDownloadStringSlowNetworkAsync(
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://sample.com&amp;quot;&lt;/span&gt;),
        (sender, e) =&amp;gt;
        {
            WriteLinePageTitle(e.Result);
        },
        (ex) =&amp;gt;
        {
            WriteLinePageTitle(&lt;span style="color: #a31515"&gt;&amp;quot;Exception is thrown: &amp;quot; &lt;/span&gt;+ ex.Message);
        });
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Test() is done with no delay &amp;quot; &lt;/span&gt;+ GetCurrentContext());
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;BeginDownloadStringSlowNetworkAsync(
    &lt;span style="color: #2b91af"&gt;Uri &lt;/span&gt;uri, 
    &lt;span style="color: #2b91af"&gt;DownloadStringCompletedEventHandler &lt;/span&gt;completedCallback, 
    &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;&amp;gt; errorCallback)
{
    &lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(500);
    &lt;span style="color: blue"&gt;try
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;var &lt;/span&gt;client = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebClient&lt;/span&gt;();
        client.DownloadStringCompleted += completedCallback;
        client.DownloadStringAsync(uri);
    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
    {
        errorCallback(ex);
    }
}&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/2047.image_5F00_38010727.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/0878.image_5F00_thumb_5F00_10C6BDF2.png" width="455" height="92" /&gt;&lt;/a&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;This is a little bit better, now UI is not blocked (notice UI running in thread 1 continued working fine)… &lt;br /&gt;There are 2 down sides of this implementation,&lt;br /&gt;1- Code isn’t intuitive, it’s way different than the counter sync code.&lt;br /&gt;2- Exception handling is not propagating well enough, stack trace isn’t preserved for instance.&lt;/pre&gt;

&lt;pre class="code"&gt;Now with the better version, using &lt;a href="http://msdn.microsoft.com/en-us/vstudio/async.aspx" target="_blank"&gt;&lt;strong&gt;&lt;font size="3"&gt;Async Library&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;.&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public async &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Task &lt;/span&gt;TestAsync2()
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Starting Test() &amp;quot; &lt;/span&gt;+ GetCurrentContext());
    RunAsync();
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Test() is done with no delay &amp;quot; &lt;/span&gt;+ GetCurrentContext());
}

&lt;span style="color: blue"&gt;private async void &lt;/span&gt;RunAsync()
{
    WriteLinePageTitle(&lt;span style="color: blue"&gt;await &lt;/span&gt;DownloadStringTaskSlowNetworkAsync(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://sample.com&amp;quot;&lt;/span&gt;)));
}&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public async &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; DownloadStringTaskSlowNetworkAsync(&lt;span style="color: #2b91af"&gt;Uri &lt;/span&gt;address)
{
    &lt;span style="color: blue"&gt;await &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TaskEx&lt;/span&gt;.Delay(500);    &lt;span style="color: green"&gt;// Simulate 500ms of network delay
    &lt;/span&gt;&lt;span style="color: blue"&gt;return await new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebClient&lt;/span&gt;().DownloadStringTaskAsync(address);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/6747.image_5F00_5E62EA72.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/2626.image_5F00_thumb_5F00_170D9480.png" width="399" height="85" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will notice a couple of things here: 
  &lt;br /&gt;1- The result is identical to the async code we wrote above 

  &lt;br /&gt;2- Code looks intuitive, very similar to sync version 

  &lt;br /&gt;3- There are two new keywords async and await. And that’s what I &lt;em&gt;actually &lt;/em&gt;want to talk about..&lt;/p&gt;

&lt;p&gt;async: you decorate your methods with async to be able to call await inside them.
  &lt;br /&gt;await: you prefix method calls with await when you want the library to create callbacks for you to handle async operation.&lt;/p&gt;

&lt;p&gt;In DownloadStringTaskSlowNetworkAsync() method, we call DownloadStringTaskAsync, which is an extension method included in Async library. What await does is that it creates a callback method that includes the rest of your code (return statement in this case), then calls the method DownloadStringTaskAsync() and registers that callback, when the callback is invoked (ie when the string is actually downloaded), it invokes the rest of your code (return… etc).&lt;/p&gt;

&lt;p&gt;Going one level up, RunAsync(), it awaits on DownloadStringTaskSlowNetworkAsync, which means WriteLinePageTitle will not be called till DownloadStringTaskSlowNetworkAsync returns… till then, RunAsync will just return a void task.&lt;/p&gt;

&lt;p&gt;One more level up, here we called RunAsync without prefixing the call with await, this means, the function will behave exactly like our previous Begin method, it’ll start, and control flow will directly continue to print “Test() is done…”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A few things to note:&lt;/strong&gt;

  &lt;br /&gt;1- The leaves of the calls (DownloadStringTaskAsync… etc) are implemented by Async Library, if you want to implement your own leaf method, it’s a little bit more interesting, I’ll save that for a coming post.

  &lt;br /&gt;2- await doesn’t block current thread, it rather wraps up the rest of code in your method and creates a callback to execute the code after async operation is done.

  &lt;br /&gt;3- If you surrounded an await statement with try,catch, catch statements will be included in that callback, which means you will be able to catch exceptions for async methods the same way you catch exceptions for sync methods&lt;/p&gt;

&lt;p&gt;Last to note, Async Library is available for WP7 as well as WPF/Silverlight… I’ve to tell you, it makes async code WAY easier to write!&lt;/p&gt;

&lt;p&gt;Stay Tuned &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/2548.wlEmoticon_2D00_smile_5F00_08CF1B90.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10173774" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Async/">Async</category></item><item><title>WebParts fail to import for readers? here is a possible fix</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2010/06/09/webparts-fail-to-import-for-readers-here-is-a-possible-fix.aspx</link><pubDate>Wed, 09 Jun 2010 23:42:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10022646</guid><dc:creator>Kasparov</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=10022646</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=10022646</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2010/06/09/webparts-fail-to-import-for-readers-here-is-a-possible-fix.aspx#comments</comments><description>&lt;p&gt;One of our partners faced a problem that I think we will eventually see occurring frequently with external customers, so I thought to document the problem’s symptoms and a possible fix..&lt;/p&gt;  &lt;h3&gt;Problem Context:&lt;/h3&gt;  &lt;ol&gt;   &lt;li&gt;You have a v2 WebPart (didn’t try it with v3 but it’s likely to repro as well), that you deployed to a server. &lt;/li&gt;    &lt;li&gt;You (or some developer who uses your WebPart), creates a feature, with an elements file that tries to deploy that WebPart into a page, something like this:      &lt;br /&gt;      &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Elements&lt;/span&gt; &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;&amp;lt;a href=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;http&lt;/span&gt;://&lt;span style="color: #ff0000"&gt;schemas&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;microsoft&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;com&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;sharepoint&lt;/span&gt;/&amp;quot;&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;http://schemas.microsoft.com/sharepoint/&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Module&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;MyModule&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Url&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Shared Documents&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;File&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Url&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Test.aspx&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Type&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;GhostableInLibrary&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;AllUsersWebPart&lt;/span&gt; &lt;span style="color: #ff0000"&gt;WebPartZoneID&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Header&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;WebPartOrder&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;![CDATA[
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;&lt;span style="color: #0000ff"&gt;?&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;WebPart&lt;/span&gt; &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;xsd&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;&amp;lt;a href=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;http&lt;/span&gt;://&lt;span style="color: #ff0000"&gt;www&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;w3&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;org&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;2001&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;XMLSchema&lt;/span&gt;&amp;quot;&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; xmlns:xsi=&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt; &lt;span style="color: #ff0000"&gt;href&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/span&gt;&amp;quot;&amp;gt;&lt;span style="color: #ff0000"&gt;http&lt;/span&gt;://&lt;span style="color: #ff0000"&gt;www&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;w3&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;org&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;2001&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;XMLSchema&lt;/span&gt;-&lt;span style="color: #ff0000"&gt;instance&lt;/span&gt;&amp;quot;&amp;lt;/&lt;span style="color: #ff0000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; xmlns=&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt; &lt;span style="color: #ff0000"&gt;href&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;http://schemas.microsoft.com/WebPart/v2&amp;quot;&lt;/span&gt;&amp;quot;&amp;gt;&lt;span style="color: #ff0000"&gt;http&lt;/span&gt;://&lt;span style="color: #ff0000"&gt;schemas&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;microsoft&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;com&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;WebPart&lt;/span&gt;/&lt;span style="color: #ff0000"&gt;v2&lt;/span&gt;&amp;quot;&amp;lt;/&lt;span style="color: #ff0000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Title&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;A Test&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Title&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;FrameType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Default&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;FrameType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Description&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;IsIncluded&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;IsIncluded&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ZoneID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Header&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ZoneID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;PartOrder&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;0&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;PartOrder&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;FrameState&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Normal&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;FrameState&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;222px&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;AllowRemove&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;false&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;AllowRemove&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;AllowZoneChange&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;false&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;AllowZoneChange&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;AllowMinimize&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;false&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;AllowMinimize&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;IsVisible&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;IsVisible&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;DetailLink&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;HelpLink&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Dir&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Default&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Dir&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;PartImageSmall&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;MissingAssembly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;PartImageLarge&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;IsIncludedFilter&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;g_A4D135E1_0834_4b88_AA94_E7EC5BA80E93&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Assembly&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;MyWebPartAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Assembly&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;TypeName&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;MyWebParts.WebPart1&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;TypeName&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;WebPart&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;            ]]&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;AllUsersWebPart&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;File&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Module&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Elements&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Login as admin, enable the feature.. do NOT browse to Shared Documents\test.aspx. &lt;/li&gt;

  &lt;li&gt;Login as reader/visitor and try to browse to that page, you will be greeted with an ErrorWebPart saying: 
    &lt;br /&gt;&lt;strong&gt;Error 
      &lt;br /&gt;Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. You don't have Add and Customize Pages permissions required to perform this action. &lt;/strong&gt;&lt;/li&gt;

  &lt;li&gt;If, at this point, you logged in as Admin, the WebPart will render just fine and any reader logs in from now on will render just fine as well &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;The problem: &lt;/h3&gt;

&lt;p&gt;Well, it’s hard to call it a problem, it’s a design decision we took. Sharepoint at that first page visit, will do the WebPart import into the page, and as it’s a reader who tries to do the import, it fails. &lt;/p&gt;

&lt;h3&gt;The solution: &lt;/h3&gt;

&lt;p&gt;The failure (as the text tells) is because the logged in user doesn’t have AddAndCustomizePages permission, this error pops up in different places but the reason for that specific one is because MyWebPart1 is not marked as SafeAgainstScript=True (I’ve a post about it &lt;a href="http://blogs.msdn.com/b/haythamalaa/archive/2009/12/03/contributors-contributors-contributors-sharepoint.aspx" target="_blank"&gt;here&lt;/a&gt;). You don’t want to mark just any WebPart as SafeAgainstScript=True because that can expose you, the site and your users to danger. At this point you have 3 options: &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;If you know for sure that non of your WebPart properties accepts strings from users (or that you do proper encoding/stripping to any script you might get), then go ahead and mark the WebPart as SafeAgainstScript=True in the &amp;lt;SafeControls&amp;gt; entry. &lt;/li&gt;

  &lt;li&gt;If you are unsure, then your best bet is to mark it as SafeAgainstScript=True and put [RequiresDesignerPermissions] attribute on all properties of your WebPart. This will allow readers to import the WebPart but not to change any. &lt;/li&gt;

  &lt;li&gt;Just let your users be aware of this scenario &lt;img style="border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-06-30-metablogapi/1321.wlEmoticon_2D00_smile_5F00_2.png" /&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s it for now… stay tuned!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10022646" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Sharepoint/">Sharepoint</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/SafeAgainstScript/">SafeAgainstScript</category></item><item><title>What should I talk about?</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2009/12/17/what-should-i-talk-about.aspx</link><pubDate>Thu, 17 Dec 2009 18:25:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9938315</guid><dc:creator>Kasparov</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=9938315</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=9938315</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2009/12/17/what-should-i-talk-about.aspx#comments</comments><description>&lt;p&gt;Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace Upon You)&lt;/p&gt;  &lt;p&gt;One of the problems every blogger faces is how to answer the above question… We often ask our fellow friends for suggestions, or even post a whole post and let people suggest topics in the comments… well, no more! skribit (&lt;a title="http://skribit.com/" href="http://skribit.com/"&gt;http://skribit.com/&lt;/a&gt;) tries to find a better solution to that, by subscribing, it gives you a widget code that you can put as a side bar in your blog to let people suggest topics to talk about…&lt;/p&gt;  &lt;p&gt;And I’m starting this right away, I’ve included the suggestions box in this post, please use it to suggest topics for me to talk about… after a while I’ll move it into the sidebar and keep it there for the foreseeable future :)&lt;/p&gt;  &lt;p&gt;Note: if you are using RSS, you might not be able to get the box, please visit the original post link.&lt;/p&gt;  &lt;div id="writeSkribitHere"&gt;&lt;/div&gt;&lt;script src="http://assets.skribit.com/javascripts/SkribitWidget.js?renderTo=writeSkribitHere&amp;amp;blog=57e97897412109ac650e09e944081db3&amp;amp;cnt=5"&gt;&lt;/script&gt;&lt;noscript&gt;Sorry, but the &lt;a href="http://skribit.com" title="Skribit - Cure Writer's Block"&gt;Skribit&lt;/a&gt; widget only works on browsers with JavaScript support.  &lt;a href="http://skribit.com/blogs/haytham-talks-from-microsoft" title="Skribit Suggestions for Haytham talks from Microsoft"&gt;View suggestions for this blog here.&lt;/a&gt;&lt;/noscript&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Thanks, &lt;/p&gt;  &lt;p&gt;Stay tuned ;)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9938315" width="1" height="1"&gt;</description></item><item><title>Contributors, Contributors, Contributors [Sharepoint]</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2009/12/03/contributors-contributors-contributors-sharepoint.aspx</link><pubDate>Thu, 03 Dec 2009 19:14:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932134</guid><dc:creator>Kasparov</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=9932134</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=9932134</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2009/12/03/contributors-contributors-contributors-sharepoint.aspx#comments</comments><description>&lt;p&gt;Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace Upon You)&lt;/p&gt;  &lt;p&gt;I’m planning to start a series of posts, targeting SharePoint developers, introducing some parts of features I got exposed to. I’ll start this by talking about new features we are releasing as part of our security solution in Windows Sharepoint Foundation 2010.&lt;/p&gt;  &lt;p&gt;One of the famous threats in the internet is XSS (Cross Site Scripting [&lt;a href="http://en.wikipedia.org/wiki/Cross-site_scripting"&gt;Wikipedia link&lt;/a&gt;]). Let me describe it in a short scenario,     &lt;br /&gt;BadUser creates a page with a form and some javascript code that posts the form data to BadSite.com, then BadUser uploads that page into GoodSite.com/userPages, now he also got access to whatever information that site provides about its users.     &lt;br /&gt;GoodUser browses to the page in GoodSite.com, fills the form and violla, BadUser gets the information + whatever his script can gather about GoodUser..     &lt;br /&gt;The problem here is that GoodUser didn’t really know that this page will go post data somewhere else… and has no way to know except by using some advanced techniques…&lt;/p&gt;  &lt;p&gt;This can be applied on WebParts, some WebParts allow users to directly inject nonencoded text that gets rendered into WebPart Pages (WikiPages).. eg. ContentEditorWebPart. For this, we have introduced new web.config flag called SafeAgainstScript. which basically if set to false, will prevent non-privileged users from changing properties that might not be encoded.&lt;/p&gt;  &lt;p&gt;And into the details…&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;We, generally, prevent contributors from modifying unsafe properties in WebParts. For that to be flexible, we introduce 2 new flags &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;SafeAgainstScript&lt;/strong&gt; SafeControls [&lt;a href="http://msdn.microsoft.com/en-us/library/ms413697.aspx"&gt;MSDN Link&lt;/a&gt;] flag, setting this flag to false (it’s &lt;strong&gt;&lt;font color="#ff0000"&gt;false&lt;/font&gt;&lt;/strong&gt; by default) on any SafeControl entry, means contributors are not allowed to insert these WebParts into WebPart Pages / Wiki Pages, and if a Designer (a user with Add and Customize Pages permission [&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spsecuritytrimmedcontrol.permissionsstring.aspx"&gt;MSND Link&lt;/a&gt;]) has already added that, then contributors won’t be allowed to modify any of its properties except for the Title. &lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;e.g &lt;/li&gt;      &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 15px; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;SafeControls&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 27px; font-size: 12px"&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;SafeControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Assembly&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXX&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;Namespace&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Microsoft.SharePoint.WebPartPages&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;TypeName&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;TitleBarWebPart&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Safe&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;True&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;AllowRemoteDesigner&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;True&amp;quot;&lt;/span&gt; &lt;strong&gt;&lt;span style="color: #ff0000"&gt;SafeAgainstScript&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;True&amp;quot;&lt;/span&gt;&lt;/strong&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;SafeControls&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;
  &lt;/ul&gt;

  &lt;li&gt;&lt;strong&gt;RequiresDesignerPermission&lt;/strong&gt; property-level attribute. It comes in effect only when a WebPart is marked as &lt;strong&gt;SafeAgainstScript=True&lt;/strong&gt; in Web.Config SafeControls entries, WebPart developers can use it to say “even though this WebPart is safe, these specific properties are not” &lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;e.g &lt;/li&gt;

    &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 14px; font-size: 12px"&gt;[RequiresDesignerPermission] 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 17px; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Content 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 16px; font-size: 12px"&gt;{ 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 15px; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _content; } 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; height: 14px; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { _content = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; } 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;} &lt;/pre&gt;&lt;/pre&gt;
  &lt;/ul&gt;

  &lt;li&gt;Here is a matrix that explains which flag will have the upper hand in different situations: 
    &lt;ul&gt;
      &lt;li&gt;SafeAgainstScript=&lt;strong&gt;False&lt;/strong&gt; 

        &lt;table border="1" cellspacing="0" cellpadding="2" width="477"&gt;&lt;tbody&gt;
            &lt;tr&gt;
              &lt;td valign="top" width="135"&gt;&amp;#160;&lt;/td&gt;

              &lt;td valign="top" width="131"&gt;Contributors&lt;/td&gt;

              &lt;td valign="top" width="209"&gt;Designers &amp;amp; higher&lt;/td&gt;
            &lt;/tr&gt;

            &lt;tr&gt;
              &lt;td valign="top" width="135"&gt;RequiresDesignerPermission=True&lt;/td&gt;

              &lt;td valign="top" width="131"&gt;&lt;strong&gt;No Access&lt;/strong&gt;&lt;/td&gt;

              &lt;td valign="top" width="209"&gt;Can Access&lt;/td&gt;
            &lt;/tr&gt;

            &lt;tr&gt;
              &lt;td valign="top" width="135"&gt;RequiresDesignerPermission=False&lt;/td&gt;

              &lt;td valign="top" width="131"&gt;&lt;strong&gt;No Access&lt;/strong&gt;&lt;/td&gt;

              &lt;td valign="top" width="209"&gt;Can Access&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;&lt;/table&gt;

        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;ul&gt;
      &lt;li&gt;SafeAgainstScript&lt;strong&gt;=True&lt;/strong&gt; 

        &lt;table border="1" cellspacing="0" cellpadding="2" width="471"&gt;&lt;tbody&gt;
            &lt;tr&gt;
              &lt;td valign="top" width="133"&gt;&amp;#160;&lt;/td&gt;

              &lt;td valign="top" width="133"&gt;Contributors&lt;/td&gt;

              &lt;td valign="top" width="203"&gt;Designers &amp;amp; higher&lt;/td&gt;
            &lt;/tr&gt;

            &lt;tr&gt;
              &lt;td valign="top" width="133"&gt;RequiresDesignerPermision=True&lt;/td&gt;

              &lt;td valign="top" width="133"&gt;Can Access&lt;/td&gt;

              &lt;td valign="top" width="203"&gt;Can Access&lt;/td&gt;
            &lt;/tr&gt;

            &lt;tr&gt;
              &lt;td valign="top" width="133"&gt;RequiresDesignerPermission=False&lt;/td&gt;

              &lt;td valign="top" width="133"&gt;&lt;strong&gt;No Access&lt;/strong&gt;&lt;/td&gt;

              &lt;td valign="top" width="203"&gt;Can Access&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;&lt;/table&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;For the built-in WebParts, we have taken the effort to hide the properties from the toolpane in situations where users have No Access to modify them. For custom WebParts, developers have two choices 
    &lt;ol&gt;
      &lt;li&gt;Use our built in ToolPane provider, and we will take care of hiding and showing these properties. &lt;/li&gt;

      &lt;li&gt;If you need to provide a custom ToolPane, you will need to take care of that. (I understand we need to improve at this point to provide you with the public APIs to make your life easier) &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I’ll put together a sample custom WebPart that demonstrate these new flags, and will update this post once I get it uploaded.&lt;/p&gt;

&lt;p&gt;Stay Tuned!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932134" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Sharepoint/">Sharepoint</category></item><item><title>Invent your own language… using Oslo Part II</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2009/05/24/invent-your-own-language-using-oslo-part-ii.aspx</link><pubDate>Sun, 24 May 2009 21:54:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9639251</guid><dc:creator>Kasparov</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=9639251</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=9639251</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2009/05/24/invent-your-own-language-using-oslo-part-ii.aspx#comments</comments><description>&lt;p&gt;Alsalam alikom wa ra7mat Allah wa barakatoh (Peace Upon You)&lt;/p&gt;  &lt;p align="center"&gt;&lt;strong&gt;&lt;font size="5"&gt;Part II: Consume the Abstract Syntax Tree        &lt;br /&gt;&lt;/font&gt;&lt;/strong&gt;&lt;em&gt;&lt;font size="2"&gt;… Do some action! …&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;If you have not read &lt;a href="http://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" target="_blank"&gt;Part I&lt;/a&gt;, we have created our first grammar to recognize this language:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Send &lt;span style="color: #006080"&gt;&amp;quot;D:\Reports\Templates\Regular.tmpl&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;to&lt;/span&gt; sm1@hotmail.com&lt;br /&gt;Send &lt;span style="color: #006080"&gt;&amp;quot;D:\Reports\Templates\Special.tmpl&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;to&lt;/span&gt; sm2@hotmail.com,sm3@hotmail.com&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Visit &lt;a href="http://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" target="_blank"&gt;here&lt;/a&gt; [Part I link] to find the reporter.mg file listing at the end.&lt;/p&gt;

&lt;p&gt;In this post, we will explore how to compile/parse the generated tree programmatically. And we will dig a little bit one cool way to do .NET code generation works.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a new project (You can pick a console one for simplicity), Add Reference to: 
    &lt;br /&gt;C:\Program Files\Microsoft Oslo SDK 1.0\Bin\Microsoft.M.Grammar.dll and 

    &lt;br /&gt;C:\Program Files\Microsoft Oslo SDK 1.0\Bin\System.Dataflow.dll 

    &lt;br /&gt;

    &lt;br /&gt;Microsoft.M.Grammar.dll contains MGrammarCompiler which we will use to compile our .mg file into .mgx (just as we used mg.exe tool) 

    &lt;br /&gt;

    &lt;br /&gt;System.Dataflow.dll contains DynamicParser class which will be our starting point to parse the generated AST. 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;This is the compile function: 
    &lt;div&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Compile()&lt;br /&gt;{&lt;br /&gt;    MGrammarCompiler compiler = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MGrammarCompiler();&lt;br /&gt;    compiler.FileNames = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] { &lt;br /&gt;        &lt;span style="color: #006080"&gt;@&amp;quot;C:\Users\HaythamAlaa\Documents\reporter.mg&amp;quot;&lt;/span&gt; };&lt;br /&gt;    compiler.OutFile = &lt;span style="color: #006080"&gt;&amp;quot;reporter.mgx&amp;quot;&lt;/span&gt;;&lt;br /&gt;    compiler.Execute(ErrorReporter.Standard);&lt;br /&gt;}&lt;/pre&gt;
    &lt;/div&gt;

    &lt;div&gt;
      &lt;br /&gt;Basically, we pass the fileName(s) and output file then just call Execute and you are done. 

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div&gt;Then, we add a Parse method: 
      &lt;br /&gt;&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Parse()&lt;br /&gt;{&lt;br /&gt;    FileStream mgxFile = File.OpenRead(&lt;span style="color: #006080"&gt;&amp;quot;reporter.mgx&amp;quot;&lt;/span&gt;);&lt;br /&gt;    DynamicParser parser = DynamicParser.LoadFromMgx(mgxFile, &lt;br /&gt;                                &lt;span style="color: #006080"&gt;&amp;quot;Basic.Languages.Reporter&amp;quot;&lt;/span&gt;);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; root = parser.Parse&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;(&lt;br /&gt;            &lt;span style="color: #006080"&gt;@&amp;quot;C:\Users\HaythamAlaa\Documents\reportsData.bundle&amp;quot;&lt;/span&gt;, &lt;br /&gt;            &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;, ErrorReporter.Standard);&lt;br /&gt;    IGraphBuilder graphBuilder = parser.GraphBuilder;&lt;br /&gt;&lt;br /&gt;    Traverse(root, graphBuilder);&lt;br /&gt;&lt;/pre&gt;
DynamicParser loads the mgx file we compiled earlier, and then we ask it to parse the bundle file… note that parser.Parse&amp;lt;object&amp;gt; object is obligatory (I believe this will be refactored in the release) 

      &lt;br /&gt;

      &lt;br /&gt;As all nodes returned of type object, parser provides an IGraphBuilder object that helps us traverse and query the AST. 

      &lt;br /&gt;

      &lt;br /&gt;Traverse method should just loop over all nodes (and sub nodes) and output their values. 

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div&gt;Traverse is very straight forward. However, there is a couple of tricks we need to be aware of while traversing the tree… 
      &lt;br /&gt;First, here is the basic Traverse Method:&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Traverse(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; root, IGraphBuilder graphBuilder)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (root &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(root.ToString());&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;-&amp;gt;&amp;quot;&lt;/span&gt; + graphBuilder.GetLabel(root));&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; childNode &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; graphBuilder.GetSuccessors(root))&lt;br /&gt;    {&lt;br /&gt;        Traverse(childNode, graphBuilder);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;The output should be like this:&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;-&amp;gt;Commands&lt;br /&gt;-&amp;gt;Send&lt;br /&gt;D:\Reports\Templates\Regular.tmpl&lt;br /&gt;-&amp;gt;Emails&lt;br /&gt;sm1@hotmail.com&lt;br /&gt;-&amp;gt;Send&lt;br /&gt;D:\Reports\Templates\Special.tmpl&lt;br /&gt;-&amp;gt;Emails&lt;br /&gt;sm2@hotmail.com&lt;br /&gt;&lt;a href="mailto:sm3@hotmail.com"&gt;sm3@hotmail.com&lt;/a&gt;&lt;/pre&gt;

      &lt;br /&gt;First thing to notice, this is a simple Depth First Search (or Visitor Pattern as architects like to call it). 

      &lt;br /&gt;

      &lt;br /&gt;Second, The leaf nodes of the tree are all strings, the non-leaf ones can be of type SimpleNode or SequenceNode (or something else?) depending on how they were projected in MGrammar. 

      &lt;br /&gt;e.g:&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;=&amp;gt; Send{. . .};&lt;/pre&gt;
Would produce: 

      &lt;br /&gt;&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Send{&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;
That will be parsed as &lt;strong&gt;SimpleNode&lt;/strong&gt; 

      &lt;br /&gt;

      &lt;br /&gt;While,&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;=&amp;gt; Send[. . .];&lt;/pre&gt;
Would produce:&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Send[&lt;br /&gt;.....&lt;br /&gt;]&lt;/pre&gt;
And will be parsed as &lt;strong&gt;SequenceNode&lt;/strong&gt; 

      &lt;br /&gt;

      &lt;br /&gt;I didn’t read any recommendation on when to use what… but I personally prefer to use {} when you are thinking of a Class and use [] when you are thinking of an Array… more of this will come later when we start generating code. 

      &lt;br /&gt;

      &lt;br /&gt;Third, GetSuccessor: This method can visit any node and iterate over its children. There is also GetSequenceElements and GetEntityMembers… probably what we want to do is something like this: 

      &lt;br /&gt;if (graphBuilder.IsEntity(root)) 

      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; foreach (… graphBuilder.GetEntityMembers(root)) 

      &lt;br /&gt;else if (graphBuilder.IsSequence(root)) 

      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; foreach (… graphBuilder.GetSequenceElements(root)) 

      &lt;br /&gt;… same thing for IsNode 

      &lt;br /&gt;

      &lt;br /&gt;Note the order of checking is important, (e.g IsSequence before IsNode) because a sequence node is also a “node”… 

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div&gt;Let the fun begin, first, let’s declare a method that accepts a path and a list of emails as parameters,&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Send(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; reportPath, &lt;span style="color: #0000ff"&gt;params&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] contacts)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(reportPath);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; str &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; contacts)&lt;br /&gt;        Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;-&amp;gt;&amp;quot;&lt;/span&gt; + str);&lt;br /&gt;}&lt;/pre&gt;
Now, we want to map every Send node into a method call with the right parameters… 

      &lt;br /&gt;We will use &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx" target="_blank"&gt;System.Linq.Expressions&lt;/a&gt; namespace [MSDN Link]. 

      &lt;br /&gt;

      &lt;br /&gt;This namespace contains virtually all types of operations you can perform in .NET, Assignment, MethodCall, Lambda Expression, ArrayIndexing…. etc 

      &lt;br /&gt;

      &lt;br /&gt;2 things we are interested in for now, are MethodCallExpression (to be able to represent our Send method) and Lambda Expression to be able to compile into MSIL and actually run these instructions 

      &lt;br /&gt;

      &lt;br /&gt;Here is an example for this in action&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;MethodCallExpression expression = &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Expression.Call(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Program).GetMethod(&lt;span style="color: #006080"&gt;&amp;quot;Send&amp;quot;&lt;/span&gt;),&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Expression[] { Expression.Constant(path, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)),&lt;br /&gt;                       Expression.Constant(emails, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[])) });&lt;br /&gt;LambdaExpression lambda = Expression.Lambda(expression);&lt;br /&gt;lambda.DynamicInvoke();&lt;br /&gt;&lt;/pre&gt;
Expression is an abstract base class that contains static methods to instantiate all types of expressions.. here we call Expression.Call to create a MethodCallExpression. 

      &lt;br /&gt;It accepts a MethodInfo for the method you want to call… 

      &lt;br /&gt;and a list of parameters. 

      &lt;br /&gt;You can then call lambda.DynamicInvoke() to compile into MSIL and execute whatever expressions are inside… 

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div&gt;Here is the full listing for creating the lambda expressions out of a passed node: &lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; LambdaExpression[] TraverseAndBuildTree(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; root, &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;                                         IGraphBuilder graphBuilder)&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (graphBuilder.GetLabel(root).ToString().ToLower() == &lt;span style="color: #006080"&gt;&amp;quot;send&amp;quot;&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt; childs = graphBuilder.GetSuccessors(root);&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; path = &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;         graphBuilder.GetSuccessors(childs.First()).First().ToString(&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     List&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; emails = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; objContact &lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;         graphBuilder.GetSequenceElements(childs.Last()))&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;       emails.Add(objContact.ToString());&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;     MethodCallExpression expression = &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;       Expression.Call(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Program).GetMethod(&lt;span style="color: #006080"&gt;&amp;quot;Send&amp;quot;&lt;/span&gt;),&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Expression[] { Expression.Constant(path, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)),&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;           Expression.Constant(emails.ToArray(), &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[])) })&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt;     LambdaExpression lambda = Expression.Lambda(expression);&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; LambdaExpression[] { lambda };&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt;   List&amp;lt;LambdaExpression&amp;gt; expressions = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;LambdaExpression&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum26"&gt;  26:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (graphBuilder.IsSequence(root))&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum27"&gt;  27:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; childNode &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum28"&gt;  28:&lt;/span&gt;         graphBuilder.GetSequenceElements(root))&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum29"&gt;  29:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum30"&gt;  30:&lt;/span&gt;       expressions.AddRange(&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum31"&gt;  31:&lt;/span&gt;           TraverseAndBuildTree(childNode, graphBuilder));&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum32"&gt;  32:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum33"&gt;  33:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (graphBuilder.IsNode(root))&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum34"&gt;  34:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; childNode &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum35"&gt;  35:&lt;/span&gt;         graphBuilder.GetSuccessors(root))&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum36"&gt;  36:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum37"&gt;  37:&lt;/span&gt;       expressions.AddRange(&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum38"&gt;  38:&lt;/span&gt;           TraverseAndBuildTree(childNode, graphBuilder));&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum39"&gt;  39:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum40"&gt;  40:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; expressions.ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;

        &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum41"&gt;  41:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
    &lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;25 – 40: We just traverse all nodes available and concatenate the lambda expressions into one big list. 
      &lt;br /&gt;6 – 15: We get the values of path and email parameters from AST. 

      &lt;br /&gt;17 – 22: We create the Call Expression. 

      &lt;br /&gt;

      &lt;br /&gt;You can then use this method like this:&lt;/div&gt;

    &lt;div id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;LambdaExpression[] lambdas = TraverseAndBuildTree(root, graphBuilder);&lt;br /&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (LambdaExpression lambda &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; lambdas)&lt;br /&gt;    lambda.Compile().DynamicInvoke();&lt;br /&gt;&lt;/pre&gt;
That’s it ! Spend sometime and play around with expressions ;)&lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Congrats&lt;/em&gt;, you’ve reached your 2nd Checkpoint! 

  &lt;br /&gt;

  &lt;br /&gt;The technique described here works well with simple languages, but once your language gets more complicated, you will need a more structured design (and hopefully an automated process) which we will explore in the next part.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div&gt;&lt;/div&gt;
&lt;a href="http://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" target="_blank"&gt;Part I: Create the Grammar (What your users write)&lt;/a&gt;. 

&lt;br /&gt;Part II: Consume the abstract syntax tree (Do some action). [This post] 

&lt;br /&gt;Path III: Compile your language into MSIL. 

&lt;div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9639251" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Oslo/">Oslo</category></item><item><title>Static Constructor… saved my day</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2009/05/21/static-constructor-saved-my-day.aspx</link><pubDate>Thu, 21 May 2009 18:47:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9633910</guid><dc:creator>Kasparov</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=9633910</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=9633910</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2009/05/21/static-constructor-saved-my-day.aspx#comments</comments><description>&lt;p&gt;Alsalam alikom wa ra7mat Allah wa barakatoh (Peace upon you)&lt;/p&gt;  &lt;p&gt;I was designing some classes today and I got stuck with an old problem I used to fix in an ugly way… &lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; A&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; InitializeMe()&lt;br /&gt;    {&lt;br /&gt;        SomeClassManager.Listeners.Add(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(A));&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;It would have been easy if:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;You know when exactly to call InitializeMe() so that you get hooked up at the right time (before Listeners start iterating over its elements) &lt;/li&gt;

  &lt;li&gt;You know what classes contain an InitializeMe function.. you can use reflection to get a list of all classes and then check whether they have the method or not.. –you can’t use interfaces as they don’t allow static members- &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What if you are designing a plugin model for an application, and want to call some static initialization code… again you will have to do reflection (which is slow).&lt;/p&gt;

&lt;p&gt;And here comes static constructors!&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; A&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; A()&lt;br /&gt;    {&lt;br /&gt;        InitializeMe();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; InitializeMe()&lt;br /&gt;    {&lt;br /&gt;        SomeClassManager.Listeners.Add(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(A));&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;That simple.. &lt;a href="http://msdn.microsoft.com/en-us/library/k9x6w0hc(VS.80).aspx" target="_blank"&gt;Static Constructor&lt;/a&gt; [MSDN Link] will be called once this type is “loaded” which happens in .NET when your assembly first gets loaded into memory.. which is exactly the time I wanted… and it saved my day!&lt;/p&gt;

&lt;p&gt;P.S, Check MEF (Managed Extensibility Framework) if you are interested in supporting plugins in ur apps, &lt;a href="http://www.codeplex.com/MEF"&gt;http://www.codeplex.com/MEF&lt;/a&gt;

  &lt;br /&gt;And here is a getting started guide, &lt;a href="http://code-inside.de/blog-in/2008/11/19/howto-first-steps-with-mef-hello-mef/"&gt;http://code-inside.de/blog-in/2008/11/19/howto-first-steps-with-mef-hello-mef/&lt;/a&gt;&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9633910" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Tips+n+Tricks/">Tips n Tricks</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category></item><item><title>Invent your own language… using Oslo. Part I</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx</link><pubDate>Fri, 15 May 2009 04:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9617519</guid><dc:creator>Kasparov</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=9617519</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=9617519</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx#comments</comments><description>Alsalam alikom wa ra7mat Allah wa barakatoh (Peace Upon You) 
 
 Part I: Create the Grammar …What your users will write… 
 
 If you are writing enterprise software, you probably came across this problem many times when you want to give IT Admins the ability to customize your application through some sort of scripts. The typical solution used to be VBS on windows and now PowerShell.. they both offer great ability to expose your public APIs and let others consume them. However, there is no way to customize the syntax to your application needs. 
 
 If you are writing a software that does some reporting and want to let users decide what template should be used when sending to every customer… wouldn’t it be nice if you let them write something like this: 
 
 Send "D:\Reports\Templates\Regular.tmpl" to sm1@hotmail.com, sm2@hotmail.com 
 
 
 
 Send "D:\Reports\Templates\Special.tmpl" to sm3@hotmail.com 
 


 Or how about a language that describes mathematical equations such as this: 
 [[x, y] ; [1, 2] ; [2, x + 5]] * Func(2, 3, 5) – x2 + y * j ^ 3 + d - mul(3) 
 This may look similar to Matlab syntax… in fact it’s. The first part “[[x, y] ; [1, 2] ; [2, x + 5]] ” declares a 2x2 matrix. You can think of all sorts of symbols and operations that can be put into an equation… Latex and MathML [Wikipedia links] come into mind. Latex is not only for representing equations but it’s one feature of it. My point is, even if you want to reuse one of the existing languages out there in your application, you don’t have to rewrite a parser, buy a commercial package or get an OSS that might limit how you will be able to distribute your software. MGrammer (included in Oslo) is one elegant way to achieve this… 

 Or maybe you just don’t like the brackets () in C# and want to define a bracketless C# (VB maybe 8-) )… 

 There might be a debate around this and whether you will want each app to have it’s own Domain Specific Language (DSL) or is it better to have a common known scripting language (like VBS or PowerShell).. I won’t go into this debate actually here… but I would just say, “it depends”. 

 Let’s get to work, 
 First thing you need to do is to get Oslo installed [Microsoft Downloads Link]. It needs SQL server ( Free Express Edition works fine) –I installed it without SQL Server, it gave me an error but it’s ok as long as you don’t do any stuff that requires this-… 

 Oslo comes with a toolchain. We will actually make use of only 2 tools out of these; IntelliPad.exe (GUI tool) and Mg.exe (CMD tool) 

 We will be using IntelliPad to write MGrammar (.mg file), test it on our examplr data and make sure the syntax tree is in the right format. 

 We will then use Mg tool to compile the MGrammar file into .mgx file which represents you compiled language parser.. you can then use it from any .NET app. 

 We will make a grammar that recognizes that first language above, let’s call it Reporter . We will get to know MGrammer as we move on… 

 
 
 
 Open notepad (or notepad++ you geeky guys), type in this example and save as reportsData.bundle. 
 
 
 Send "D:\Reports\Templates\Regular.tmpl" to sm1@hotmail.com Send "D:\Reports\Templates\Special.tmpl" to sm2@hotmail.com, sm3@hotmail.com 
 
 This will work as our test data to make sure our grammar works fine. 
 
 

 
 
 Open IntelliPad: 
 
 and save that empty file into Reporter.mg (you have to put the extension yourself or else IntelliPad won’t recognize the file as an MGrammar one) 
 Now close IntelliPad, and double click on your file to open IntelliPad again in MGrammar Mode (it’s a bug if you ask me) 
 
 

 
 
 The container in MG is the module element (much like namespaces or packages) 
 inside a module you can have different elements (language, type, … etc) in MG we are interested in language element. 
 Now go ahead and type your first lines 
 
 module Basic.Languages 
 { 
 language Reporter 
 { 
 } 
 } 
 
 

 
 
 From MGrammar Mode menu, choose Tree Preview, this allows you to see how the data match as you start writing more code. It will ask you for an input file, go ahead and choose reportsData.bundle we created in step 1. 
 Here is how it should look like now: 
 
 
 

 
 
 Add this statement (syntax Main = any*; ) into language section, you will notice that the right tree got populated with a long list of characters. 
 This statement is the entry point (Main), here it tells Mg that the syntax you are expecting is “any*” which basically matches with any number of any type of characters. 
 
 

 
 
 We know that every statement we have starts with “Send” and has a “to” in the middle. So we can refine that any* matching with something more useful, like this: 
 syntax Main = "Send" any* "to" any*; 
 This instructs MG that you are expecting a word “Send” then some text then “to” then some text. 
 You will notice that the preview tree now has a little structure… “Send” node and some characters in side then a “to” node with the rest of the statement.. 
 But there is an error saying that the second statement didn’t match anything. 
 This is because our main statement matches only one instance. 
 We can define it like this: 
 syntax Main = ("Send" any* "to" any*)+; 
 or we can be more descent and define another syntax, say SendCommand.. like this 
 
 
 
 syntax SendCommand = "Send" any* "to" any*; syntax Main = SendCommand+; 
 
 As you might have guessed, + means 1 or more, * means 0 or more (same as in Regular Expressions) and ? means 0 or 1 occurrence. 
 
 
 

 
 
 To make things a little bit more descent, we can define tokens for “Send” and “to”… Tokens can pretty much looked at as the basic building element for a statement or a matching pattern.. 
 
 Here is our code after defining the tokens: 
 
 
 token SendToken = "Send" ; token ToToken = "to" ; syntax SendCommand = SendToken any* ToToken any*; syntax Main = SendCommand+; 
 
 

 
 
 We need to match the quoted string (file path) but we don’t really bother for now to validate whether it’s a valid file path or not (Left as an exercise to the reader) So we will go ahead and define a more complex token for that, 
 
 
 
 token AlphaNumeric = 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '-'; token Path = (AlphaNumeric | ':' | '\\' | '.')+; token Email = (AlphaNumeric | '@' | '.')+; token QuotedPath = '"' Path '"'; syntax SendCommand = SendToken QuotedPath ToToken Email; 
 
 
 
 ‘a’..’z’ defines range (as you might have guessed) other things are straight forward. 
 
 
 
 This is how the right preview tree should look like by now. 
 
 

 
 
 Things are getting to look better, right? maybe you started to get the feeling it’s just like regular expressions, in fact, it’s pretty much the same concept (String matching after all if you want the truth) but writing in MGrammar gives you a lot of other options when writing your rules than you have when matching with regEx Also you will almost write 0 Lines of code to get your abstract syntax tree (AST) built ;) 
 
 
 
 

 
 
 You will notice IntelliPad is complaining about the ‘,’ in the second example line. He’s right, we didn’t define how a list of emails may look like. 
 
 
 
 syntax ListOfEmails = Email | ListOfEmails "," Email; syntax SendCommand = SendToken QuotedPath ToToken ListOfEmails; 
 This is a recursive rule. It recognizes “a@b.com ” and “a@b.com,c@d.com ” but it doesn’t recognize “a@b.com ,” in other words, the “,” must be between 2 emails… which is exactly what we want. 
 
 

 
 
 We are almost done, we now need to modify the resultant tree to make it look, well, better. 
 
 
 
 
 We need to fix the Send nodes so that we don’t actually put “Send” and “to” into the resultant tree. We will use projection, that’s we will define how do we want our rules to be projected (output) into the syntax tree… 
 
 
 
 syntax SendCommand = SendToken p:QuotedPath ToToken list:ListOfEmails =&amp;gt; Send[p, list]; 
 We named the match result of QuotedPath as p and ListOfEmails as list.. then the projection is Send[p, list]; would create a node called Send, with 2 children only. 
 Here is the output: 
 
 
 
 Main[ [ Send[ "\" D:\\Reports\\Templates\\Regular.tmpl\ "" , ListOfEmails[ "sm1@hotmail.com" ] ], Send[ "\" D:\\Reports\\Templates\\Special.tmpl\ "" , ListOfEmails[ ListOfEmails[ "sm2@hotmail.com" ], "," , "sm3@hotmail.com" ] ] ] ] 
 
 
 
 

 
 
 Now we need to remove that “\” before and after the path… they don’t look neat. 
 
 
 
 token QuotedPath = '"' p:Path '"' =&amp;gt; Path[p]; syntax SendCommand = SendToken p:QuotedPath ToToken list:ListOfEmails =&amp;gt; Send[valuesof(p), list]; 
 First modification would make a node for every path and would remove the double quotes like this 
 Path[ 
 “D:\…..” 
 ] 
 
 valuesof(p) will extract the node contents and project it to the tree, in this case the contents is the path with singles double quotations (if you know what I mean). 
 
 

 
 
 You can go ahead and try to modify the Email rule so that it doesn’t project the “,” and projects all emails in one list rather than nested lists. 
 
 
 
Here is how the tree looks like after all my modifications (with those left to you as exercise :)) 
 
 
 
 Commands[ Send[ "D:\\Reports\\Templates\\Regular.tmpl" , Emails[ "sm1@hotmail.com" ] ], Send[ "D:\\Reports\\Templates\\Special.tmpl" , Emails[ "sm2@hotmail.com" , "sm3@hotmail.com" ] ] ] 
 
 
 

 
 
 Now, to compile use .mg file, open cmd, browse to the folder where reporter.mg file is and type this: 
 "C:\Program Files\Microsoft Oslo SDK 1.0\Bin\mg.exe" reporter.mg 
 This will generate a reporter.mgx file in the current directory. 
 Now run: 
 "C:\Program Files\Microsoft Oslo SDK 1.0\Bin\mgx.exe” reportsData.bundle -r:reporter.mgx 
 This will generate the M structure (similar to AST with some key differences that we will explore in the next post) into a separate file. Viola ! 
 
 
 
 

 Conclusion: 

 
 
 We used IntelliPad to write and test our MGrammer. 

 
 MGrammar is a descriptive language to define the grammar of any language. 

 
 We need to define module and language sections in .mg file. 

 
 syntax Main is the starting point, syntax es can be recursive. 

 
 tokens are the basic elements. 

 
 We use projection rules on syntax and token to reshape the resultant tree. 

 
 MGrammar is awesome! 
 

 Congrats , you reached your first Checkpoint ! 

 Here is the full listing of Reporter.mg file. 

 
 
 module Basic.Languages { language Reporter { token SendToken = "Send" ; token ToToken = "to" ; token AlphaNumeric = 'a'..'z' | 'A'..'Z' | '0'..'9'; token Path = (AlphaNumeric | ':' | '\\' | '.')+; token Email = (AlphaNumeric | '@' | '.')+; token QuotedPath = '"' p:Path '"' =&amp;gt; Path{p}; syntax ListOfEmails = e:Email =&amp;gt; Emails[e] | list:ListOfEmails "," e:Email =&amp;gt; Emails[valuesof(list), e]; syntax SendCommand = SendToken p:QuotedPath ToToken list:ListOfEmails =&amp;gt; Send{valuesof(p), list}; syntax Main = s:SendCommand+ =&amp;gt; Main{Commands[valuesof(s)]}; interleave whitespace = ( " " | "\r" | "\n" | "\t" )+; } } 
 

 Path I: Create the Grammar (What your users write). [This post] 
 Part II: Consume the abstract syntax tree (Do some action!). 
 Path III: Compile your language into MSIL. 

 References:
 A good tutorial for MGrammar: http://msdn.microsoft.com/en-us/library/dd441702.aspx 

 You will also find some good documents in C:\Program Files\Microsoft Oslo SDK 1.0\Documents
 2 in particular are interesting:
 MGrammar Language Specification.docx
 MGrammar in a Nutshell.docx 

 Have a nice time! 
 Creating a .NET language by Haytham Abuel-Futuh is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License . Based on a work at blogs.msdn.com ....(&lt;a href="http://blogs.msdn.com/b/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9617519" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Oslo/">Oslo</category></item><item><title>Friend Assemblies in .NET</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2009/04/30/friend-assemblies-in-net.aspx</link><pubDate>Fri, 01 May 2009 02:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9581465</guid><dc:creator>Kasparov</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=9581465</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=9581465</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2009/04/30/friend-assemblies-in-net.aspx#comments</comments><description>Alsalam alikom wa r7amat Allah wa barakatoh (Peace upon you..) Long story short, you have AssemblyA (with source code) and AssemblyB… you want AssemblyB to be able to access internal members (types, data, methods.. etc) of AssemblyA… You can of course do that through Reflection but come on, it’s not a descent way to do it.. right?? .Net framework offers an attribute called InternalsVisibleToAttribute [ MSDN Link ]. It works on .NET 2.0 and above. An example grabbed from MSDN: 1: // AssemblyA.dll 2: using System.Runtime.CompilerServices; 3: using System; 4: 5: [assembly:InternalsVisibleTo( "AssemblyB" )] 6: 7: // internal by default 8: class Class1 9: { 10: public void Test() 11: { 12: Console.WriteLine( "Class1.Test" ); 13: } 14: } 15: 16: // public type with internal member 17: public class Class2 18: { 19: internal void Test() 20: { 21: Console.WriteLine( "Class2.Test" ); 22: } 23: } This way, you can easily use Class1 and Class2.Test from AssemblyB code (obviously after adding reference to AssemblyA). I had 2 questions in mind after reading this, This can be some sort of a hack, I simply create another asesmbly that replaces your AssemblyB and violla, I’ve access to your internal members… A: You can include the public token key for AssemblyB in InternalsVisibleTo constructor Can I selectively determine what classes (types) are visible to AssemblyB? .NET offers a class called StrongNameIdentityPermission [ MSDN Link ] Which can solve both of these problems actually. I’ll not go into details for how to do it, but let me state what can you do using this class.. You can require that calling assemblies (e.g AssemblyB) must be strongly signed using a private key you provide.. (Imagine you are building a multi-tier application and you don’t want anybody else to just call your public classes…) StrongNameIdentityPermission works only on public classes, while InternalsVisibleToAttribute works on assembly level. One last thing, it’s actually kind of funny.. InternalsVisibleTo constructor has 2 overloads (only in .NET3.5), the second accepted named parameter named AllIntenalsVisible (bool) which gave me hope that I can somehow selectively choose what to make visible… but… here is what I found in MSDN Documentation: This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. Catch you later…...(&lt;a href="http://blogs.msdn.com/b/haythamalaa/archive/2009/04/30/friend-assemblies-in-net.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9581465" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Tips+n+Tricks/">Tips n Tricks</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/-NET/">.NET</category></item><item><title>Javascript Execution Context</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2008/07/21/javascript-execution-context.aspx</link><pubDate>Mon, 21 Jul 2008 15:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8760352</guid><dc:creator>Kasparov</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=8760352</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=8760352</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2008/07/21/javascript-execution-context.aspx#comments</comments><description>Alsalam alikom wa ra7mat Allah wa barakatoh I've faced a strange javascript exception when running a piece of code on Firefox/Safari, while it works fine on IE. Here is what Firefox 3.0 says: uncaught exception : [ Exception ... "Illegal operation on WrappedNative prototype object " nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame : After some searching, I found out that the problem is related to how Firefox/Safari handles the execution context for any called function. Have a look on this code: 1: &amp;lt; html &amp;gt; 2: &amp;lt; head &amp;gt; 3: &amp;lt; script language ="javascript" &amp;gt; 4: function ExecuteRef(str, arg) { 5: str(arg); 6: } 7: function TestRun(objID){ 8: var obj = document.getElementById(objID); 9: var funcRef = obj.parentNode.removeChild; 10: ExecuteRef(funcRef, obj); 11: } 12: &amp;lt;/ script &amp;gt; 13: &amp;lt;/ head &amp;gt; 14: 15: &amp;lt; body onload ="javascript:TestRun('delete1');" &amp;gt; 16: 17: &amp;lt; div id ='delete1' &amp;gt; This should not appear &amp;lt;/ div &amp;gt; 18: &amp;lt; div &amp;gt; This should alwasy appear &amp;lt;/ div &amp;gt; 19: 20: &amp;lt;/ body &amp;gt; 21: &amp;lt;/ html &amp;gt; What this code is supposed to do is to remove the first div -'delete1'- just when the page load. What actually happens on FF/Safari, is that nothing is deleted, while on IE the first div no longer appears. We have 2 functions here, TestRun which given an ID for an element, it creates a reference for that object -ln 8- and a reference for the removeChild method -ln 9- , then passes them both to ExecuteRef, which will simply execute that -ln 5- . Why is that happening? Is it a problem with FF Javascript Engine AND Safari's one? or a problem with IE's ? Actually it's not a mistake at all. It's all about how each engine determines the context of function execution. Let's speak in code language: var funcRef = obj.parentNode.removeChild; funcRef is now a function pointer for removeChild. When you execute funcRef(args), it's translated as funcRef(this, args) in all javascript engine. this way it's easy for your function body to reference member variables of the object it's defined in. The difference between IE &amp;amp; FF/Safari engines, is that IE preserves the original caller object, in this case the "obj.parentNode".. so that when it translates funcRef(args) to funcRef( this , args) it makes sure that " this " is "obj.parentNode" and your code works fine. On the other hand, FF/Safari don't preserve the original caller, instead they just try to execute the function in the new context. Which in turn means that in funcRef( this , args), this refers to the ExecuteRef method itself, and as there is no function called removeChild defined inside ExecuteRef, the calling just fails giving that weird exception above. I've explored different solutions for that problem. I'll try to summarize them here: 1. Explicitly declare the parameters of the function pointer: var funcRef = function (x) { obj.parentNode.removeChild (x); } This way, jscript engine will have to preserve the calling object -obj.parentNode- 2. If you already know the full parameters that will passed, you can get use of the closure feature JS supports. var funcRef = function (){obj.parentNode.removeChild(obj);};ExecuteRef(funcRef); 3. Use Function.call / Function.apply: this requires that you pass the context to ExecuteRef.. function ExecuteRef(context, str, arg) { str.call(context, arg);} function TestRun(objID){ .... ExecuteRef(obj.parentNode, funcRef, obj);} That's it so far. Thanks for reading!...(&lt;a href="http://blogs.msdn.com/b/haythamalaa/archive/2008/07/21/javascript-execution-context.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8760352" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Javascript/">Javascript</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Errors/">Errors</category><category domain="http://blogs.msdn.com/b/haythamalaa/archive/tags/Script/">Script</category></item><item><title>Hello World!</title><link>http://blogs.msdn.com/b/haythamalaa/archive/2008/06/19/hello-world.aspx</link><pubDate>Thu, 19 Jun 2008 22:03:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8622751</guid><dc:creator>Kasparov</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/rsscomments.aspx?WeblogPostID=8622751</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/haythamalaa/commentapi.aspx?WeblogPostID=8622751</wfw:comment><comments>http://blogs.msdn.com/b/haythamalaa/archive/2008/06/19/hello-world.aspx#comments</comments><description>&lt;p&gt;Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace upon you)&lt;/p&gt; &lt;p&gt;&lt;br&gt;This's my first post on MSDN blogs, I've been blogging since 2005 on blogspot though... a mixture of personal and technical posts you can find on &lt;a href="http://haythamalaa.blogspot.com"&gt;http://haythamalaa.blogspot.com&lt;/a&gt;&lt;/p&gt; &lt;p&gt;I'll keep this blog technical except for this first post &lt;img alt="Smile" src="http://messenger.msn.com/MMM2006-04-19_17.00/Resource/emoticons/regular_smile.gif"&gt;.&lt;/p&gt; &lt;p&gt;A brief about me:&lt;/p&gt; &lt;p&gt;Haytham Alaa Abuel-Futuh, Muslim, Egyptian. Graduated from faculty of Computer and Information Sciences, Ain-Shams University, Cairo, Egypt.&lt;br&gt;he was actively joining ACM during school days, and was a part of a team which participated in our regional contest for 2 consecutive years, getting the 9th then 5th positions respectively.&lt;/p&gt; &lt;p&gt;He got a research scholarship in University of Paderborn, Germany in 2007 during which he -along with his teammates- developed RDES (Remote Debugging for Embedded Systems).&lt;/p&gt; &lt;p&gt;He then joined &lt;a href="http://criticalsites.com" target="_blank"&gt;Critical Sites&lt;/a&gt; -the development division of &lt;a href="http://ntpsoftware.com" target="_blank"&gt;NTP Software&lt;/a&gt;-the world leader in managing massive unorganized data- in late 2007 till early May-2008 when he moved to &lt;a href="http://microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Keep in touch.&lt;/p&gt; &lt;p&gt;Haytham Abuel-Futuh,&lt;br&gt;SDE, WSS, MCDC,&lt;br&gt;&lt;u&gt;&lt;font color="#669966"&gt;&lt;a href="http://haythamalaa.blogspot.com"&gt;http://haythamalaa.blogspot.com&lt;/a&gt;.&lt;/font&gt;&lt;/u&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8622751" width="1" height="1"&gt;</description></item></channel></rss>
