<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Johan Leino</title>
	<atom:link href="http://johanleino.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://johanleino.wordpress.com</link>
	<description>// TODO: Add clever comment about the focus of this blog...</description>
	<lastBuildDate>Wed, 25 Jan 2012 20:01:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='johanleino.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Johan Leino</title>
		<link>http://johanleino.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://johanleino.wordpress.com/osd.xml" title="Johan Leino" />
	<atom:link rel='hub' href='http://johanleino.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Avoiding serialization issues in SharePoint property bags by using JSON</title>
		<link>http://johanleino.wordpress.com/2012/01/24/avoiding-serialization-issues-in-sharepoint-property-bags-by-using-json/</link>
		<comments>http://johanleino.wordpress.com/2012/01/24/avoiding-serialization-issues-in-sharepoint-property-bags-by-using-json/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 20:53:10 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Enterprise Library]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PropertyBag]]></category>
		<category><![CDATA[Settings]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[SharePoint Deployment]]></category>
		<category><![CDATA[SPG]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1233</guid>
		<description><![CDATA[Storing application settings in a SharePoint solution can be done in numerous ways. One of the more robust solutions is to use the property bags that exist all throughout the hierarchy of the SharePoint object model. The only real issue that we have in all of these property bags are that we can store any [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1233&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Storing application settings in a SharePoint solution can be done in numerous ways. One of the more robust solutions is to use the property bags that exist all throughout the hierarchy of the SharePoint object model.</p>
<p>The only real issue that we have in all of these property bags are that we can store any value of any type as long as it can be serialized. <strong><em>“…as long as it can be serialized…”</em></strong></p>
<p>SharePoint property bags provide an easy-to-use storage mechanism, but developers risk corrupting the configuration database or the content database if they attempt to persist non-serializable types.</p>
<p>One excellent way to get around some of these issues is to use <strong>PnP’s SharePoint Guidance</strong> (<a href="http://spg.codeplex.com/" target="_blank">SPG</a>) framework. Even though you do you might still have issues serializing types. Especially when you use it from the sandbox where you have to get around some of these issues by providing serialization assemblies to go with your sandbox settings (C# classes) that you want to store. Not so straight forward I would say.</p>
<p>One thing you can be sure of though is that you can always store a string without getting into these serialization issues. And what notation that can work as classes but internally is just a string??</p>
<p>That’s right…Json</p>
<p>So, despite if you use <a href="http://spg.codeplex.com/" target="_blank">SPG</a> (which you can and probably should combine with the following solution) or not…if you store values as <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">Json</a> (strings) you’ll never get into serialization issues.</p>
<h2><a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">JSON.NET</a> to the rescue</h2>
<p>I’ll start with a little utilities C# project (<strong><em>SP.Utilities</em></strong>) that will later serve as the repository for reading and writing settings. Now, I will start off by including <a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">JSON.NET</a> by <a href="http://james.newtonking.com/" target="_blank">James Newton-King</a> via the NuGet package manager.</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image21.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb21.png?w=444&#038;h=248" alt="image" width="444" height="248" /></a></p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image22.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb22.png?w=365&#038;h=90" alt="image" width="365" height="90" /></a></p>
<p>No, once that is installed let’s implement a simple solution for storing and retrieving settings in SharePoint.</p>
<h3>Basic Architecture</h3>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image23.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb23.png?w=572&#038;h=310" alt="image" width="572" height="310" /></a></p>
<p>Here’s the basic rundown on how I’m going to solve this issue.</p>
<ol>
<li>In the SP.Utilities project I’m constructing an extension method, <strong><em>Settings()</em></strong>, on the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank">SPWeb</a> class that returns</li>
<li>A local, internal, implementation of the <strong><em>ISettingCollection</em></strong> interface which has all the <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a> operations (GET, SET, etc).</li>
<li><strong><em>SPWebSettingsCollection</em></strong> is the internal class that does all the magic and that we will look at next.</li>
</ol>
<div></div>
<h3>SPWebSettingsCollection</h3>
<p>You will use the extension method called <strong><em>Settings</em></strong> on the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank">SPWeb</a> object which look like this:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image24.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb24.png?w=556&#038;h=139" alt="image" width="556" height="139" /></a></p>
<p>You can see that from the “outside” you’re just using an interface, <strong><em>ISettingCollection</em></strong>, but internally the actual implementation of that interface is called <strong><em>SPWebSettingsCollection</em></strong>. The <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank">SPWeb</a> object is passed along to the <strong><em>SPWebSettingsCollection</em></strong> class in the constructor.</p>
<p>You could potentially make any number of implementations of this interface, and expose them via extension methods, this is just showing you how to make one of them:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:7af448c1-a1e7-4d3a-a74a-ad1193a4ceb4" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp; highlight: [13,49]; pad-line-numbers: true; wrap-lines: false;">
    internal class SPWebSettingsCollection : ISettingCollection
    {
        SPWeb m_Web;

        public SPWebSettingsCollection(SPWeb web)
        {
            this.m_Web = web;
        }

        public ISettingCollection Set&lt;TSetting&gt;(string key, TSetting setting) 
            where TSetting : ISetting
        {
            string value = JsonConvert.SerializeObject(setting);
            this[key] = value;

            return this;
        }


        public bool TryGet&lt;TSetting&gt;(string key, out TSetting setting) 
            where TSetting : ISetting
        {
            bool output = false;
            setting = default(TSetting);

            if (this.Contains(key))
            {
                try
                {
                    setting = this.Get&lt;TSetting&gt;(key);
                    output = true;
                }
                catch (Exception e)
                {
                    // TODO: add logging
                }
            }

            return output;

        }




        public TSetting Get&lt;TSetting&gt;(string key) where TSetting : ISetting
        {
            string value = this[key];
            return JsonConvert.DeserializeObject&lt;TSetting&gt;(value);
        }

        public string this[string key]
        {
            get
            {
                return this.m_Web.AllProperties[key] as string;
            }

            set
            {
                if (this.Contains(key))
                {
                    this.m_Web.SetProperty(key, value);
                }
                else
                {
                    this.m_Web.AddProperty(key, value);
                }
                Update();
            }
        }

        public bool Contains(string key)
        {
            bool exists = false;
            if (this.m_Web.AllProperties.Contains(key))
            {
                exists = this.m_Web.AllProperties[key] != null;
            }
            return exists;
        }

        private void Update()
        {
            this.m_Web.Update();
        }


        public ISettingCollection Delete(string key)
        {
            if (this.Contains(key))
            {
                this.m_Web.DeleteProperty(key);
                Update();
            }

            return this;
        }
    }
</pre></p>
<p>Have a look at the highlighted lines (13 and 49).</p>
</div>
<p>On line <strong><em>13</em></strong>, when an object comes in to be stored I’ll start by serializing it into <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">Json</a> (a string that is) before storing it in the SPWeb’s property collection.</p>
<p>And on line <strong><em>49</em></strong>, deserialize it back from a <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">Json</a> string into the actual C# object. All of this made possible by using <a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">JSON.NET</a></p>
<p>I’m not going to go through all the methods by I think you get the idea right?</p>
<h3>Make a quick test with a console application</h3>
<p>Let’s make a quick console application to see if the concept works then.</p>
<p>First of all there is a class, <strong><em>ColorSetting</em></strong>, that we’ll use to store a color (simple enough test). It implements a “marker” interface, <strong><em>ISetting</em></strong>, simply because want I to be able to enforce things on settings later on. (optional of course)</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:94522ea8-d00b-47de-824b-8fa6d5065f9f" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp; wrap-lines: false;">
    class ColorSetting : ISetting
    {
        public string DefaultColor { get; set; }
    }

    class Program
    {
        const string ColorSettingKey = &quot;johanleino.com.ColorSetting&quot;;

        static void Main(string[] args)
        {
            using (SPSite sitecollection = new SPSite(&quot;http://www.johanleino.com&quot;))
            {
                SPWeb web = sitecollection.RootWeb;

                web
                    .Settings()
                    .Set&lt;ColorSetting&gt;(ColorSettingKey, new ColorSetting
                    {
                        DefaultColor = &quot;#000000&quot;
                    });

                var color = web
                     .Settings()
                     .Get&lt;ColorSetting&gt;(ColorSettingKey);

                System.Console.WriteLine(&quot;The default color of johanleino.com is: {0}&quot;, color.DefaultColor);


                color.DefaultColor = &quot;#333333&quot;;

                web
                    .Settings()
                    .Set&lt;ColorSetting&gt;(ColorSettingKey, color);

                var updatedColor = web
                                    .Settings()
                                    .Get&lt;ColorSetting&gt;(ColorSettingKey);

                System.Console.WriteLine(&quot;The new default color of johanleino.com is: {0}&quot;, updatedColor.DefaultColor);
            }

            System.Console.Read();
        }
    }
</pre></p>
</div>
<p>The code does in short:</p>
<p>Store setting with value #000000 –&gt; get the setting and print the value –&gt; update the setting with value #333333 –&gt; store the updated setting –&gt; get the setting again and print the new value</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image25.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb25.png?w=697&#038;h=122" alt="image" width="697" height="122" /></a></p>
<h3></h3>
<h3></h3>
<h3>How about the Sandbox?</h3>
<p>You could leave it as it is now and it will work in a farm solution situation.</p>
<p>But I’m taking it one step further though and making this work from a sandbox solution since (as a stated at the beginning of this post) that’s usually where you’ll have serialization issues.</p>
<p>So, from the sandbox you basically have two options. One of them is to construct a proxy (but if you do that you you to make some changes to the extension method…maybe another post). The second option is to deploy the SP.Utilities dll via a farm solution and use it from the sandbox code. I’m showing the second option here.</p>
<p><strong><em>In short:</em></strong> Add SP.Utilities + Json.NET to GAC via a farm scoped WSP.</p>
<p>From a new sandbox project I have a feature which will store a setting in the feature activating event:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:3c10fe61-b43e-40d3-b0ec-1ec3efe49d90" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp; wrap-lines: false;">
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {               

            SPWeb web = properties.UserCodeSite.RootWeb;

            web
                .Settings()
                .Set&lt;SiteSetting&gt;(&quot;johanleino.com.SiteSetting&quot;, new SiteSetting
                {
                    Creator = &quot;Johan Leino&quot;
                });
        }
</pre></p>
</div>
<p>Same idea as before (in the console) but this time I’m storing a setting called <strong><em>SiteSetting</em></strong> which have a property for <strong><em>Creator</em></strong>. Now if you run this code you will see:</p>
<p><strong><em>“Error occurred in deployment step &#8216;Activate Features&#8217;: Unhandled exception was thrown by the sandboxed code wrapper&#8217;s Execute method in the partial trust app domain: An unexpected error has occurred”</em></strong></p>
<p>That’s because a sandbox solution is a partial trust caller. So to SP.Utilities you have to add this:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image26.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb26.png?w=331&#038;h=25" alt="image" width="331" height="25" /></a></p>
<p>To test the setting once it has been added via the feature I’ll add a simple web part to display the value of the setting:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image27.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb27.png?w=626&#038;h=227" alt="image" width="626" height="227" /></a></p>
<p>Build –&gt; Deploy –&gt; Add web part to a page and…:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image28.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb28.png?w=614&#038;h=265" alt="image" width="614" height="265" /></a></p>
<p><strong><span style="font-size:medium;">TADA!!</span></strong></p>
<p>Easy right!?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1233/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1233/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1233/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1233&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2012/01/24/avoiding-serialization-issues-in-sharepoint-property-bags-by-using-json/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

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

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

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

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

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

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

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

		<media:content url="http://johanleino.files.wordpress.com/2012/01/image_thumb28.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Inconvenient: Files inside mapped folder which links to existing items</title>
		<link>http://johanleino.wordpress.com/2012/01/19/inconvenient-files-inside-mapped-folder-which-links-to-existing-items/</link>
		<comments>http://johanleino.wordpress.com/2012/01/19/inconvenient-files-inside-mapped-folder-which-links-to-existing-items/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 20:55:50 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[SharePoint Deployment]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1212</guid>
		<description><![CDATA[Problem You’re working on a SharePoint site for company x in Visual Studio 2010. Your boss tells you that company x uses a graphical profile (css + images) and some javascript files that you “must” include in the new site. The boss also informs you that if you make any changes to these files they [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1212&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Problem</h2>
<p>You’re working on a SharePoint site for company x in Visual Studio 2010. Your boss tells you that company x uses a graphical profile (css + images) and some javascript files that you “must” include in the new site. The boss also informs you that if you make any changes to these files they have to stay in sync with the originals.</p>
<p>Finally:<br />
<strong><em>“And I don’t want to see any copy/paste action between these files…keep them in sync with each other OK?!”</em></strong>…the boss tells you.</p>
<p>The main issue is that these files are not included in “your” part of the source control, hence they reside somewhere else in the file structure and you have to find a way to seamlessly include them in your SP solution before you can deploy it.</p>
<p>Hey, I can use “..add existing item” you say and add the files as <strong><em>links</em></strong> to the actual files. Then if I update the files (or someone else does) it will update the original ones also since we’re just adding links to the original ones.</p>
<p>OK, so let’s try that approach then and see what happens then.</p>
<p><em><strong>Expect to see problems…</strong></em></p>
<h2>Mapped Folders</h2>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image7.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb7.png?w=659&#038;h=349" alt="image" width="659" height="349" /></a></p>
<p>So here’s an example on how this setup might look from a source file (windows explorer) perspective.<br />
You have one folder called <strong>JSLib</strong> which holds the <strong><em>company.js</em></strong> file which is the file that we will try to include into the other folder <strong><em>johanleino.com</em></strong> which is the actual VS SharePoint project.</p>
<p>What comes to mind here is to use a mapped folder synced to “Layouts” and add the company.js file in there.</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image8.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb8.png?w=669&#038;h=315" alt="image" width="669" height="315" /></a></p>
<p>So, there is a mapped folder in the project already. I’m adding the company <strong>JSLib</strong> files into that folder (subfolder actually that’s called company-x under johanleino.com…<strong><span style="color:#ff0000;">please note</span></strong> that screenshot doesn’t show exactly that though).</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image9.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb9.png?w=669&#038;h=262" alt="image" width="669" height="262" /></a></p>
<p>So, here I’m picking the <strong><em>company.js</em></strong> file from the <strong>JSLib</strong> folder from the “root” of the source file structure. It’s added as a link into my SP project’s mapped folder named <strong><em>Layouts/johanleino.com/company-x</em></strong></p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image10.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb10.png?w=316&#038;h=120" alt="image" width="316" height="120" /></a></p>
<p>You can see that VS “marks” that file with an icon telling me that the file is linked (the original is not actually in that place…it’s just a link).</p>
<h3>So, are we finished now??</h3>
<p>Let’s look in the WSP package explorer designer:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image11.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb11.png?w=361&#038;h=156" alt="image" width="361" height="156" /></a></p>
<p>All files that are in mapped folders (at least a mapped folder that deploys to layouts) are so-called <strong>template files</strong> (<strong>deployment type = templatefile</strong> in VS SP tools).<br />
This means that they are deployed somewhere beneath the <strong><em>TEMPLATE</em></strong> directory in the SharePoint root files. This is however not something you can see from the package explorer in VS (as you can see from the image above).</p>
<p>To see the actual deployment location we have to look at the properties for the file (company.js) or package and look in the WSP:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image12.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb12.png?w=653&#038;h=234" alt="image" width="653" height="234" /></a></p>
<p>As you can see it looks kind of weird (<strong><em>Layouts\..\..\JSLib</em></strong>) and you can’t change that value.<br />
<strong><span style="color:#ff0000;font-size:medium;">That’s the inconvenience!!</span></strong></p>
<p>And you guessed right…trying to deploy the file will result in:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image13.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb13.png?w=608&#038;h=256" alt="image" width="608" height="256" /></a></p>
<p>…no trace of the company.js file or the subfolder I placed it in.</p>
<h2>Solution: How to trick Visual Studio</h2>
<p>OK, so mapped folders are normally great since we can hook it up directly to a folder in SP root files and it displays like a folder in the VS IDE, it syncs the files, etc etc&#8230;</p>
<p>But in this case where we need more control over the deployment we’ll use a “regular” <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a>.</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image14.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb14.png?w=463&#038;h=124" alt="image" width="463" height="124" border="0" /></a></p>
<p>…add new item on so on (I’ll go with empty element type).</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image15.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb15.png?w=537&#038;h=130" alt="image" width="537" height="130" /></a></p>
<p>I’ve named the <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a> <strong>JSLib</strong> and I’ve completed all the same steps as for the mapped folder approach (add existing item, browse to the file, add as links…etc) so that company.js is included as a link in the <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a>.</p>
<p>The next step is to tell VS SP tools that we want to deploy that file as a template file, like:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image16.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb16.png?w=525&#038;h=328" alt="image" width="525" height="328" /></a></p>
<p>Oops…you might get an exception (just ignore it though).</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image17.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb17.png?w=240&#038;h=195" alt="image" width="240" height="195" /></a></p>
<p>As a result of that exception (or so) you have to “handcraft” the path beneath the <strong>TEMPLATE</strong> directory where you want this file to end up (which you couldn’t do with the mapped folder files):</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image18.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb18.png?w=601&#038;h=103" alt="image" width="601" height="103" /></a></p>
<p>The final step is to use the package explorer to include the <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a> in the packaged output (WSP).<br />
“Move from the left side to the right side…”</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image19.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb19.png?w=600&#038;h=105" alt="image" width="600" height="105" /></a></p>
<p><strong><span style="font-size:medium;">Package –&gt; Deploy</span></strong> …and:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image20.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb20.png?w=609&#038;h=330" alt="image" width="609" height="330" /></a></p>
<p><strong><span style="font-size:medium;">TADA!!</span></strong></p>
<p>A plus is that the CKS:Dev tools extensions for VS 2010 will also pick this up (hence, you can still use the “copy to SharePoint root command” or “Auto copy on save”)</p>
<p>Enjoy and hope the MS fixes this until VS11 releases…right!?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1212/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1212&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2012/01/19/inconvenient-files-inside-mapped-folder-which-links-to-existing-items/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

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

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

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

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

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

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

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

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

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

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

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

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

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

		<media:content url="http://johanleino.files.wordpress.com/2012/01/image_thumb20.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting to know your SharePoint 2010 packaging options</title>
		<link>http://johanleino.wordpress.com/2012/01/09/getting-to-know-your-sharepoint-2010-packing-options/</link>
		<comments>http://johanleino.wordpress.com/2012/01/09/getting-to-know-your-sharepoint-2010-packing-options/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 18:16:17 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[SharePoint Deployment]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1173</guid>
		<description><![CDATA[Have a look at the image above. If you’re familiar with working with SharePoint 2010 projects in VS2010 you should know that when you add a new SPI (SharePoint Project Item), like a module&#124;content type&#124;empty item&#124;etc, it’s by default added to a feature of the “correct” scope. Waldek Mastykarz has more information about this, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1173&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://johanleino.files.wordpress.com/2012/01/image.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb.png?w=379&#038;h=332" alt="image" width="379" height="332" /></a></p>
<p>Have a look at the image above.</p>
<p>If you’re familiar with working with SharePoint 2010 projects in VS2010 you should know that when you add a new <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a> (<em><strong>SharePoint Project Item</strong></em>), like a <strong><em>module|content type|empty item|etc</em></strong>, it’s by default added to a feature of the “correct” scope.<br />
<a href="http://blog.mastykarz.nl/easier-working-sharepoint-project-items-mavention-spi-references/" target="_blank">Waldek Mastykarz</a> has more information about this, and a way around it, if you’re interested.</p>
<p>The image below tries to illustrate what happens to your items (in this example an element file) if the feature is removed from the package (if the feature is removed, the <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a> is removed from the package&#8230;by default that is):</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image1.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb1.png?w=667&#038;h=551" alt="image" width="667" height="551" /></a></p>
<p>So who cares you might say. When will I ever need to pay attention to this?</p>
<p>Well, have a look at the following scenario then…</p>
<h2>Issue: adding a root file</h2>
<p>What I want to accomplish now is to add a configuration file to the <strong><em>14/CONFIG</em></strong> folder upon deploying the WSP.<br />
This will be accomplished by adding the configuration file to an empty <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a> and setting the deployment type to <strong><em>root file</em></strong> (like shown below).</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image2.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb2.png?w=575&#038;h=158" alt="image" width="575" height="158" /></a></p>
<p><strong><em>Note</em></strong> that I also needed to change the “default” Path property value to CONFIG (+ subdir if you want one) to make the file end up in the correct place among the 14” folders.</p>
<h3>But hey wait a minute now!!</h3>
<p>…you might say. You could just use a <strong><em>mapped folder</em></strong> right?</p>
<p>Well, yes you could but there are two reasons why I can’t do that:</p>
<ol>
<li>I want to make a point that you have to look out in these kinds of situations.</li>
<li>I’m (in an upcoming post I’m working on) using a configuration transformation on this file and I don’t want my CONFIG directory cluttered with configuration files (I’ll show you what I mean in an upcoming post) since you can’t control which files under a mapped folder that are actually included in the WSP ouput. (only code files C#+VB are removed by default)</li>
</ol>
<p>OK, back on track. If you now package this project up as WSP (without including the <a href="http://msdn.microsoft.com/en-us/library/ee231554.aspx" target="_blank">SPI</a> in a feature as the default option is) there will be no trace of the configuration file in the WSP (as the packing explorer below shows):</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image3.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb3.png?w=556&#038;h=225" alt="image" width="556" height="225" /></a></p>
<h2>Solution: you basically have two options</h2>
<ol>
<li>Include the SPI inside a feature (as shown before)</li>
<li>Use the package manager UI to add it to the WSP (shown below)</li>
</ol>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image4.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb4.png?w=592&#038;h=328" alt="image" width="592" height="328" /></a></p>
<p>Now, look at the packing explorer again and you should see this:</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image5.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb5.png?w=544&#038;h=335" alt="image" width="544" height="335" /></a></p>
<p>And subsequently after deploying your package the file appears at the correct place in the root files directory on the server/s.</p>
<p><a href="http://johanleino.files.wordpress.com/2012/01/image6.png"><img style="display:inline;" title="image" src="http://johanleino.files.wordpress.com/2012/01/image_thumb6.png?w=575&#038;h=175" alt="image" width="575" height="175" /></a></p>
<h2>To summarize</h2>
<p>Two basic things to take away from this post are:</p>
<ol>
<li>Get to know your deployment type options (i.e. how does the type of SPI affect your package)</li>
<li>Get to know the packaging explorer and the management UI (it might be useful to alter the VS packaging system sometimes)</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1173/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1173&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2012/01/09/getting-to-know-your-sharepoint-2010-packing-options/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

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

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

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

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

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

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

		<media:content url="http://johanleino.files.wordpress.com/2012/01/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>2011 in review</title>
		<link>http://johanleino.wordpress.com/2012/01/04/2011-in-review/</link>
		<comments>http://johanleino.wordpress.com/2012/01/04/2011-in-review/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 12:47:04 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[annual report]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://johanleino.wordpress.com/?p=1154</guid>
		<description><![CDATA[One more year has passed and looking at the stats from past year two things to notice are that I blogged less (18 compared to 26) times in 2011 compared to 2010. This is not OK and I have a couple of posts in the making but haven&#8217;t really found the time to complete them [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1154&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One more year has passed and looking at the stats from past year two things to notice are that I blogged less (18 compared to 26) times in 2011 compared to 2010. This is not OK and I have a couple of posts in the making but haven&#8217;t really found the time to complete them yet. Hoping to improve during 2012 though&#8230;</p>
<p>My blog was viewed 10,000 times more though than in 2011 which is quite nice&#8230;!!</p>
<p>Here&#8217;s the stats for 2011 and happy new 2012 to you all!!<br />
The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.</p>
<p><a href="/2011/annual-report/"><img src="http://www.wordpress.com/wp-content/mu-plugins/annual-reports/img/emailteaser.jpg" alt="" width="100%" /></a></p>
<p>Here&#8217;s an excerpt:</p>
<blockquote><p>The concert hall at the Syndey Opera House holds 2,700 people. This blog was viewed about <strong>37,000</strong> times in 2011. If it were a concert at Sydney Opera House, it would take about 14 sold-out performances for that many people to see it.</p></blockquote>
<p><a href="/2011/annual-report/">Click here to see the complete report.</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1154&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2012/01/04/2011-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://www.wordpress.com/wp-content/mu-plugins/annual-reports/img/emailteaser.jpg" medium="image" />
	</item>
		<item>
		<title>Using a different master page for authenticated users in SharePoint</title>
		<link>http://johanleino.wordpress.com/2011/10/20/using-a-different-master-page-for-authenticated-users-in-sharepoint/</link>
		<comments>http://johanleino.wordpress.com/2011/10/20/using-a-different-master-page-for-authenticated-users-in-sharepoint/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 17:25:00 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[ECM]]></category>
		<category><![CDATA[Master Pages]]></category>
		<category><![CDATA[Public Facing Internet Sites]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1147</guid>
		<description><![CDATA[So, let’s say you’re building a public facing internet site using SharePoint and you want to conditionally hide content from anonymous users viewing your site from the internet. Traditionally, you would use either a SPSecurityTrimmedControl or a LoginView. In this post I’m going to show you yet another way to accomplish sort of the same [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1147&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So, let’s say you’re building a public facing internet site using SharePoint and you want to conditionally hide content from anonymous users viewing your site from the internet.</p>
<p>Traditionally, you would use either a <a href="http://blog.mastykarz.nl/inconvenient-spsecuritytrimmedcontrol-revisited/" target="_blank">SPSecurityTrimmedControl</a> or a <a href="http://blog.mastykarz.nl/conditional-content-anonymous-users-loginview/" target="_blank">LoginView</a>.</p>
<p>In this post I’m going to show you yet another way to accomplish sort of the same thing (or something that you could combine with the above mentioned approaches to achieve the desired effect).</p>
<p>One of the things that you’ll probably want to “hide”, or in practice not render at all, from anonymous users is the <strong>ribbon</strong>. So from my point of view that shouldn’t even have to be in the master page for anonymous users and thereby reducing the complexity of that master page markup (also for the developer).<br />
This will require me to use an alternative master page that is rendered for users who are authenticated to show them the ribbon so that they can work with page (edit, check-in, publish, and such).</p>
<p>&nbsp;</p>
<p><span class="Apple-style-span" style="font-size:20px;font-weight:bold;"> </span></p>
<h2>The authenticated Master Page approach</h2>
<p>What you’ll need:</p>
<ul>
<li>Custom Master Page/s</li>
<li>Custom Page Layout/s</li>
</ul>
<p>But since this is public facing internet site I’ll assume that you’ll want that anyway.</p>
<h3></h3>
<h3>Public.com</h3>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb4.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image_thumb4" src="http://johanleino.files.wordpress.com/2011/10/image_thumb4_thumb.png?w=635&#038;h=138" alt="image_thumb4" width="635" height="138" border="0" /></a></p>
<p>So for the purpose of demoing this I’ve setup a web application called <strong>public.com</strong></p>
<p><strong>public.com</strong> is hosted on an internal url (<strong>http://private.public.com</strong>) which is setup to <strong><span style="text-decoration:underline;">not</span></strong> allow anonymous users. I’ve created an extended web application (<strong>http://www.public.com</strong>) that allows anonymous users (or should I say enabled for anonymous access since the actual allowing of anonymous users is configured on the site collection)</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb8.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image_thumb8" src="http://johanleino.files.wordpress.com/2011/10/image_thumb8_thumb.png?w=630&#038;h=447" alt="image_thumb8" width="630" height="447" border="0" /></a></p>
<p>So OOB both these “sites” (yes, same site but with different urls and authentication) show the <strong>ribbon</strong>, although the anonymous one only shows a “sign in” option. Well, we don’t want <a href="http://www.public.com">www.public.com</a> to show any ribbon at all.</p>
<h3></h3>
<h3>Master Pages</h3>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb12.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" title="image_thumb12" src="http://johanleino.files.wordpress.com/2011/10/image_thumb12_thumb.png?w=454&#038;h=416" alt="image_thumb12" width="454" height="416" border="0" /></a></p>
<p>You’ll need two master pages. We’ll look at how these master pages look in short but for now a feature provisions two modules with a master page in each of these modules.</p>
<h4></h4>
<h4>Module Element</h4>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb15.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" title="image_thumb15" src="http://johanleino.files.wordpress.com/2011/10/image_thumb15_thumb.png?w=631&#038;h=342" alt="image_thumb15" width="631" height="342" border="0" /></a></p>
<p>The only difference between these modules is that “<strong>private.master</strong>” is instructed to be hidden (<strong>PublishingHidden=true</strong>). So as you can see, after activating the feature, only <strong>public.master</strong> shows up as an option in the SharePoint picker. That’s by purpose.</p>
<h4></h4>
<h4>public vs. private</h4>
<p>So, what’s difference between these master pages then?<br />
Well, I’m not going to go through all of the markup but the basic idea is that <strong>public.master</strong> has as little of the SharePoint stuff as possible and <strong>private.master</strong> will include that so the admins can work with the site in a normal way.</p>
<p>Here’s a brief look a the <strong>public.master</strong>:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb19.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" title="image_thumb19" src="http://johanleino.files.wordpress.com/2011/10/image_thumb19_thumb.png?w=650&#038;h=371" alt="image_thumb19" width="650" height="371" border="0" /></a></p>
<p>So things to notice:</p>
<ul>
<li>Head tag doesn’t include any CustomJsUrl or ScriptLinks or any of that stuff.</li>
<li>It still includes the placeholders that we are used to (notice <strong>PlaceHolderAdditionalPageHead</strong>for example)</li>
<li>Body content is also pretty clear, no ribbon.</li>
<li>Body content includes two custom placeholders (<strong>PlaceHolderBeforeMain</strong> and <strong>PlaceHolderAfterMain</strong>). We will use these shortly.</li>
</ul>
<p>Now let’s look a <strong>private.master</strong>:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb22.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image_thumb22" src="http://johanleino.files.wordpress.com/2011/10/image_thumb22_thumb.png?w=656&#038;h=41" alt="image_thumb22" width="656" height="41" border="0" /></a></p>
<p><strong>private.master</strong> inherits from (or uses <strong>public.master</strong>) using the <strong>MasterPageFile</strong> attribute.</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb28.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image_thumb28" src="http://johanleino.files.wordpress.com/2011/10/image_thumb28_thumb.png?w=617&#038;h=239" alt="image_thumb28" width="617" height="239" border="0" /></a></p>
<p>We start by overriding <strong>PlaceHolderAdditionalPageHead</strong> from <strong>public.master</strong> and start to insert a bunch of SharePoint stuff. Important thing to notice is that we repeat the placeholder again as a last option to enable page layouts to insert their custom stuff into the head tag.</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb31.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image_thumb31" src="http://johanleino.files.wordpress.com/2011/10/image_thumb31_thumb.png?w=615&#038;h=328" alt="image_thumb31" width="615" height="328" border="0" /></a></p>
<p>Next up is to insert the <strong>ribbon</strong>. We’ll do that before the <strong>PlaceHolderMain</strong> so using the <strong>PlaceHolderBeforeMain</strong> from <strong>public.master</strong> is the place to do that.</p>
<p>Next up is to repeat the <strong>PlaceHolderMain</strong> placeholder (important) since we’ll want content from page layouts here.</p>
<p>And finally add some things after the page content like the developer dashboard using the <strong>PlaceHolderAfterMain</strong> placeholder.</p>
<h3></h3>
<h3>Page Layout</h3>
<p>So, I briefly mentioned that you’ll need custom page layouts to make this work. Here’s why.</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb36.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" title="image_thumb36" src="http://johanleino.files.wordpress.com/2011/10/image_thumb36_thumb.png?w=633&#038;h=448" alt="image_thumb36" width="633" height="448" border="0" /></a></p>
<p>In order to alter the master page based on whether or not the user is authenticated we’ll need a custom page layout. In the image above you can see that I’ve created a page layout (<strong>welcome-default.aspx</strong>) which is a custom page layout that can be used for welcome pages (the content type).</p>
<p>In the page directive for that page you can see that it inherits (code behind) from<strong> PageLayoutBase</strong> (normally a page layout would inherit from <strong>PublishingLayoutPage</strong> which is a SharePoint class).</p>
<p>That’s actually a class that I’ve created and that’s the class that does all the magic. Now let’s have a look at it:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:16f69c47-7503-4b90-a575-7e8feefc926b" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp; wrap-lines: false;">
namespace Public.SharePoint.PageLayouts
{
    public class PageLayoutBase : PublishingLayoutPage
    {

        protected override void OnPreInit(EventArgs e)
        {
            // this sets the MasterPageFile to public.master (if the web has been setup to use that)
            base.OnPreInit(e);

            try
            {
                if (SPContext.Current.Web.CurrentUser != null &amp;&amp; this.MasterPageFile.EndsWith(&quot;public.master&quot;, StringComparison.OrdinalIgnoreCase))
                {
                    //the user is logged in and the site uses the public.master

                    // change it to private.master
                    this.MasterPageFile = SPUrlUtility.CombineUrl(SPContext.Current.Site.ServerRelativeUrl, string.Concat(&quot;_catalogs/masterpage/&quot;, &quot;private.master&quot;));

                }

            }
            catch { } // this is an error trap, don't do anything
        }
    }
}
</pre></p>
</div>
<p>So, if you want to set the master page for a page (asp.net) using code you’ll need to override <strong>OnPreInit</strong>. This is actually what SharePoint does internally and reads the value from the <strong>SPWeb.CustomMasterUrl</strong> property.</p>
<p>What I’m doing is just checking if we have a user (i.e. if the user has authenticated) plus if the page uses the public.master, then I’ll change it to use private.master</p>
<p>This is just an example of how you could accomplish this. There’s of course a couple of hardcodings in there that could be abstracted an such.</p>
<p>Also, now it checks if the user is authenticated. Another option could be to check if the IIS settings instead…like this:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/10/image_thumb38.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image_thumb38" src="http://johanleino.files.wordpress.com/2011/10/image_thumb38_thumb.png?w=624&#038;h=45" alt="image_thumb38" width="624" height="45" border="0" /></a></p>
<h2></h2>
<h2></h2>
<h2>And the end result?</h2>
<h2><a href="http://johanleino.files.wordpress.com/2011/10/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/10/image_thumb.png?w=632&#038;h=487" alt="image" width="632" height="487" border="0" /></a></h2>
<p>Here’s how the same page looks viewed with the <strong>public.master</strong> and with the <strong>private.master</strong> (which changes dynamically)</p>
<h2></h2>
<h2>And in summary</h2>
<p>You could still combine this approach with login views and SPSecurityTrimming if you’d want that. For me at least, this is a bit cleaner approach than just “hiding” content from anonymous users. The markup looks better using this approach.</p>
<p>Enjoy!!</p>
<p>Download source code from <a href="http://johanleino.codeplex.com/releases/view/75450" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1147&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2011/10/20/using-a-different-master-page-for-authenticated-users-in-sharepoint/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb4_thumb.png" medium="image">
			<media:title type="html">image_thumb4</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb8_thumb.png" medium="image">
			<media:title type="html">image_thumb8</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb12_thumb.png" medium="image">
			<media:title type="html">image_thumb12</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb15_thumb.png" medium="image">
			<media:title type="html">image_thumb15</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb19_thumb.png" medium="image">
			<media:title type="html">image_thumb19</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb22_thumb.png" medium="image">
			<media:title type="html">image_thumb22</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb28_thumb.png" medium="image">
			<media:title type="html">image_thumb28</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb31_thumb.png" medium="image">
			<media:title type="html">image_thumb31</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb36_thumb.png" medium="image">
			<media:title type="html">image_thumb36</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb38_thumb.png" medium="image">
			<media:title type="html">image_thumb38</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/10/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Using PowerShell to list all features with Title and ID</title>
		<link>http://johanleino.wordpress.com/2011/09/19/using-powershell-to-list-all-features-with-title-and-id/</link>
		<comments>http://johanleino.wordpress.com/2011/09/19/using-powershell-to-list-all-features-with-title-and-id/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 06:10:19 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Features]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1121</guid>
		<description><![CDATA[Ever wanted to list all the features installed in the farm with their id and Title (not the name of the folder)?? Here’s the PS script to run then: It will sort all the features based on title (remember to add another language if you don’t want 1033 which is US English). Then it will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1121&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to list all the features installed in the farm with their id and Title (not the name of the folder)??</p>
<p>Here’s the PS script to run then:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e2dc28b4-20c0-4f9c-89a5-273f0a1df085" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: powershell; pad-line-numbers: true;">
Get-SPFeature | Sort @{Expression={$_.GetTitle(1033)}} -Descending:$false | ft @{Label='ID';Width=40;Expression={$_.Id}}, @{Label='Title';Width=250;Expression={$_.GetTitle(1033)}} | Out-File -Width 350 &quot;c:\TEMP\features.txt&quot;
</pre></p>
</div>
<p>It will sort all the features based on title (remember to add another language if you don’t want 1033 which is US English).<br />
Then it will expand the title property and output the whole thing into a text file.</p>
<p>Like this:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/09/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://johanleino.files.wordpress.com/2011/09/image_thumb1.png?w=487&#038;h=361" alt="image" width="487" height="361" border="0" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1121/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1121&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2011/09/19/using-powershell-to-list-all-features-with-title-and-id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/09/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>What does SPSolution.ContainsWebApplicationResource really mean?</title>
		<link>http://johanleino.wordpress.com/2011/09/16/what-does-spsolution-containswebapplicationresource-really-mean/</link>
		<comments>http://johanleino.wordpress.com/2011/09/16/what-does-spsolution-containswebapplicationresource-really-mean/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 06:36:45 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1116</guid>
		<description><![CDATA[If you look at the documentation for this property on MSDN it says “Gets a value indicating whether the solution contains any web application-specific resources to deploy.” So, at least to me, it wasn’t totally super clear what a web application specific resource really resource meant. Let’s see an example to get a better understanding: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1116&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you look at the documentation for this property on <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spsolution.containswebapplicationresource.aspx" target="_blank">MSDN </a>it says <em>“Gets a value indicating whether the solution contains any web application-specific resources to deploy.”</em></p>
<p>So, at least to me, it wasn’t totally super clear what a web application specific resource really resource meant.<br />
Let’s see an example to get a better understanding:</p>
<h2>The WSP.ContainsWebApplicationResource SharePoint Project</h2>
<p>I’ve created a basic SP project in VS 2010 and deployed it, without adding anything to it, to the local farm.<br />
Let’s first see what that gives us:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/09/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://johanleino.files.wordpress.com/2011/09/image_thumb.png?w=617&#038;h=39" alt="image" width="617" height="39" border="0" /></a></p>
<p>::false</p>
<p>OK, but that was excepted right.</p>
<p>Let’s add a web application scoped feature to it. That should be web application specific…or?</p>
<p>::false</p>
<p>No, a web application scoped feature isn’t web application specific. It can be activated on a web application but it isn’t specific to a specific web application.</p>
<p>How about a site collection scoped feature then? Let’s add site scoped feature (but not include anything in it…emtpy feature).</p>
<p>::false</p>
<p>Strange…? An empty site collection (or site scoped) feature still returns false.</p>
<p>Let’s try adding a web part (or any SPI) to the feature then.</p>
<p>::true</p>
<h2>Summary</h2>
<p>So it turns out that the <strong>ContainsWebApplicationResource</strong> really means that the WSP contains elements (in VS those are called <strong>SPIs</strong>) that are included in a feature that is scoped to either <strong>SPSite</strong> or <strong>SPWeb</strong>.</p>
<p>When would you use this property then?</p>
<p>Well, it’s very common to use when you’re writing a PS script that deploys your WSP to the farm…like this little example (sorry about the formatting):</p>
<blockquote><p>if($SPSolution.ContainsWebApplicationResource) {</p>
<p>$SPSolution | Install-SPSolution -GACDeployment:$ContainsGlobalAssembly<br />
-CASPolicies:$ContainsCasPolicy<br />
-WebApplication $WebApplicationUrl -Confirm:$false -Force -ErrorAction:SilentlyContinue<br />
-ErrorVariable e | Out-Null</p>
<p>}</p></blockquote>
<blockquote><p>} else {<br />
$SPSolution | Install-Solution -GACDeployment:$ContainsGlobalAssembly<br />
-CASPolicies:$ContainsCasPolicy<br />
-AllWebApplications:$true -Confirm:$false -Force -ErrorAction:SilentlyContinue -ErrorVariable e | Out-Null</p>
<p>}</p></blockquote>
<p>If the SPSolution object has web application resources you have the ability to deploy it to one or more web applications, hence I don’t have to deploy it globally across the farm.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1116/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1116&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2011/09/16/what-does-spsolution-containswebapplicationresource-really-mean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/09/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>jQuery: implementing custom selectors for jQuery Templates plugin</title>
		<link>http://johanleino.wordpress.com/2011/08/04/jquery-implementing-custom-selectors-for-jquery-templates-plugin/</link>
		<comments>http://johanleino.wordpress.com/2011/08/04/jquery-implementing-custom-selectors-for-jquery-templates-plugin/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 15:40:00 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[JQuery Templating Plugin]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1111</guid>
		<description><![CDATA[I’ve been working a lot over the last couple of months with jQuery and the Templates plugin written by Microsoft (wondering if that will ever leave the beta phase??). What I’ve come to love about it is the flexibility and nice separation of concerns it introduces. However, I still felt that it was a little [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1111&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I’ve been working a lot over the last couple of months with <a href="http://jquery.com/" target="_blank">jQuery</a> and the Templates plugin written by Microsoft (wondering if that will ever leave the beta phase??).<br />
What I’ve come to love about it is the flexibility and nice separation of concerns it introduces.<br />
However, I still felt that it was a little bit “ugly” to write the actual code that connects a <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> result with a template rendering stage.<br />
Luckily, <a href="http://jquery.com/" target="_blank">jQuery</a> has some great extension points that can make this easier and I found some great articles by <a href="http://james.padolsey.com/javascript/extending-jquerys-selector-capabilities/" target="_blank">James Podolsey</a> and from  <a href="http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html" target="_blank">jQuery HowTo</a> that really helped my write my own extensions. So here goes…</p>
<h2>Custom Selector Templates</h2>
<p>Before going into actually implementing the custom selectors we want for the <a href="http://api.jquery.com/category/plugins/templates/" target="_blank">jQuery Template Plugin</a> we’ll first have a look at how a custom selector works by providing a little example.</p>
<p>Similar to the built-in custom selectors that extend <a href="http://jquery.com/" target="_blank">jQuery</a>’s CSS selectors like <strong>:contains</strong> and <strong>:even</strong> you can write your own custom selectors using a predefined template model that looks like this (below are two examples of implementing the exact same selector which is called <strong>:test</strong>):</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb.png?w=603&#038;h=490" alt="image" width="603" height="490" border="0" /></a></p>
<p><strong><em>Note:</em></strong> that a custom selector can take up to four arguments (<strong>current, index, metadata, and elements</strong>) which we will have a look at shortly.</p>
<p>The two approaches (<strong><em>marked with red and blue</em></strong>) for extending <a href="http://jquery.com/" target="_blank">jQuery</a> with custom selectors are equal though I prefer the second approach (which is also better if you want more that one selector in the same self calling function).</p>
<p><strong><span style="text-decoration:underline;">Example usages:</span></strong></p>
<ul>
<li><strong>$(:test)</strong></li>
<li><strong>$(:test(‘one’))</strong></li>
<li><strong>$(p:test)</strong></li>
<li><strong>$(:test, div)</strong></li>
<li><strong>…</strong></li>
</ul>
<p>Now let’s test the selector by using <strong><span style="font-size:medium;">$(p:test(‘one’))</span></strong> and see what happens, remember that the <strong>:test</strong> selector will log its arguments to the console window for now so we can have a look at them.</p>
<p>This is the html that the selector will execute on (three p-tags):</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image51.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image51_thumb.png?w=418&#038;h=111" alt="image" width="418" height="111" border="0" /></a></p>
<p>So <a href="http://jquery.com/" target="_blank">jQuery</a> will actually first select all P-tags, using the <a href="http://api.jquery.com/element-selector/" target="_blank">element selector</a>, and then execute the :test selector on the result so we would expect three calls to be seen in the console window.<br />
As a side note it’s always good to limit the selector calls as much as possible or should I say narrow them, perhaps by using a context so the search doesn’t need to look at the entire DOM tree. We’ll come back to that later on.</p>
<h3>The arguments</h3>
<p>Remember that I said that the selector function can take up to four arguments (<strong><em>test: function (current, index, metadata, elements</em></strong>))</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb1.png?w=576&#038;h=144" alt="image" width="576" height="144" border="0" /></a></p>
<p>As you can see we get four arguments from the call <strong><em>$(p:test(‘one’))</em></strong>:</p>
<ol>
<li>the <strong>current</strong>DOM element (the p-tag in the example)</li>
<li>the <strong>index</strong>of the current DOM element (within all the elements to execute the selector on)</li>
<li>some <strong>metadata</strong>about the selector (well look more into detail on this array in a bit)</li>
<li>finally, an array containing all the <strong>elements</strong> to run the selector on (since we executed it on p-tags we’ll get all three p-tags)</li>
</ol>
<h3>The metadata argument</h3>
<p>This is the interesting part. Remember the call, <strong>$(p:test(‘one’))</strong></p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image2.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb2.png?w=566&#038;h=165" alt="image" width="566" height="165" border="0" /></a></p>
<p>So looking at the metadata argument (which is an array) we can see that also contains four elements:</p>
<ol>
<li>the complete selector</li>
<li>the name of the selector</li>
<li>the quote symbol used around the arguments, if used</li>
<li>the arguments used by the selector (in the example <strong>‘one’</strong>)</li>
</ol>
<p>Let’s just try to call the selector without any arguments just to see the difference:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image3.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb3.png?w=240&#038;h=53" alt="image" width="240" height="53" border="0" /></a></p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image4.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb4.png?w=240&#038;h=203" alt="image" width="240" height="203" border="0" /></a></p>
<p>You can see that the quote symbol and arguments are <strong>undefined</strong>.</p>
<p>Lastly, keep in mind that a selector should always return <strong>true</strong> or <strong>false</strong> depending on whether you want the current element to be included in the result. In this example <strong><em>$(p:test).size()</em></strong> will return <strong>0</strong> since we always return false from the selector, just saying.</p>
<p>Now I think we are ready to implement the custom selectors for the jQuery Templates Plugin.</p>
<h2>jQuery Templates Plugin Selectors</h2>
<p>Let’s first briefly see how we would use the template plugin OOB  (so to say) to see what improvement can be made.<br />
Consider this template:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image5.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb5.png?w=543&#038;h=251" alt="image" width="543" height="251" border="0" /></a></p>
<p>We have a DIV that works as the <strong>target</strong> for the rendered content that will be executed on the SCRIPT tag that has the <strong>template</strong> information. Let’s see how that is rendered now.</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image6.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb6.png?w=536&#038;h=134" alt="image" width="536" height="134" border="0" /></a></p>
<p>From a <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> result we’ll start by finding the <strong>container</strong> (or target) using the ID selector.<br />
Then <strong>render</strong> the <strong>template</strong> (and here we can see an example of specifying the context, hence when looking for the SCRIPT tag we’ll limit the scope to search in by telling <a href="http://jquery.com/" target="_blank">jQuery</a> to look in our container since that was where we placed it).</p>
<p>Finally, <strong>append</strong> the html that was rendered to the container (DIV) to see the result on the screen.<br />
Problems? No, it works.<br />
Can we make it simpler and more readable? I hope so.</p>
<p>It would be great if we could find containers and templates be just saying something like this:</p>
<p><strong>$(:container(books))</strong> and <strong>$(:template(books))</strong></p>
<p>Thus changing the javascript code to look like this instead:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image7.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb7.png?w=535&#038;h=145" alt="image" width="535" height="145" border="0" /></a></p>
<p>To make that work we have a couple of choices. We can make an assumption that the ID tag of a container and/or template will always have an ID <strong>prefix</strong> of container-{ID} and/or template-{ID} just like we had in the example.<br />
Another solution is to use the data attribute that <a href="http://jquery.com/" target="_blank">jQuery</a> is good at working with. I’ll use that method in my example.</p>
<h3>Using the data attribute</h3>
<p>By using the <strong>data-</strong> (dash) syntax to identify my containers and templates I now have altered the html to look like this:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image15.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image15_thumb.png?w=600&#038;h=255" alt="image" width="600" height="255" border="0" /></a></p>
<p>…and now here is the code for the actual selectors:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:1c436f66-1c28-4a73-98a8-67d7b690c99f" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: jscript; pad-line-numbers: true;">
(function ($, undefined) {

    if ($ === undefined) {
        throw &quot;Dependency: JQuery is not defined. Please check javascript imports.&quot;;
    }

    $.extend($.expr[':'],
    {

        // :template(name)
        template: function (current, index, metadata, elements) {

            var arg = metadata[3],
                d = $(current).data(&quot;template-for&quot;);

            if (d === undefined) {
                return false;
            }

            return arg ? arg === d : true;

        },

        // :container(name)
        container: function (current, index, metadata, elements) {

            var arg = metadata[3],
                d = $(current).data(&quot;container-for&quot;);

            if (d === undefined) {
                return false;
            }

            return arg ? arg === d : true;

        }

    });

} (jQuery));
</pre></p>
</div>
<p>So the selector/s first check that we have found an element with the <strong>data-container-for</strong> and/or <strong>data-template-for</strong> attribute and then optionally compares that to the <strong>name</strong> of the container and/or template.</p>
<h2>Summing up</h2>
<p>So by including this javascript we can now find all “container” elements (yeah, I’ll call them that) and make some changes to them (like putting everything in bold text).</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image8.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb8.png?w=316&#038;h=29" alt="image" width="316" height="29" border="0" /></a></p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image9.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb9.png?w=230&#038;h=315" alt="image" width="230" height="315" border="0" /></a></p>
<p>yeah…that’s pretty!!</p>
<p>More interesting though, is that the <strong><em>OnSuccess</em></strong> javascript method now looks like this instead:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/08/image10.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="image" src="http://johanleino.files.wordpress.com/2011/08/image_thumb10.png?w=487&#038;h=134" alt="image" width="487" height="134" border="0" /></a></p>
<p>Of course, you can optimize and change this a lot more but the basic idea was to show how easy it is to write these custom selectors.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1111&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2011/08/04/jquery-implementing-custom-selectors-for-jquery-templates-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image51_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image15_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/08/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Introducing the jQuery CRUD extension</title>
		<link>http://johanleino.wordpress.com/2011/06/22/introducing-the-jquery-crud-extension/</link>
		<comments>http://johanleino.wordpress.com/2011/06/22/introducing-the-jquery-crud-extension/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 20:37:00 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/2011/06/21/introducing-the-jquery-rest-extension-plugin/</guid>
		<description><![CDATA[I’ve been getting my hands dirty with some jQuery/JavaScript, WCF, JSON, and REST lately doing a backend domain layer exposed via WCF as RESTful service operations that communicate via JSON. On the client I’ve used jQuery as means for communicating with the WCF endpoints to get data and create/update/delete data…aka CRUD. What I’ve come to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1053&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I’ve been getting my hands dirty with some <a href="http://jquery.com/" target="_blank">jQuery</a>/JavaScript, <a href="http://msdn.microsoft.com/en-us/library/ms731082.aspx" target="_blank">WCF</a>, <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a>, and <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">REST</a> lately doing a backend domain layer exposed via <a href="http://msdn.microsoft.com/en-us/library/ms731082.aspx" target="_blank">WCF</a> as RESTful service operations that communicate via <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a>.</p>
<p>On the client I’ve used <a href="http://jquery.com/" target="_blank">jQuery</a> as means for communicating with the <a href="http://msdn.microsoft.com/en-us/library/ms731082.aspx" target="_blank">WCF</a> endpoints to get data and create/update/delete data…aka <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a>.</p>
<p>What I’ve come to learn is that it’s sometimes very cumbersome and time-consuming to create these $.ajax(…{}) calls via <a href="http://jquery.com/" target="_blank">jQuery</a>, specifying a bunch of settings every time, so I decided to create a plugin/extension that would make things easier for me when I communicated with these RESTful operations.</p>
<p>For the purpose of demonstrating this extension I’m going to use a small scenario and throughout it explain what the extension does. The entire code for it is at the end of this post.</p>
<p><span style="color:#ff0000;"><strong>Update:</strong></span> I&#8217;ve also made a NuGet package of the extension so you can also get it from VS 2010</p>
<h2>The services</h2>
<p>For this purpose of this post I’ve created a tiny domain consisting of <strong>books</strong> and <strong>authors</strong>. Consequently I have two endpoints, <strong>books</strong> and <strong>authors</strong> with these available operations:</p>
<h3>Book Service Operations</h3>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image13.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb13.png?w=618&#038;h=147" alt="image" width="618" height="147" border="0" /></a></p>
<ul>
<li><strong>Read Operations (3):</strong> all books <strong>OR</strong> all books within a genre <strong>OR </strong>one book with a specified id</li>
<li><strong>Create Operations (1):</strong>create a new book</li>
<li><strong>Update Operations (1):</strong>update a book with a specified id</li>
<li><strong>Delete Operations (1):</strong> delete a book with a specified id</li>
</ul>
<h3>Author Service Operations</h3>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image14.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb14.png?w=615&#038;h=100" alt="image" width="615" height="100" border="0" /></a></p>
<ul>
<li><strong>Read Operations (2):</strong> all authors <strong>OR</strong> all books by one author with a specified id</li>
</ul>
<p>These endpoints are exposed via <a href="http://msdn.microsoft.com/en-us/library/ms731082.aspx" target="_blank">WCF</a> via routes but since this post’s topic isn’t about <a href="http://msdn.microsoft.com/en-us/library/ms731082.aspx" target="_blank">WCF</a> we’ll leave the services now knowing that they respond via <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> and subsequently expect <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> input when using PUT or POST.</p>
<h2>The client</h2>
<p>As I previously stated when using <a href="http://jquery.com/" target="_blank">jQuery</a> for communicating with a service that exposes RESTful endpoints it’s sometimes a bit cumbersome to work with <a href="http://jquery.com/" target="_blank">jQuery</a>’s $.ajax function. On the contrary, the $.getJSON is a bit to simple for me sometimes so what I did was to look around the web for what was available when working with <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">REST</a> and <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> with <a href="http://jquery.com/" target="_blank">jQuery</a>.<br />
I found <a href="http://plugins.jquery.com/project/Rest" target="_blank">this</a> plugin by <strong>Nathan Bubna</strong> but somehow I didn’t get it to work and there wasn’t any samples included. But I took Nathan’s excellent code and decided to change some things and add some other things.</p>
<h3>The jQuery extension</h3>
<p><strong><span style="text-decoration:underline;">Disclaimer:</span></strong> I’ve created this extension for the purpose of using it myself, hence it works like I want it to. I may not suit all needs for every application. But, feel free to change the code anyway you like it…</p>
<ul>
<li>$.Create(uri, data, callback, options) – <strong>POST</strong></li>
<li>$.Read(uri, data, callback, options) &#8211; <strong>GET</strong></li>
<li>$.Update(uri, data, callback, options) – <strong>PUT</strong></li>
<li>$.Delete(uri, data, callback, options) – <strong>DELETE</strong></li>
</ul>
<p>Those are the methods that this extension/plugin adds to <a href="http://jquery.com/" target="_blank">jQuery</a>. They are added to <a href="http://jquery.com/" target="_blank">jQuery</a>/$ itself and therefore will not work together with the $() selectors. Again…that’s what I wanted, easy enough to change if you’d like that.</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image15.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb15.png?w=586&#038;h=337" alt="image" width="586" height="337" border="0" /></a></p>
<p>Each of the methods take at most four parameters; <strong>URI</strong>, <strong>DATA</strong>, <strong>CALLBACK</strong>, and <strong>OPTIONS</strong></p>
<ul>
<li><strong>URI: </strong>the uri/url to the RESTful operation. e.g. “books”. Can also be used with placeholders such as<br />
“books/{id}”. This parameter is <strong>required<br />
</strong></li>
<li><strong>DATA: optional</strong> data to send. If data contains an object like {id:1} then the id placeholder in the uri will be replaced (i.e. <strong>“books/1”). </strong>If it’s a create or update call (i.e. POST/PUT) the <strong>data:</strong> {JSON obj}within object will be <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a> serialized ( e.g. <strong>{data: {Id:1, Title: “The title”}} </strong>), after the replacement has taken place (which removes all the replacement parameters from the data parameter), and sent as <a href="http://en.wikipedia.org/wiki/JSON" target="_blank">JSON</a>to the service endpoint.The data parameter can also be replaced by the callback function (if there is no data to send), e.g.<br />
<strong><em>$.Read(“books”, OnSuccess)</em></strong>.</li>
<li><strong>CALLBACK: optional</strong> callback function to execute with the response (i.e. the success function of <a href="http://jquery.com/" target="_blank">jQuery</a>’s $.ajax method).The callback parameter can also be replaced by the options object, e.g.<br />
<strong><em>$.Read(“books”, OnSuccess, {cache:true, error: OnError}).<br />
</em></strong></li>
<li><strong>OPTIONS:</strong> <strong>optional</strong> options object that corresponds to <a href="http://jquery.com/" target="_blank">jQuery</a>’s $.ajax settings object. Use this to change any default settings or add something that wasn’t added by the extension.</li>
</ul>
<p>Each of the methods call an internal function called <strong>execute </strong>and passes in the result from another internal function called <strong>prepare</strong>.<br />
<strong>Prepare</strong>, in turn, is called via <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply" target="_blank"><strong>apply</strong></a><strong> </strong>which is just a fancy way of calling a function when you don’t know the exact number of parameters you’ll pass…and yes, the first parameter is the <strong>this</strong> pointer (which we’ll come back to in a bit).</p>
<p>Let’s have a look at <strong>execute</strong> and <strong>prepare</strong> next:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image16.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb16.png?w=401&#038;h=120" alt="image" width="401" height="120" border="0" /></a></p>
<p><strong>Execute</strong> doesn’t need that much explaining, it simple calls the underling $.ajax method with the options that <strong>prepare</strong> will do for us. So <strong>prepare</strong> then:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image17.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb17.png?w=600&#038;h=70" alt="image" width="600" height="70" border="0" /></a></p>
<p>I’m just showing parts of the prepare function that I find interesting to highlight. For instance, the first thing that happens is that I merge the default settings with the settings for the current operation. These look like:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image18.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb18.png?w=478&#038;h=503" alt="image" width="478" height="503" border="0" /></a></p>
<p>Here’s where the <strong>THIS</strong> pointer comes into play. Since I call <strong>operations[this]</strong>, <strong>this</strong> will be one of the values of <strong>$create</strong>, <strong>$read</strong>, <strong>$update</strong>, or <strong>$delete</strong> depending on which method was called. So what I’ll get is a combination of the <strong>default settings</strong> combined with the <strong>settings for the current operation</strong>. All of which can be altered by using the options parameter.</p>
<p>Prepare will then go on and parse, merge, replace etc the parameters that was sent to it…have a look in the code if you’re interested.</p>
<p>So, now that you know how the extension works…let’s take it out for a spin.</p>
<p><strong><span style="text-decoration:underline;">Note:</span></strong> the OnSuccess method used in the following examples looks like this</p>
<p><strong><em>function OnSuccess(response) {<br />
// do stuff with response which is a </em></strong><a href="http://en.wikipedia.org/wiki/JSON" target="_blank"><strong><em>JSON</em></strong></a><strong><em> object<br />
}</em></strong></p>
<h3>READ: all books</h3>
<p><strong><em>$.Read(&#8220;books&#8221;, OnSuccess);</em></strong></p>
<p>returns:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image19.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb19.png?w=535&#038;h=181" alt="image" width="535" height="181" border="0" /></a></p>
<h3>READ: book by id</h3>
<p><strong><em>$.Read(&#8220;books/{id}&#8221;, { id: &#8220;bk101&#8243; }, OnSuccess);</em></strong></p>
<p>returns:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image20.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb20.png?w=506&#038;h=34" alt="image" width="506" height="34" border="0" /></a></p>
<h3>READ: books in a specific genre</h3>
<p><strong><em>$.Read(&#8220;books?genre={genre}&#8221;, { genre:&#8221;Romance&#8221; }, OnSuccess);</em></strong></p>
<p>returns:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image21.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb21.png?w=517&#038;h=41" alt="image" width="517" height="41" border="0" /></a></p>
<h3>CREATE: book</h3>
<p><strong><em>var book = {<br />
Id: &#8220;bk113&#8243;,<br />
Title: &#8220;The new book&#8221;,<br />
Genre: &#8220;NewAge&#8221;,<br />
Description: &#8220;This is the newest book on the shelf&#8221;<br />
};<br />
$.Create(&#8220;books&#8221;, book, OnSuccess);</em></strong></p>
<p>returns:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image22.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb22.png?w=517&#038;h=67" alt="image" width="517" height="67" border="0" /></a></p>
<h3>UPDATE: book</h3>
<p><strong><em>var book = {<br />
Id: &#8220;bk113&#8243;,<br />
Title: &#8220;The new book&#8221;,<br />
Genre: &#8220;Romance&#8221;,<br />
Description: &#8220;This is the newest book on the shelf&#8221;<br />
};<br />
$.Update(&#8220;books/{id}&#8221;, {id: &#8220;bk113&#8243;, data: book}, OnSuccess);</em></strong></p>
<p>returns:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image23.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb23.png?w=524&#038;h=68" alt="image" width="524" height="68" border="0" /></a></p>
<h3>DELETE: book</h3>
<p><strong><em>$.Delete(&#8220;books/{id}&#8221;, { id: &#8220;bk113&#8243; });</em></strong></p>
<h3>READ: books by an author</h3>
<p><strong><em>$.Read(&#8220;authors/{author}/books&#8221;, { author: &#8220;Corets, Eva&#8221; }, OnSuccess);</em></strong></p>
<p>returns:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image24.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb24.png?w=536&#038;h=96" alt="image" width="536" height="96" border="0" /></a></p>
<h2></h2>
<h2>The final code</h2>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:d384b6eb-a63d-42b1-8212-d4ad340e451d" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: jscript;">
(function ($, undefined) {

    if (!$) {
        throw &quot;Dependency: jQuery is not defined. Check javascript file imports.&quot;;
    }

    var defaults = {
        contentType: &quot;application/json; charset=utf-8&quot;,
        dataType: &quot;json&quot;,
        cache: false,
        data: null
    },
    // CRUD
        operations = {
            $create: {
                type: &quot;POST&quot;,
                processData: true
            },
            $read: {
                type: &quot;GET&quot;,
                processData: false
            },
            $update: {
                type: &quot;PUT&quot;,
                processData: true
            },
            $delete: {
                type: &quot;DELETE&quot;,
                processData: false
            }
        };

    function prepareUri(url, data) {

        // replaces tokens with their actual values and removes the replaced values from the data array
        // e.g. url: &quot;service/{route}?tag={tag}&quot;, data: {route:&quot;bookmarks&quot;, tag:&quot;public&quot;} =&gt; service/bookmarks?tag=public
        var k,
            v,
            u;

        for (k in data) {
            v = data[k];
            u = url.replace('{' + k + '}', v);

            if (u !== url) {
                url = u;
                delete data[k];
            }
        }

        return url;

    }

    function prepareData() {

        // handle JSON serialization from Object
        if (this.data &amp;&amp; typeof this.data !== &quot;string&quot;) {

            if ($.isEmptyObject(this.data)) {
                this.data = null;
            } else {
                this.data = JSON.stringify(this.data);
            }
        }
    }

    function prepare(uri, data, callback, options) {

        if (typeof uri !== &quot;string&quot; || !uri) {
            throw &quot;Parameter: 'uri' must be of type string&quot;;
        }

        if (options &amp;&amp; $.isPlainObject(options) === false) {
            throw &quot;Parameter: 'options' must be of type object&quot;;
        }

        options = options || {};

        // merge default settings and the current operation (crud operation) settings
        var o = $.extend({}, defaults, operations[this]);

        // determine if data is a function or an object
        // 1. function - $.Read(&quot;uri&quot;, OnSuccess)
        // 2. object - $.Read(&quot;uri&quot;, {} )
        if ($.isFunction(data)) {

            // determine if the callback is an object
            // 1. object - $.Read(&quot;uri&quot;, OnSuccess, {} )
            if ($.isPlainObject(callback)) {
                options = callback;
            }

            callback = data;
            data = null;
        } else {

            uri = prepareUri(uri, data);
            options.data = data.data || data;

            // determine if callback is a function or an object
            // 1. object - $.Read(&quot;uri&quot;, {}, {} )
            // 2. function - $.Read(&quot;uri&quot;, {}, OnSuccess )
            if ($.isPlainObject(callback)) {
                options = callback;
                callback = null;
            }
        }

        o.url = uri;

        if (callback) {
            o.success = callback;
        }
        if (options) {
            prepareData.apply(options);
            $.extend(o, options);
        }

        return o;

    }

    function execute(options) {
        return $.ajax(options);
    }

    // extend jQuery
    $.extend({

        Create: function (uri, data, callback, options) {
            ///
&lt;summary&gt;Create operation (uses POST by default)&lt;/summary&gt;
            ///[String] the uri template (required)
            ///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
            ///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
            ///[Object] the $.ajax settings (optional)
            return execute(prepare.apply(&quot;$create&quot;, arguments));
        },
        Read: function (uri, data, callback, options) {
            ///
&lt;summary&gt;Read operation (uses GET by default)&lt;/summary&gt;
            ///[String] the uri template (required)
            ///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
            ///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
            ///[Object] the $.ajax settings (optional)
            return execute(prepare.apply(&quot;$read&quot;, arguments));
        },
        Update: function (uri, data, callback, options) {
            ///
&lt;summary&gt;Update operation (uses PUT by default)&lt;/summary&gt;
            ///[String] the uri template (required)
            ///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
            ///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
            ///[Object] the $.ajax settings (optional)
            return execute(prepare.apply(&quot;$update&quot;, arguments));
        },
        Delete: function (uri, data, callback, options) {
            ///
&lt;summary&gt;Delete operation (uses DELETE by default)&lt;/summary&gt;
            ///[String] the uri template (required)
            ///[Object|Function] token replacements and/or JSON object OR the $.ajax success callback (optional)
            ///[Object|Function] the $.ajax success callback OR the $.ajax settings (optional)
            ///[Object] the $.ajax settings (optional)
            return execute(prepare.apply(&quot;$delete&quot;, arguments));
        }
    });

} (jQuery));
</pre></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1053/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1053/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1053&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2011/06/22/introducing-the-jquery-crud-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb13.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb14.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb15.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb16.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb17.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb18.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb19.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb20.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb21.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb22.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb23.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb24.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Notifications not showing when displayed in dialogs</title>
		<link>http://johanleino.wordpress.com/2011/05/31/notifications-not-showing-when-displayed-in-dialogs/</link>
		<comments>http://johanleino.wordpress.com/2011/05/31/notifications-not-showing-when-displayed-in-dialogs/#comments</comments>
		<pubDate>Tue, 31 May 2011 17:46:00 +0000</pubDate>
		<dc:creator>Johan Leino</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Client Object Model]]></category>
		<category><![CDATA[Dialog Framework]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">https://johanleino.wordpress.com/?p=1050</guid>
		<description><![CDATA[I love working with the new dialog framework and notifications in SharePoint 2010. The other day though I ran into an issue with the notifications that somehow wouldn’t show up when I tried to use them in an application page I’ve built that was displayed through the dialog framework. In my application page I wanted [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1050&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I love working with the new dialog framework and notifications in SharePoint 2010. The other day though I ran into an issue with the notifications that somehow wouldn’t show up when I tried to use them in an application page I’ve built that was displayed through the dialog framework.
<p>In my application page I wanted to use display a notification to the user. My code looked something like this:&nbsp;&nbsp;
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:ab580c41-4ee1-44ab-b00d-3da64274eaee" class="wlWriterEditableSmartContent">
<pre style="white-space:normal;">
<pre class="brush: jscript;">
        var notifier = {
            _nid: null,
            ShowMessage: function (msg) {
                notifier._nid = SP.UI.Notify.addNotification(&quot;This is a message...&quot;, true);
            },
            HideMessage: function () {
                SP.UI.Notify.removeNotification(notifier._nid);
                notifier._nid = null;
            }

        }
</pre>
</pre>
</div>
<p>And for the purpose of this post let’s use two buttons, one to show the notification and one to hide it: </p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:43ddc40c-e88e-448b-88c2-2a17434218ed" class="wlWriterEditableSmartContent">
<pre style="white-space:normal;">
<pre class="brush: xml;">
    &lt;input type=&quot;button&quot; 
        value=&quot;Show Notification&quot; 
        onclick=&quot;notifier.ShowMessage();&quot; 
        style=&quot;width:150px&quot; /&gt;
    &lt;input type=&quot;button&quot; 
        value=&quot;Remove Notification&quot; 
        onclick=&quot;notifier.HideMessage();&quot; 
        style=&quot;width:150px&quot; /&gt;
</pre>
</pre>
</div>
<p>Opening the page in the browser (not through a dialog) and clicking the “show” button displays this:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb.png?w=373&#038;h=196" width="373" height="196"></a> </p>
<p>…as expected.</p>
<p>&nbsp;<br />
<h2>But what happens if I open this application page in a dialog? </h2>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb1.png?w=455&#038;h=91" width="455" height="91"></a> </p>
<p>To “simulate” that behavior I added the <strong><em>IsDlg=1</em></strong> query string parameter which is what the dialog framework will do for us if we where to open this page in a dialog.&nbsp;
<p>The first thing to notice is that the ribbon disappears completely from the page and secondly when I click the button again nothing happens.</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image2.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb2.png?w=687&#038;h=66" width="687" height="66"></a></p>
<h3>&nbsp;</h3>
<h3>So why is this happening? </h3>
<p>I searched the internet to find an answer for this and yes I did find a solution for the problem (which is at the end of this post) but I always like to know <strong>why</strong> this happens and not only <strong>how</strong> to solve it. So my investigation starts… </p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image3.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb3.png?w=744&#038;h=366" width="744" height="366"></a> </p>
<p>If I use firebug to inspect the html I can see that the ribbon (the div in the screenshot that is highlighted) is in fact rendered on the page but a style rule is hiding it. That style rule resides in some style sheet called <strong>dlgframe.css</strong> which I’ve tried to illustrate above.</p>
<p>Using firebug I tried to remove that line (<strong>display:none</strong>) and the ribbon row appeared again and the notification was displayed like before. You can see it from the image below that the page now has that dark blue ribbon row. </p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image4.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb4.png?w=492&#038;h=189" width="492" height="189"></a>&nbsp;</p>
<p>&nbsp;</p>
<h3>How do the notifications work? </h3>
<p>In order to fully understand the problem I now looked at the JavaScript used to display the notifications.&nbsp;
<p><a href="http://johanleino.files.wordpress.com/2011/06/image5.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb5.png?w=725&#038;h=230" width="725" height="230"></a></p>
<p>Deep inside the core.js file I found that the notifications are finally shown using an element with id <strong>notificationArea</strong>. <br />AHA!!<br />That element resides inside the ribbon (which you an see from the screenshot above). So if that element isn’t visible the notifications simply will not be shown to the user.</p>
<p>&nbsp;<br />
<h3>The dlgframe.css style sheet </h3>
<p>Going forward in my investigation I found that the dlgframe.css style sheet has been added to the <strong>head</strong> tag of the page (hmmm who did that?)</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image6.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb6.png?w=589&#038;h=162" width="589" height="162"></a> </p>
<p>If we have a look inside that style sheet one of the rules read:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image7.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb7.png?w=550&#038;h=94" width="550" height="94"></a></p>
<p>Or in plain English if you don´t speak fluent CSS:<br /><em><font size="3">”set display equals none to the element with id <strong>s4-ribbonrow</strong> that is a child of a <strong>body</strong> tag and if that body tag is also a child of an element with a class name that reads <strong>ms-dialog-nr</strong>”</font></em>&nbsp;
<p>OK, so we know that <strong>s4-ribbonrow</strong> is that div that is the starting point of the Ribbon from what we saw earlier. That div is a child of the body tag (duh?). <br />Furthermore, if we inspect the html that is outputted when <strong>IsDlg=1</strong> is added to the query strings, the html tag apparently has a class named <strong>ms-dialog-nr</strong>…hey that is what that style rule says!!</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image8.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb8.png?w=648&#038;h=54" width="648" height="54"></a></p>
<p>That is exactly what we saw earlier in firebug and that is the rule that hides the ribbon and ultimately what causes the problem with the notification to being hidden. OK, so now that we know <strong>why</strong> the notification won´t display we’ll now continue with the <strong>how </strong>is that possible?</p>
<h3>Which control outputs these style rules then?</h3>
<p>It turns out that the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.csslink.aspx" target="_blank">CssLink</a> control is the culprit. From the <strong>OnLoad</strong> method a call is made to the <strong>SeupDialogCSS</strong> method that looks like this:</p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image9.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb9.png?w=715&#038;h=131" width="715" height="131"></a> </p>
<p>Looking at the code with <a href="http://wiki.sharpdevelop.net/ilspy.ashx" target="_blank">ILSpy</a> you can see that if the current <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.aspx" target="_blank">SPContext</a> is a dialog (e.g. <strong>IsDlg=1</strong>) the <strong>dlgframe.css</strong> is registered (among some other thing that I have omitted for now).&nbsp;
<p>The <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.csslink.aspx" target="_blank">CssLink</a> control is added to the html via the <strong>applicationv4.master</strong> that the application page uses (if we haven´t overridden that with a custom one…chances that it uses the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.csslink.aspx" target="_blank">CssLink</a> are still there though).&nbsp;&nbsp;
<p>&nbsp;<br />
<h2>So what options does this present us with? </h2>
<p>One option is to use <a href="http://jquery.com/" target="_blank">JQuery</a> to forcefully show the ribbon row, something like: </p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image10.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb10.png?w=326&#038;h=54" width="326" height="54"></a></p>
<p> Somehow, that doesn’t seem quite right though.</p>
<p>&nbsp;<br />
<h3>Another option</h3>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image11.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb11.png?w=542&#038;h=167" width="542" height="167"></a></p>
<p>That is a screenshot of some more code from the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.csslink.aspx" target="_blank">CssLink</a> class that I omitted before. <br />From it we can see that the code uses the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spribbon.aspx" target="_blank">SPRibbon</a> to determine if the command UI is <strong>not</strong> set to show (by default) and if so the <strong>ms-dialog-nr</strong> css class is added to some <strong>HtmlGenericControl</strong> which is in fact the <strong>html</strong> tag (that can bee seen from looking at the whole code).</p>
<p>This presents us with an option though.<br /> Change that behavior in the code behind used by our application page, thus forcing the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.csslink.aspx" target="_blank">CssLink</a> not adding that css class.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:07cc9368-7806-48d9-8b01-87a3f36034e7" class="wlWriterEditableSmartContent">
<pre style="white-space:normal;">
<pre class="brush: csharp; pad-line-numbers: true;">
        protected override void OnPreRender(EventArgs e)
        {
            if (SPContext.Current != null &amp;&amp; SPContext.Current.IsPopUI)
            {
                var ribbon = SPRibbon.GetCurrent(this.Page);
                if (ribbon != null)
                {
                    ribbon.CommandUIVisible = true;
                }
            }
            base.OnPreRender(e);
        }
</pre>
</pre>
</div>
<p>Build, Deploy and…</p>
<p>&nbsp;
<p><strong></strong></p>
<p><a href="http://johanleino.files.wordpress.com/2011/06/image12.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://johanleino.files.wordpress.com/2011/06/image_thumb12.png?w=349&#038;h=264" width="349" height="264"></a></p>
<p>&nbsp;</p>
<p>…it’s showing!!!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/johanleino.wordpress.com/1050/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/johanleino.wordpress.com/1050/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/johanleino.wordpress.com/1050/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/johanleino.wordpress.com/1050/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/johanleino.wordpress.com/1050/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/johanleino.wordpress.com/1050/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/johanleino.wordpress.com/1050/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/johanleino.wordpress.com/1050/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=johanleino.wordpress.com&amp;blog=8899881&amp;post=1050&amp;subd=johanleino&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://johanleino.wordpress.com/2011/05/31/notifications-not-showing-when-displayed-in-dialogs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b98ad62b2f0fc1fd135ba53f56f9ffd3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Johan Leino</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb11.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://johanleino.files.wordpress.com/2011/06/image_thumb12.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
	</channel>
</rss>
