<?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>DevExpertise &#187; LINQ</title>
	<atom:link href="http://www.devexpertise.com/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devexpertise.com</link>
	<description>Practical tips and tricks for all things .NET, SharePoint, Silverlight, InfoPath, and general application development.</description>
	<lastBuildDate>Wed, 12 May 2010 14:32:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Implementing a LINQ version of SQL&#8217;s LIKE Operator</title>
		<link>http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/</link>
		<comments>http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 18:30:51 +0000</pubDate>
		<dc:creator>DevExpert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/</guid>
		<description><![CDATA[One of the requirements of one of my recent projects was to implement a search page which allowed the user to enter a search term that supported wildcards.&#160; The search term could contain any number of wildcards in any position within that term.
If you’ve done anything like this before, you probably know there’s nothing built-in [...]]]></description>
			<content:encoded><![CDATA[<p>One of the requirements of one of my recent projects was to implement a search page which allowed the user to enter a search term that supported wildcards.&#160; The search term could contain any number of wildcards in any position within that term.</p>
<p>If you’ve done anything like this before, you probably know there’s nothing built-in to LINQ that supports this type of behavior.&#160; Sure, you could use a combination of <a href="http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">String.StartsWith</a>, <a href="http://msdn.microsoft.com/en-us/library/system.string.endswith.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">String.EndsWith</a>, or <a href="http://msdn.microsoft.com/en-us/library/system.string.contains.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">String.Contains</a>, but this could quickly become too cumbersome if there are many wildcards and/or they are scattered throughout the search term.&#160; Let’s look at a couple simple examples to illustrate…</p>
<p>Pretend for a second I was doing this in SQL, and I needed to get all values that start with the letter T.&#160; I would do this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: gray">* </span><span style="color: blue">from </span>SomeTable <span style="color: blue">where </span>SomeField <span style="color: gray">LIKE </span><span style="color: red">'T%'</span></pre>
</div>
<p>
  <br />The .NET/LINQ equivalent would be this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>results = (<span style="color: blue">from </span>v <span style="color: blue">in </span>values <span style="color: blue">where </span>v.StartsWith(<span style="color: #a31515">&quot;T&quot;</span>) <span style="color: blue">select </span>v);</pre>
</div>
<p>
  <br />Not too difficult.&#160; However, what if you wanted to do the SQL-equivalent of this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: gray">* </span><span style="color: blue">from </span>SomeTable <span style="color: blue">where </span>SomeField <span style="color: gray">LIKE </span><span style="color: red">'%a%a%'</span></pre>
</div>
<p>
  <br />You’d have do a little creative parsing.&#160; It gets even worse when you as the developer doesn’t know what search term will be entered, how many wildcards will be included, and where in the term they appear.&#160; It all has to be dynamic. </p>
<p>I did a little poking around to see if anyone has done this before, and the only thing I could find was recommendations on using StartsWith/EndsWith/Contains, which I already ruled out.&#160; I also found the <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods.like.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">SqlMethods.Like()</a> method which sounded perfect.&#160; However after further research, discovered it can <em>only</em> be used on an entity directly retrieved from a DataContext, such as this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">using </span>(<span style="color: #2b91af">DemoDataContext </span>db = <span style="color: blue">new </span><span style="color: #2b91af">DemoDataContext</span>()){
    <span style="color: blue">var </span>results = (<span style="color: blue">from </span>v <span style="color: blue">in </span>db.SomeTable <span style="color: blue">where </span><span style="color: #2b91af">SqlMethods</span>.Like(v.SomeField, <span style="color: #a31515">&quot;*a*a*&quot;</span>) <span style="color: blue">select </span>v);
}</pre>
</div>
<p>
  <br />If you try to use the SqlMethods.Like() method on anything except a DataContext’s Table&lt;T&gt;, you’ll get the following message:</p>
<blockquote>
<p><strong><font color="#ff0000">“Method &#8216;Boolean Like(System.String, System.String)&#8217; cannot be used on the client; it is only for translation to SQL.”</font></strong></p>
</blockquote>
<p>So much for that.&#160; I decided to write my own extension method.&#160; I figured I could write one fairly easily using a regular expression, and I was right!&#160; I checked out a trusty <a href="http://regexlib.com/CheatSheet.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/regexlib.com');">RegEx cheat sheet</a> and found the following relevant metacharacters:</p>
<ul>
<li><font size="1" face="cour"><strong>^</strong>&#160;&#160;&#160;&#160; </font>Indicates the start of a string </li>
<li><font size="1" face="cour"><strong>$</strong>&#160;&#160;&#160;&#160; </font>Indicates the end of a string </li>
<li><font size="1" face="cour"><strong>* </strong>&#160;&#160;&#160; </font>Indicates zero or more of previous expression </li>
</ul>
<p>Knowing this, I wrote the following extension method:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">public static bool </span>Like(<span style="color: blue">this string </span>value, <span style="color: blue">string </span>term) {
    <span style="color: #2b91af">Regex </span>regex = <span style="color: blue">new </span><span style="color: #2b91af">Regex</span>(<span style="color: blue">string</span>.Format(<span style="color: #a31515">&quot;^{0}$&quot;</span>, term.Replace(<span style="color: #a31515">&quot;*&quot;</span>, <span style="color: #a31515">&quot;.*&quot;</span>)), <span style="color: #2b91af">RegexOptions</span>.IgnoreCase);
    <span style="color: blue">return </span>regex.IsMatch(value ?? <span style="color: blue">string</span>.Empty);
}</pre>
</div>
<p>
  <br />Which I can then use like this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>results = (<span style="color: blue">from </span>v <span style="color: blue">in </span>values <span style="color: blue">where </span>v.Like(<span style="color: #a31515">&quot;*a*a*&quot;</span>) <span style="color: blue">select </span>v);</pre>
