<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ThumNet &#187; csharp</title>
	<atom:link href="http://blog.thumnet.com/tag/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thumnet.com</link>
	<description>- Jeffrey Tummers</description>
	<lastBuildDate>Fri, 23 Sep 2011 21:33:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Automatically connect WebParts on a SharePoint page</title>
		<link>http://blog.thumnet.com/automatically-connect-webparts-on-sharepoint-page/201/</link>
		<comments>http://blog.thumnet.com/automatically-connect-webparts-on-sharepoint-page/201/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 09:34:55 +0000</pubDate>
		<dc:creator>Jeffrey Tummers</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[sharepoint2007]]></category>
		<category><![CDATA[sharepoint2010]]></category>
		<category><![CDATA[webpart]]></category>

		<guid isPermaLink="false">http://blog.thumnet.com/?p=201</guid>
		<description><![CDATA[For a recent project we had several WebParts (Provider and Consumer webparts) on a page which had to be connected to each other when the page was provisioned by our solution. Unfortunately this cannot be done within the Module.xml So I created a method which automatically connects the consumer WebParts to their correct provider WebParts. &#8230; </p><p><a class="more-link block-button" href="http://blog.thumnet.com/automatically-connect-webparts-on-sharepoint-page/201/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>For a recent project we had several WebParts (Provider and Consumer webparts) on a page which had to be connected to each other when the page was provisioned by our solution.</p>
<p>Unfortunately this cannot be done within the Module.xml <img src='http://blog.thumnet.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>So I created a method which automatically connects the consumer WebParts to their correct provider WebParts.</p>
<p>To do this I basically used the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.splimitedwebpartmanager.aspx">SPLimitedWebPartManager</a>, <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.providerconnectionpoint.aspx">ProviderConnectionPoint</a>, <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.consumerconnectionpoint.aspx">ConsumerConnectionPoint </a>and some other nice classes.</p>
<p><strong>The method (warning long block of code)</strong></p>
<pre class="brush: csharp; title: ; notranslate">
/// &lt;summary&gt;
/// Connects the web parts on page.
/// &lt;/summary&gt;
/// &lt;param name=&quot;web&quot;&gt;The web.&lt;/param&gt;
/// &lt;param name=&quot;pageUrl&quot;&gt;The page URL.&lt;/param&gt;
/// &lt;param name=&quot;publish&quot;&gt;if set to &lt;c&gt;true&lt;/c&gt; [publish].&lt;/param&gt;
public static void ConnectWebPartsOnPage(SPWeb web, string pageUrl, bool publish)
{
	// get the page from the url
	SPFile page = web.GetFile(pageUrl);

	// make sure the page exists
	if (page.Exists)
	{
		// if the page is checked out, check it in because then the user can do an rollback
		if (page.Level == SPFileLevel.Checkout)
		{
			page.CheckIn(&quot;Page was checked out, needs to be checked in before connecting webparts&quot;, SPCheckinType.MinorCheckIn);
		}

		// check out the page because we are going to do changes on it
		page.CheckOut();

		// get the webpartmanager for the page
		using (Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
		{
			try
			{
				// try to connect the webparts on the page
				if (ConnectWebParts(mgr))
				{
					string checkinComment = &quot;Auto connected WebParts on page&quot;;

					// webparts are connected so update the page and check it in
					page.Update();
					page.CheckIn(checkinComment);

					if (publish)
					{
						page.Publish(checkinComment);
						page.Approve(checkinComment);
					}
				}
			}
			catch (Exception ex)
			{
				page.UndoCheckOut();
				throw ex;
			}
		}
	}
}

/// &lt;summary&gt;
/// Checks if a WebPart connection exists.
/// &lt;/summary&gt;
/// &lt;param name=&quot;connections&quot;&gt;The connections.&lt;/param&gt;
/// &lt;param name=&quot;provider&quot;&gt;The provider.&lt;/param&gt;
/// &lt;param name=&quot;consumer&quot;&gt;The consumer.&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private static bool WebPartConnectionExist(Microsoft.SharePoint.WebPartPages.SPWebPartConnectionCollection connections,
	System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.WebPart consumer)
{
	foreach (Microsoft.SharePoint.WebPartPages.SPWebPartConnection conn in connections)
	{
		if (conn.Provider == provider &amp;&amp; conn.Consumer == consumer)
		{
			return true;
		}
	}

	return false;
}

/// &lt;summary&gt;
/// Connects the web parts.
/// &lt;/summary&gt;
/// &lt;param name=&quot;manager&quot;&gt;The manager.&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private static bool ConnectWebParts(Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager)
{
	bool connectionMade = false;

	// get the webparts on the page
	Microsoft.SharePoint.WebPartPages.SPLimitedWebPartCollection webparts = manager.WebParts;

	// only continu if there are any webparts
	if (webparts.Count &gt; 0)
	{
		ProviderConnectionPointCollection provColl;
		ConsumerConnectionPointCollection consColl;
		// walk through all the webparts on the page to find provider webparts
		foreach (System.Web.UI.WebControls.WebParts.WebPart providerPart in webparts)
		{

			// check if the webpart is a Provider webpart
			provColl = manager.GetProviderConnectionPoints(providerPart);
			if (provColl != null &amp;&amp; provColl.Default != null)
			{

				// walk through all the webparts on the page again now to find the consumer webparts
				foreach (System.Web.UI.WebControls.WebParts.WebPart consumerPart in webparts)
				{

					// first make sure the webpart isn't the provider webpart
					if (consumerPart != providerPart)
					{
						// check if the webpart is a Consumer webpart,
						// then make sure the consumer webpart isn't already connected to the provider webpart
						// then check if the interface of the consumer webpart is assignable from the interface of the provider webpart
						consColl = manager.GetConsumerConnectionPoints(consumerPart);
						if (consColl != null &amp;&amp; consColl.Default != null
							&amp;&amp; !WebPartConnectionExist(manager.SPWebPartConnections, providerPart, consumerPart)
							&amp;&amp; consColl.Default.InterfaceType.IsAssignableFrom(provColl.Default.InterfaceType))
						{
							// connect the webparts
							manager.SPConnectWebParts(providerPart, provColl.Default, consumerPart, consColl.Default);
							connectionMade = true;
						}
					}
				}
			}
		}
	}
	return connectionMade;
}
</pre>
<p><strong>Sample usage (console application)</strong></p>
<pre class="brush: csharp; title: ; notranslate">
try
{
	using (SPSite site = new SPSite(&quot;http://localhost&quot;))
	using (SPWeb web = site.OpenWeb())
	{
		ConnectWebPartsOnPage(web, &quot;pages/default.aspx&quot;, false);
	}
}
catch (Exception ex)
{
	Console.WriteLine();
	Console.WriteLine(&quot;Message:\n{0}\n\nStack:\n{1}\n\n&quot;, ex.Message, ex.StackTrace);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.thumnet.com/automatically-connect-webparts-on-sharepoint-page/201/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# 4.0 .NET becomes dynamic</title>
		<link>http://blog.thumnet.com/csharp4-dotnet-becomes-dynamic/73/</link>
		<comments>http://blog.thumnet.com/csharp4-dotnet-becomes-dynamic/73/#comments</comments>
		<pubDate>Fri, 29 May 2009 17:09:23 +0000</pubDate>
		<dc:creator>Jeffrey Tummers</dc:creator>
				<category><![CDATA[DevDays09]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[qnh]]></category>

		<guid isPermaLink="false">http://blog.thumnet.com/?p=73</guid>
		<description><![CDATA[This post is about the session called C# 4.0 / The Future of C# given by Krishnan Subramanian. First off let me start with a quick summary of main new feauture for the different versions of the C# that are released. C# 1.0, Managed code C# 2.0, Generics C# 3.0, Language Integrated Query, short LINQ &#8230; </p><p><a class="more-link block-button" href="http://blog.thumnet.com/csharp4-dotnet-becomes-dynamic/73/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>This post is about the session called <strong>C# 4.0 / The Future of C#</strong> given by Krishnan Subramanian.</p>
<p><img src="http://blog.thumnet.com/wp-content/uploads/2009/05/csharp41.png" alt="csharp4-dlr" width="464" height="300" /></p>
<p>First off let me start with a quick summary of main new feauture for the different versions of the C# that are released.</p>
<ul>
<li>C# 1.0, Managed code</li>
<li>C# 2.0, Generics</li>
<li>C# 3.0, Language Integrated Query, short LINQ</li>
<li>C# 4.0, Dynamic programming</li>
</ul>
<p>Some off the innovation for C# 4.0 are:</p>
<p><strong>Dynamic Language Runtime</strong><br />
The new version of C# has a new type of object declaration, called <code>dynamic</code>. This looks a bit the <code>var</code> keyword. With the var keyword the compiler replaced the var with the object type on compile time, intellisense and code completion still was available in Visual Studio. With the new dynamic keyword the compiler doesn&#8217;t replace this and even more important the compiler can&#8217;t check for syntax error on a dynamic object. Intellisense and code complition are also not available for objects defined with dynamic.</p>
<p>Dynamic objects are losely typed instead of strongly. </p>
<p>After reading this you might think whats the use of this new DLR, in the following sample I&#8217;ll try to explain this. We have a simple C# class called Calculator, and use it like below.</p>
<pre class="brush: csharp; title: ; notranslate">
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);
</pre>
<p>Now suppose the calculator class is not strongly typed, the code would like something like the following</p>
<pre class="brush: csharp; title: ; notranslate">
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember(&quot;Add&quot;, BindingFlags.InvokeMethod, null, new object[] { 10, 20 });
int sum = Convert.ToInt32(res);
</pre>
<p>With the new dynamic keyword in C# 4.0 it would simply be</p>
<pre class="brush: csharp; title: ; notranslate">
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
</pre>
<p><strong>Optional Named Parameters</strong><br />
To show you the what this means take a look at the examples below.</p>
<p>In previous version of C# method overloads would be used.</p>
<pre class="brush: csharp; title: ; notranslate">
public void Add(string lineOfText);
public void Add(string lineOfText, bool isError);
public void Add(string lineOfText, int repeat);
public void Add(string lineOfText, int repeat, bool isError);
</pre>
<p>With the new C# 4.0, the method declaration would like this</p>
<pre class="brush: csharp; title: ; notranslate">
public void Add(string lineOfText, int repeat = 1, bool isError = false);
</pre>
<p>As you can see in the sample above, it&#8217;s now possible to specify default values for a method parameter. Some samples of how to use the Add method are:</p>
<pre class="brush: csharp; title: ; notranslate">
Add(&quot;This is the line&quot;, repeat : 5);
Add(isError : true, lineOfText : &quot;Another line of text&quot;);
</pre>
<p>For people using JavaScript this might look very familiar.</p>
<p>Another cewl new thing I saw at the demo, was the ability to interpret and compile a string of C# code at runtime. This functionality offers some great new possibilities. Again for the Javascript people reading this it&#8217;s like the <code>eval</code> function.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thumnet.com/csharp4-dotnet-becomes-dynamic/73/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

