<?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:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>SharePoint Magazine &#187; reciever</title>
	<atom:link href="http://sharepointmagazine.net/tag/reciever/feed" rel="self" type="application/rss+xml" />
	<link>http://sharepointmagazine.net</link>
	<description>SharePoint Magazine is an online Magazine dedicated to the world of SharePoint</description>
	<lastBuildDate>Mon, 05 Jul 2010 09:14:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Introduction to SharePoint Feature Stapling &#8211; Part 2</title>
		<link>http://sharepointmagazine.net/technical/development/introduction-to-sharepoint-feature-stapling-part-2</link>
		<comments>http://sharepointmagazine.net/technical/development/introduction-to-sharepoint-feature-stapling-part-2#comments</comments>
		<pubDate>Wed, 05 Nov 2008 08:34:53 +0000</pubDate>
		<dc:creator>Paul Liebrand</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[FeatureActivated]]></category>
		<category><![CDATA[features]]></category>
		<category><![CDATA[ListTemplates]]></category>
		<category><![CDATA[reciever]]></category>
		<category><![CDATA[sharepoint]]></category>
		<category><![CDATA[SPWeb]]></category>
		<category><![CDATA[stapling]]></category>

		<guid isPermaLink="false">http://sharepointmagazine.net/?p=1242</guid>
		<description><![CDATA[Welcome to part 2 of my Introduction to SharePoint Feature Stapling. In this small post, I’ll be providing some insight into the feature stapling lifecycle and also providing some potential solutions to some of the downsides of the feature stapling process.]]></description>
			<content:encoded><![CDATA[<p>Welcome to part 2 of my Introduction to SharePoint Feature Stapling. In <a href="http://sharepointmagazine.net/technical/development/introduction-to-sharepoint-feature-stapling-part-1">part 1</a> I explained the process of creating a feature stapler and associating it with a site definition. In this small post, I’ll be providing some insight into the feature stapling lifecycle and also providing some potential solutions to some of the downsides of the feature stapling process.</p>
<p><strong>Feature Stapling Lifecycle</strong></p>
<p>The actual lifecycle of the feature stapling process is not documented actually documented anywhere. The following information has been solely gathered based on my testing and experimenting with the process.</p>
<p>If we step back and look at a site definition file we know that the GLOBAL#0 site template is applied to all site definitions. Many of the Microsoft provided site definitions enable functionality through the use of the feature system. If you open up the STS site definition ONET.XML file, you will see two sections near the bottom called <strong>SiteFeatures</strong> and <strong>WebFeatures</strong>:</p>
<p><img src="http://sharepointmagazine.net/wp-content/uploads/2008/09/stsonet.png" border="0" alt="STS ONET" width="556" height="189" /></p>
<p>You will notice that in many of the Windows SharePoint Services site definition files the <strong>ListTemplates</strong> section is not populated with anything; they use list definition features instead. As you can see, the <strong>Team Site</strong> site definition points to a feature called <strong>TeamCollab</strong>. This feature makes list definitions available to the web that is being created.</p>
<p>So what does this all mean when it comes to feature association? The important thing to remember here is that any feature receivers you develop and associate to a site definition will get executed <strong>before</strong> any of the lists on that site have been provisioned. However, anything provisioned via the Global Site Definition will be available to your feature receiver.</p>
<p>This is not a problem if you simply want to create new lists, libraries, etc. However, if you are attempting to manipulate any lists that are part of the site definition you are associating with you will find yourself looking at a <strong>System.ArgumentException</strong> stating “Value does not fall within the expected ranged.”</p>
<p>The following image shows what happens when I create a Team Site that has my custom feature receiver associated to it:</p>
<p><img src="http://sharepointmagazine.net/wp-content/uploads/2008/09/exceptionmessage.png" border="0" alt="ExceptionMessage" width="604" height="371" /></p>
<p><strong>Solution</strong></p>
<p>We have already determined that feature receivers make it extremely easy for a developer to extend and customize out of the box site definitions without touching the files. But we run into a road block when we attempt to customize the lists and libraries that are part of that site definition.</p>
<p>One way I have used to get around this is to simply delay the customizations until after the site has completely provisioned.</p>
<p>The following is an example using the ThreadPool.QueueUserWorkItem method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> FeatureActivated<span style="color: #000000;">&#40;</span>SPFeatureReceiverProperties properties<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SPWeb web <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>SPWeb<span style="color: #000000;">&#41;</span>properties.<span style="color: #0000FF;">Feature</span>.<span style="color: #0000FF;">Parent</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        ThreadPool.<span style="color: #0000FF;">QueueUserWorkItem</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WaitCallback<span style="color: #000000;">&#40;</span>RunProcess<span style="color: #000000;">&#41;</span>, web.<span style="color: #0000FF;">Url</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span> 
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> RunProcess<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> state<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
    <span style="color: #FF0000;">string</span> url <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>state<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SPSite site <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SPWeb web <span style="color: #008000;">=</span> site.<span style="color: #0000FF;">OpenWeb</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// at this point, the site should be provisioned</span>
            <span style="color: #008080; font-style: italic;">// and we will now have access to all lists on it</span>
            SPList list <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Shared Documents&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            list.<span style="color: #0000FF;">Description</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Updated from Feature Receiver&quot;</span><span style="color: #008000;">;</span>
            list.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Why are we running on a different thread? Well, this allows the site provisioning process to complete and then we come back in a few seconds later to customize it as needed. Of course, the sample above is not 100% perfect. What happens if it takes 10 seconds to provision a site – well the method above would fail out.</p>
<p>A more elegant way would probably be to check the SPWeb.Provisioned property which is <strong>False</strong> until after the provisioning process has completed; but you get the general idea.</p>
<p><strong>Summary</strong></p>
<p>Hopefully this two part series has given you the necessary information needed to get started using SharePoint Feature Stapling and what potential issues to watch out for. There is many resources out on the Internet that cover other aspects of Feature Stapling, but I’ll list a few here for your reference:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/bb861862.aspx">MSDN: Feature Stapling</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/bb862634.aspx">MSDN: Feature Event Receiver</a></p>
<p><a href="http://blogs.msdn.com/cjohnson/archive/2006/11/01/feature-stapling-in-wss-v3.aspx">http://blogs.msdn.com/cjohnson/archive/2006/11/01/feature-stapling-in-wss-v3.aspx</a></p>
<p><a href="http://www.sharepointnutsandbolts.com/2007/05/feature-stapling.html">http://www.sharepointnutsandbolts.com/2007/05/feature-stapling.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sharepointmagazine.net/technical/development/introduction-to-sharepoint-feature-stapling-part-2/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