</div>
<p>
  <br />I can even simplify this by wrapping it up in another extension method:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; Like(<span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; source, <span style="color: blue">string </span>expression) {
    <span style="color: blue">return </span>(<span style="color: blue">from </span>s <span style="color: blue">in </span>source <span style="color: blue">where </span>s.Like(expression) <span style="color: blue">select </span>s);
}</pre>
</div>
<p>
  <br />Now all I have to do is the following:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>results = values.Like(<span style="color: #a31515">&quot;*a*a*&quot;</span>);</pre>
</div>
<p>
  <br />Finally, a quick usage example to prove it works:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>values = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;(){
    <span style="color: #a31515">&quot;Widget&quot;</span>, <span style="color: #a31515">&quot;Gadget&quot;</span>, <span style="color: #a31515">&quot;Whatchamacallit&quot;</span>, <span style="color: #a31515">&quot;Gizmo&quot;</span>,
    <span style="color: #a31515">&quot;Thingamabob&quot;</span>, <span style="color: #a31515">&quot;Thingamajig&quot;</span>, <span style="color: #a31515">&quot;Doodad&quot;</span>, <span style="color: #a31515">&quot;Doohickey&quot;</span>};

<span style="color: blue">var </span>results = values.Like(<span style="color: #a31515">&quot;*a*a*&quot;</span>);

<span style="color: blue">foreach </span>(<span style="color: blue">string </span>result <span style="color: blue">in </span>results) {
    <span style="color: #2b91af">Console</span>.WriteLine(result);
}</pre>
</div>
<p>
  <br />Which outputs:</p>
<p><a href="http://www.devexpertise.com/wp-content/uploads/ImplementingaLINQversionofSQLsLIKEOperat_C261/image.png"  rel="lightbox"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.devexpertise.com/wp-content/uploads/ImplementingaLINQversionofSQLsLIKEOperat_C261/image_thumb.png" width="669" height="86" /></a> </p>
<p>&#160;</p>
<p>Hopefully you’ll find this useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing a Theme as a SharePoint Feature</title>
		<link>http://www.devexpertise.com/2009/02/11/installing-a-theme-as-a-sharepoint-feature/</link>
		<comments>http://www.devexpertise.com/2009/02/11/installing-a-theme-as-a-sharepoint-feature/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 13:48:53 +0000</pubDate>
		<dc:creator>DevExpert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint UI]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Themes]]></category>

		<guid isPermaLink="false">http://www.devexpertise.com/2009/02/11/installing-a-theme-as-a-sharepoint-feature/</guid>
		<description><![CDATA[I briefly went over my approach to creating a custom SharePoint theme in my previous post, and I even included a downloadable solution package that you can install on your farm (provided you have the .NET 3.5 Framework installed).&#160; How did I accomplish this?&#160; Pretty easily actually.&#160; Unfortunately there isn’t much documentation or examples of [...]]]></description>
			<content:encoded><![CDATA[<p>I briefly went over my approach to creating a custom SharePoint theme in my <a href="http://www.devexpertise.com/2009/02/09/creating-a-custom-vista-theme-for-sharepoint/"  target="_blank">previous post</a>, and I even included a downloadable solution package that you can install on your farm (provided you have the .NET 3.5 Framework installed).&#160; How did I accomplish this?&#160; Pretty easily actually.&#160; Unfortunately there isn’t much documentation or examples of this out there, so allow me to put an end to that!</p>
<p>SharePoint features and solutions are absolutely essential if you want to provide an easy and maintainable method of deploying custom artifacts to your SharePoint servers.&#160; A theme is a perfect candidate for this, as everything is file-system based and located with the 12 directory.&#160; The only odd thing that throws a monkey wrench in this seemingly simple process is the SPTHEMES.XML file, which must be updated to include an entry for your custom theme.&#160; Here’s a portion of that file, with the custom theme I developed in my previous post highlighted:</p>
<p><a href="http://www.devexpertise.com/wp-content/uploads/2009/02/image43.png" ><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.devexpertise.com/wp-content/uploads/2009/02/image-thumb41.png" width="653" height="357" /></a></p>
<p>Now, you could by all means include the SPTHEMES.XML file in your solution and have it overwrite the existing file, but what if you have other themes defined in your file?&#160; That approach will overwrite it, and you’ll have to reenter everything.&#160; The recommended approach to this is to create a feature receiver that fires when the feature is installed and modifies the SPTHEMES.XML.&#160; When the feature is installed, a custom &lt;Templates&gt; section is added for the custom theme, and when it’s uninstalled it’s removed.</p>
<blockquote><p>NOTE: A feature is “installed” when you run the <strong>STSADM –o installfeature</strong> command, or when a feature is contained in a solution package and that solution package is <em>deployed</em>.</p>
</blockquote>
<p>One important thing to mention is that this feature is scoped for the farm, not an individual site-collection or site, as the files deployed to the file system are used by the entire farm.&#160; Let’s first take a look at the feature.xml file:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 1200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">&lt;?</span><span style="color: #a31515">xml </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot; <span style="color: red">encoding</span><span style="color: blue">=</span>&quot;<span style="color: blue">utf-8</span>&quot; <span style="color: blue">?&gt;
&lt;</span><span style="color: #a31515">Feature </span><span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://schemas.microsoft.com/sharepoint/</span>&quot;
         <span style="color: red">Id</span><span style="color: blue">=</span>&quot;<span style="color: blue">2D965A22-73A9-4e00-A530-06F2AF6EC89F</span>&quot;
         <span style="color: red">Title</span><span style="color: blue">=</span>&quot;<span style="color: blue">DevExpertise Vista Theme</span>&quot;
         <span style="color: red">Description</span><span style="color: blue">=</span>&quot;<span style="color: blue">Adds the DevExpertise Vista Theme</span>&quot;
         <span style="color: red">Scope</span><span style="color: blue">=</span>&quot;<span style="color: blue">Farm</span>&quot;
         <span style="color: red">Version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0.0.0</span>&quot;
         <span style="color: red">ImageUrl</span><span style="color: blue">=</span>&quot;<span style="color: blue">DevExpertise\devexpertiseLogo.png</span>&quot;
         <span style="color: red">ReceiverAssembly</span><span style="color: blue">=</span>&quot;<span style="color: blue">DevExpertise.SharePoint.Themes, Version=1.0.0.0,
                           Culture=neutral, PublicKeyToken=d39eedb6cff9b1c8</span>&quot;
         <span style="color: red">ReceiverClass </span><span style="color: blue">=</span>&quot;<span style="color: blue">DevExpertise.SharePoint.Themes.FeatureReceiver</span>&quot;<span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Feature</span><span style="color: blue">&gt;</span></pre>
</div>
<p>As you can see, it is executing a custom FeatureReceiver class.&#160; At a high-level, the receiver looks like this:</p>
<p><strong><font color="#ff0000">[UPDATED 5/27/2009]<br />
      <br />I received a couple comments that let me know that if this feature is activated on a farm with multiple front-ends, then the <em>FeatureActivated</em> event only gets fired on a single web front-end.&#160; This is absolutely TRUE, and an oversight on my part (thanks guys!).&#160; The solution is extremely simple – put your code in the <em>FeatureInstalled</em> and <em>FeatureUninstalling</em> events instead, as these get fired on EVERY web front-end in your farm!!&#160; I’ve modified the code to reflect this:</font></strong></p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 1200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">FeatureReceiver</span>: <span style="color: #2b91af">SPFeatureReceiver </span>{

    <span style="color: blue">private enum </span><span style="color: #2b91af">ModificationType </span>{ Add, Remove }

    <span style="color: blue">public override void </span>FeatureInstalled(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) {
        ModifySPTheme(<span style="color: #2b91af">ModificationType</span>.Add);

        <span style="color: green">// if necessary, loop through all sites and set theme
    </span>}

    <span style="color: blue">public override void </span>FeatureUninstalling(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) {
        ModifySPTheme(<span style="color: #2b91af">ModificationType</span>.Remove);

        <span style="color: green">// if necessary, loop through all sites and reset theme
    </span>}

    <span style="color: blue">public override void </span>FeatureActivated(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) {
        <span style="color: green">// do nothing
    </span>}

    <span style="color: blue">public override void </span>FeatureDeactivating(<span style="color: #2b91af">SPFeatureReceiverProperties </span>properties) {
        <span style="color: green">// do nothing
    </span>}
}</pre>
</div>
<p>For the sake of simplicity, I’m not including code to set the theme on any sites.&#160; Since this is a farm feature, it doesn’t make sense to set the theme for a particular site here, but by all means if you wanted to you could.&#160; Basically this feature receiver is only for modifying the SPTHEMES.XML file.</p>
<p>Let’s take a look at the ModifySPTheme() method.&#160; It uses LINQ to XML to open and parse the file, add the necessary elements when the feature is installed, and remove it when the feature is uninstalling:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">private void </span>ModifySPTheme(<span style="color: #2b91af">ModificationType </span>type) {
    <span style="color: #2b91af">XDocument </span>doc = <span style="color: blue">null</span>;
    <span style="color: #2b91af">XNamespace </span>ns = <span style="color: #a31515">&quot;http://tempuri.org/SPThemes.xsd&quot;</span>;

    <span style="color: green">// path to the SPTHEMES.XML file
    </span><span style="color: blue">string </span>spthemePath = <span style="color: #2b91af">Path</span>.Combine(<span style="color: #2b91af">SPUtility</span>.GetGenericSetupPath(<span style="color: #a31515">@&quot;TEMPLATE\LAYOUTS\1033&quot;</span>), <span style="color: #a31515">&quot;SPTHEMES.XML&quot;</span>);
    <span style="color: blue">string </span>contents = <span style="color: blue">string</span>.Empty;

    <span style="color: green">// read the contents of the SPTHEMES.XML file
    </span><span style="color: blue">using </span>(<span style="color: #2b91af">StreamReader </span>streamReader = <span style="color: blue">new </span><span style="color: #2b91af">StreamReader</span>(spthemePath)) {
        contents = streamReader.ReadToEnd();
        streamReader.Close();
    }

    <span style="color: blue">using </span>(<span style="color: #2b91af">StringReader </span>stringReader = <span style="color: blue">new </span><span style="color: #2b91af">StringReader</span>(contents.Trim())) {
        <span style="color: green">// create a new XDocument from the contents of the file
        </span>doc = <span style="color: #2b91af">XDocument</span>.Load(stringReader);

        <span style="color: green">// retrieve all elements with a TemplateID of 'VISTA'.  At most, there should only be one
        </span><span style="color: blue">var </span>element = <span style="color: blue">from </span>b <span style="color: blue">in </span>doc.Element(ns + <span style="color: #a31515">&quot;SPThemes&quot;</span>).Elements(ns + <span style="color: #a31515">&quot;Templates&quot;</span>)
                      <span style="color: blue">where </span>b.Element(ns + <span style="color: #a31515">&quot;TemplateID&quot;</span>).Value == <span style="color: #a31515">&quot;VISTA&quot;
                      </span><span style="color: blue">select </span>b;

        <span style="color: green">// determine if the VISTA theme element already exists
        </span><span style="color: blue">bool </span>exists = (element != <span style="color: blue">null </span>&amp;&amp; element.Count() &gt; 0);

        <span style="color: blue">if </span>(type == <span style="color: #2b91af">ModificationType</span>.Add) {
            <span style="color: blue">if </span>(!exists) {
                <span style="color: green">// create an XElement that defines our custom VISTA theme
                </span><span style="color: #2b91af">XElement </span>xml =
                    <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(ns + <span style="color: #a31515">&quot;Templates&quot;</span>,
                        <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(ns + <span style="color: #a31515">&quot;TemplateID&quot;</span>, <span style="color: #a31515">&quot;VISTA&quot;</span>),
                        <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(ns + <span style="color: #a31515">&quot;DisplayName&quot;</span>, <span style="color: #a31515">&quot;DevExpertise Vista Theme&quot;</span>),
                        <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(ns + <span style="color: #a31515">&quot;Description&quot;</span>, <span style="color: #a31515">&quot;A Vista-like Theme&quot;</span>),
                        <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(ns + <span style="color: #a31515">&quot;Thumbnail&quot;</span>, <span style="color: #a31515">&quot;images/DevExpertise.SharePoint.Themes/VISTA/thVISTA.gif&quot;</span>),
                        <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(ns + <span style="color: #a31515">&quot;Preview&quot;</span>, <span style="color: #a31515">&quot;images/DevExpertise.SharePoint.Themes/VISTA/thVISTA.gif&quot;</span>));

                <span style="color: green">// add the element to the file and save
                </span>doc.Element(ns + <span style="color: #a31515">&quot;SPThemes&quot;</span>).Add(xml);
                doc.Save(spthemePath);
            }
        }
        <span style="color: blue">else </span>{
            <span style="color: blue">if </span>(exists) {
                <span style="color: green">// if the element exists, remove it and save
                </span>element.Remove();
                doc.Save(spthemePath);
            }
        }

        stringReader.Close();
    }
}</pre>
</div>
<p>Pretty slick, huh? Now, keep in mind that this only modifies the SPTHEMES.XML file – it does NOT set the theme for any site.&#160; It will still be up to the user or site admins to set the theme for a given site.&#160; Also, if this theme is already applied to a site and the feature is deactivated, it won’t reset the theme – it will only remove the entry from the theme settings page in Site Settings.&#160; Finally, if a site has this theme installed and the solution package is uninstalled and/or removed, your site’s theme will be messed up because the source files are gone.&#160; It is probably beneficial to at least loop through all the sites and reset the theme to something else if and when the feature is deactivated, but I’ll leave that to you. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devexpertise.com/2009/02/11/installing-a-theme-as-a-sharepoint-feature/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
