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

<channel>
	<title>Florent Clairambault &#187; Sharepoint</title>
	<atom:link href="http://florent.clairambault.fr/tag/sharepoint/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Sat, 04 Feb 2012 19:56:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
		<item>
		<title>SPSecurity.RunWithElevatedPrivileges</title>
		<link>http://florent.clairambault.fr/spsecurity-runwithelevatedprivileges?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spsecurity-runwithelevatedprivileges</link>
		<comments>http://florent.clairambault.fr/spsecurity-runwithelevatedprivileges#comments</comments>
		<pubDate>Mon, 22 Feb 2010 20:00:56 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[SPSite]]></category>
		<category><![CDATA[SPWeb]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2303</guid>
		<description><![CDATA[If like me you&#8217;re wondering what exactly does SPSecurity.RunWithElevatedPrivileges, this is what I understand of it : 1 2 3 4 5 SPSecurity.RunWithElevatedPrivileges&#40; &#40;&#41; =&#62; &#123; using&#40; var newSite = new SPSite&#40; site.Url &#41; &#41; &#123; // your stuff &#125; &#125;&#41;; Is equivalent to : 1 2 3 using&#40; var newSite = new SPSite&#40; site.Url, [...]]]></description>
			<content:encoded><![CDATA[<p>If like me you&#8217;re wondering what exactly does SPSecurity.RunWithElevatedPrivileges, this is what I understand of it :</p>

<div class="wp_codebox"><table><tr id="p23033"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2303code3"><pre class="csharp" style="font-family:monospace;">SPSecurity<span style="color: #008000;">.</span><span style="color: #0000FF;">RunWithElevatedPrivileges</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #008000;">&#40;</span> var newSite <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPSite<span style="color: #008000;">&#40;</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// your stuff</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Is equivalent to :</p>

<div class="wp_codebox"><table><tr id="p23034"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2303code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #008000;">&#40;</span> var newSite <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPSite<span style="color: #008000;">&#40;</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span>, site<span style="color: #008000;">.</span><span style="color: #0000FF;">SystemAccount</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Token</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// your stuff</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So if this is just to open an elevated privilege SPSite, it will be equivalent. But if you do some more complex things, as soon as you load a new SPSite it will open it with the original UserToken. And you lose&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/spsecurity-runwithelevatedprivileges/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Threads and Sharepoint</title>
		<link>http://florent.clairambault.fr/threads-and-sharepoint?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=threads-and-sharepoint</link>
		<comments>http://florent.clairambault.fr/threads-and-sharepoint#comments</comments>
		<pubDate>Sat, 20 Feb 2010 08:00:31 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[Sharepoint]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2321</guid>
		<description><![CDATA[When you are using threads on sharepoint, you should take care of some little things : The method used to launch the thread must have the &#8220;[STAThread]&#8221; attribute, it avoids some huge memory leaks. If you need to use an SPSite from the calling site, you should copy its URL and UserToken to build a [...]]]></description>
			<content:encoded><![CDATA[<p>When you are using threads on sharepoint, you should take care of some little things :</p>
<ul>
<li>The method used to launch the thread must have the &#8220;[STAThread]&#8221; attribute, it avoids some huge memory leaks.</li>
<li>If you need to use an SPSite from the calling site, you should copy its URL and UserToken to build a new SPSite in the new thread. If you&#8217;re not using that &#8220;design pattern&#8221;, things will get really wrong.</li>
<li>And of course you should dispose any sharepoint SPSite (SPWebs come from SPSite) allocated within the thread.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/threads-and-sharepoint/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ANTS Memory Profiler</title>
		<link>http://florent.clairambault.fr/ants-memory-profiler?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ants-memory-profiler</link>
		<comments>http://florent.clairambault.fr/ants-memory-profiler#comments</comments>
		<pubDate>Wed, 30 Dec 2009 18:00:16 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.NET Reflector]]></category>
		<category><![CDATA[ANTS Memory Profiler]]></category>
		<category><![CDATA[Memory Profiler]]></category>
		<category><![CDATA[Sharepoint]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2263</guid>
		<description><![CDATA[Well, I&#8217;m speaking about a commercial product again. This time it&#8217;s about ANTS Memory Profiler (by Red Gate software, the ones that made .Net reflector). This product will help you identify any .Net memory leak you could have. The truth is, .Net never leaks but you can sometimes make stupid conception mistakes, like forgetting to [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I&#8217;m speaking about a commercial product again. This time it&#8217;s about ANTS Memory Profiler (by Red Gate software, the ones that made .Net reflector). This product will help you identify any .Net memory leak you could have.</p>
<p>The truth is, .Net never leaks but you can sometimes make stupid conception mistakes, like forgetting to remove references of some objects (that may contains heavy references themselves).</p>
<p>The tool allows you to take snapshots of your running application and compare different snapshots. You can see the difference of memory consumption by some objets or the difference of class instance count.</p>
<p>I&#8217;ve used it at work to solve a Sharepoint memory leak. And I finally discovered that the memory leak is really coming from the .Net Sharepoint object model. I&#8217;ll talk about this later. Sharepoint is amazing both ways.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/ants-memory-profiler/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharepoint 2010 on my Windows 7</title>
		<link>http://florent.clairambault.fr/sharepoint-2010-on-my-windows-7?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sharepoint-2010-on-my-windows-7</link>
		<comments>http://florent.clairambault.fr/sharepoint-2010-on-my-windows-7#comments</comments>
		<pubDate>Sun, 20 Dec 2009 21:00:11 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[IIS 7]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[sharepoint 2010]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2244</guid>
		<description><![CDATA[I finally took the time to make &#8220;Sharepoint 2010&#8243; work on my PC. I previously did the installation as recommended by the Microsoft. I had to do one additionnal thing to make it work : &#8220;Desactivate the .Net 4 ISAPI filter&#8221;. In the event logs, I had this error : 1 ISAPI Filter 'C:\Windows\Microsoft.NET\Framework\v4.0.21006\aspnet_filter.dll' could [...]]]></description>
			<content:encoded><![CDATA[<p>I finally took the time to make &#8220;Sharepoint 2010&#8243; work on my PC. I previously did the installation <a href="http://msdn.microsoft.com/en-us/library/ee554869(office.14).aspx">as recommended by the Microsoft</a>. I had to do one additionnal thing to make it work : &#8220;Desactivate the .Net 4 ISAPI filter&#8221;.</p>
<p>In the event logs, I had this error :</p>

<div class="wp_codebox"><table><tr id="p22448"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2244code8"><pre class="txt" style="font-family:monospace;">ISAPI Filter 'C:\Windows\Microsoft.NET\Framework\v4.0.21006\aspnet_filter.dll' could not be loaded due to a configuration problem. The current configuration only supports loading images built for a AMD64 processor architecture. The data field contains the error number. To learn more about this issue, including how to troubleshooting this kind of processor architecture mismatch error, see http://go.microsoft.com/fwlink/?LinkId=29349.</pre></td></tr></table></div>

<p>And then :</p>

<div class="wp_codebox"><table><tr id="p22449"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2244code9"><pre class="txt" style="font-family:monospace;">Could not load all ISAPI filters for site 'SHAREPOINT - 80'.  Therefore site startup aborted.</pre></td></tr></table></div>

<p>And the same of the &#8220;Central Administration&#8221; :</p>

<div class="wp_codebox"><table><tr id="p224410"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2244code10"><pre class="txt" style="font-family:monospace;">Could not load all ISAPI filters for site 'SHAREPOINT CENTRAL ADMINISTRATION V4'.  Therefore site startup aborted.</pre></td></tr></table></div>

<p>It seems to work fine, I&#8217;ll get back on it later. What I could see so far is that it&#8217;s more pretty, easier to use but it doesn&#8217;t seem more efficient or lighter than Sharepoint 2007, you can see the RAM consumption grow high speed with the page loading.</p>
<p><em>Totally unrelated note : I discovered I was one of the <a href="http://www.google.fr/search?q=%22Wordpress+v3.0-alpha%22"> two guys</a> to use the SVN version of WordPress and display it.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-2010-on-my-windows-7/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Inside Sharepoint</title>
		<link>http://florent.clairambault.fr/inside-sharepoint?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=inside-sharepoint</link>
		<comments>http://florent.clairambault.fr/inside-sharepoint#comments</comments>
		<pubDate>Tue, 04 Aug 2009 20:55:01 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.NET Reflector]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[File Disassembler]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint 2007]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1397</guid>
		<description><![CDATA[I recently took the time to take a look inside the Microsoft.Sharepoint.dll using reflector. I&#8217;m not sure I have the right to do that. And I&#8217;m pretty sure I don&#8217;t have the right to publish any code extracted from it, so I won&#8217;t show any. Using SPSite(s) and SPWeb(s) If you do some timing on [...]]]></description>
			<content:encoded><![CDATA[<p>I recently took the time to take a look inside the Microsoft.Sharepoint.dll <a href="http://florent.clairambault.fr/net-reflector-file-disassembler">using reflector</a>. I&#8217;m <a href="http://stackoverflow.com/questions/906746/is-it-legal-to-use-reflector-to-analyze-binaries-when-the-owner-forbids-a-viewing">not sure I have the right to do that</a>. And I&#8217;m pretty sure I don&#8217;t have the right to publish any code extracted from it, so I won&#8217;t show any.</p>
<p><strong>Using SPSite(s) and SPWeb(s)</strong><br />
If you do some timing on the SPWeb creation call (SPSite.OpenWeb), you will find out that it&#8217;s freaking fast (less then 1 ms on my server). The reason is that the most heavy object, the SPRequest class, is shared among SPWebs of a SPSite. The Dispose call only &#8220;Invalidate&#8221; the SPWeb, and if this SPWeb is the owner of the SPRequest (which is SPContext.Current.Web object in most of the cases), it releases it. </p>
<p>Personnaly, I like to have something like that when I use a particular SPWeb along my code in a WebPart :</p>

<div class="wp_codebox"><table><tr id="p139715"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p1397code15"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> SPWeb _rootWeb<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> SPWeb RootWeb <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> _rootWeb <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            _rootWeb <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Site</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RootWeb</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> _rootWeb <span style="color: #008000;">!=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span> <span style="color: #008000;">&#41;</span>
                _toDispose<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> _rootWeb <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> _rootWeb<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span>IDisposable<span style="color: #008000;">&gt;</span> _toDispose <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>IDisposable<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnUnLoad<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> var disp <span style="color: #0600FF; font-weight: bold;">in</span> _toDispose <span style="color: #008000;">&#41;</span>
        disp<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>But the code above won&#8217;t make you gain a 1 ms compared to this code (which is shorter and potentially safer, you don&#8217;t need to dispose everything) :</p>

<div class="wp_codebox"><table><tr id="p139716"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1397code16"><pre class="csharp" style="font-family:monospace;">var rootWeb <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Site</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Your code</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">finally</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> rootWeb <span style="color: #008000;">!=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span> <span style="color: #008000;">&#41;</span>
         rootWeb<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If you had to access some more indirect objects, you certainly should keep the code showed earlier. For instance to use a SuperToken Web :</p>

<div class="wp_codebox"><table><tr id="p139717"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p1397code17"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> SPSite _stSite<span style="color: #008000;">;</span>
SPSite STSite <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> _stSite <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            _stSite <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPSite<span style="color: #008000;">&#40;</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Site</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span>, SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Site</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SystemAccount</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UserToken</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> _stSite <span style="color: #008000;">!=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Site</span> <span style="color: #008000;">&#41;</span>
                _toDispose<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> _stSite <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> _stSite<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> SPWeb _stWeb<span style="color: #008000;">;</span>
SPWeb STWeb <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> _stWeb <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            _stWeb <span style="color: #008000;">=</span> STSite<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenWeb</span><span style="color: #008000;">&#40;</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> _stWeb <span style="color: #008000;">!=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span> <span style="color: #008000;">&#41;</span>
                _toDispose<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> _stWeb <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> _stWeb<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span>IDisposable<span style="color: #008000;">&gt;</span> _toDispose <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>IDisposable<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnUnLoad<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> var disp <span style="color: #0600FF; font-weight: bold;">in</span> _toDispose <span style="color: #008000;">&#41;</span>
        disp<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Here, opening these new SPSite and SPWeb takes 200 ms on my server. Making sure this only happen once per webpart (or better, per page) can really boost your performances.</p>
<p><strong>The SPRequest object</strong><br />
Well&#8230; I was very disapointed, the SPRequest object references an SPRequestInternalClass from the Microsoft.Sharepoint.Library assembly which only uses interrop COM+ methods. So, it&#8217;s basically wrapping COM+ methods. The SPRequest does a lot of exception handling and it keeps track of where (with the stacktrace) it has been created and the size of the &#8220;unmanaged stack&#8221;.</p>
<p><strong>Optimization</strong><br />
I like how they did their code. It&#8217;s pretty optimized (they even use a bunch of gotos). But sometimes there is some weird things and I don&#8217;t know if it&#8217;s the compiler&#8217;s or the developer&#8217;s fault. If you look at the code, they have an SPRequestManager.SPRequestsPerThreadWarning property, which gets (in the Registry, at &#8220;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings\LocalSPRequestWarnCount&#8221;) the max number of opened SPRequest objects opened before logging it (or 8 if this entry doesn&#8217;t exist), that takes twice the amount of code it would normaly require.</p>
<p><strong>The limit of disassembling</strong><br />
The most frustrating part is there are tons of really important methods that are obfuscated (and that .Net reflector doesn&#8217;t disassemble, it could at least give the IL code in comments). I just have :</p>

<div class="wp_codebox"><table><tr id="p139718"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1397code18"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> xxx<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// This item is obfuscated and can not be translated.</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>For instance, I really would have liked to see how works the SPList.Update() method, but it&#8217;s also obfuscated. </p>
<p><strong>You should explore it too</strong><br />
In the Microsoft.Office.Server.Search assembly, you will also find some pretty interesting things. If you look how works the standard search webparts, well you will be pretty disapointed. The advanced search uses an internal (in the C# meaning) shared object.</p>
<p>Before doing your own webpart, you should take a look at the overriden methods of some sharepoint webparts. It can show you, for instance, how to create your own toolparts. In my last project, I used a stupid text property instead of using a DropDownList in a ToolPart, I was very disappointed with myself when I discovered this.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/inside-sharepoint/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharepoint : Using BaseFieldControl</title>
		<link>http://florent.clairambault.fr/sharepoint-using-basefieldcontrol?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sharepoint-using-basefieldcontrol</link>
		<comments>http://florent.clairambault.fr/sharepoint-using-basefieldcontrol#comments</comments>
		<pubDate>Tue, 28 Jul 2009 20:45:50 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[BaseFieldControl]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint 2007]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1519</guid>
		<description><![CDATA[What for ? Sharepoint&#8217;s API provides some standard form controls to render each column. This is the controls used to render the standard add and edit forms. And they all inherit the BaseFieldControl class. In other word : In any SPList, you have some SPField fields and each of these SPField has the super power [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What for ?</strong><br />
Sharepoint&#8217;s API provides some standard form controls to render each column. This is the controls used to render the standard add and edit forms. And they all inherit the BaseFieldControl class.</p>
<p>In other word : In any SPList, you have some SPField fields and each of these SPField has the super power to create a BaseFieldControl. Each BaseFieldControl is a CompositeControl containing ASP.Net controls.</p>
<p>For a single line field, you will just have a wrapped TextBox. But for some more advanced fields like a multi-select lookup field (SPFieldLookup) or rich text field, it can generate some &#8220;complex&#8221; controls and their related javascript code.</p>
<p>The BaseFieldControl can be directly connected to your Sharepoint&#8217;s SPListItem and the BaseFieldControl.Value will match the format required to fill the SPListItem.</p>
<p>You can create your own BaseFieldControl controls, and you can directly mess up the BaseFieldControl.Controls property if you like to (that can be useful). I personally had to create a variation of the MultipleLookupField (that&#8217;s the BaseFieldControl used for a SPLookupField with the AllowMultipleValues property enabled) to create a CheckBoxList of selected items (that what the client wanted).</p>
<p><strong>How to use them to display data ?</strong><br />
You can get it by accessing the SPField.FieldRenderingControl property. It gives you everything you need. Each BaseFieldControl has a ControlMode property (SPControlMode enum) which can be set to New, Edit, Display or Invalid (I don&#8217;t know why this third one exists).</p>
<p>Here is a stripped down version of the necessary code :</p>

<div class="wp_codebox"><table><tr id="p151924"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p1519code24"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> CreateChildControls<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// The web used</span>
     var web <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// This is the code you could have easily guessed :</span>
     var list <span style="color: #008000;">=</span> web<span style="color: #008000;">.</span><span style="color: #0000FF;">Lists</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;MyList&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
     var item <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">GetItemById</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     var field <span style="color: #008000;">=</span> item<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DateOfEvent&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
     var bfc <span style="color: #008000;">=</span> field<span style="color: #008000;">.</span><span style="color: #0000FF;">FieldRenderingControl</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// This is when it becomes tricky :</span>
     var renderContext <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">GetContext</span><span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Context</span>, <span style="color: #FF0000;">0</span>, list<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span>, web <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ListId</span> <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">FieldName</span> <span style="color: #008000;">=</span> field<span style="color: #008000;">.</span><span style="color: #0000FF;">InternalName</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> field<span style="color: #008000;">.</span><span style="color: #0000FF;">InternalName</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ControlMode</span> <span style="color: #008000;">=</span> SPControlMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Edit</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderContext</span> <span style="color: #008000;">=</span> renderContext<span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemContext</span> <span style="color: #008000;">=</span> renderContext<span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">EnableViewState</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
     Controls<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> bfc <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And if you want to use it as an input form, you have to get a non existing SPListItem :</p>

<div class="wp_codebox"><table><tr id="p151925"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1519code25"><pre class="csharp" style="font-family:monospace;">var item <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">GetItemById</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var field <span style="color: #008000;">=</span> item<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DateOfEvent&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>By this :</p>

<div class="wp_codebox"><table><tr id="p151926"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1519code26"><pre class="csharp" style="font-family:monospace;">var item <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var field <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">Fields</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DateOfEvent&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>And then later add this reflection code to force the render context to take into account the temporary created item :</p>

<div class="wp_codebox"><table><tr id="p151927"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1519code27"><pre class="csharp" style="font-family:monospace;">var fiContextItem <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span> SPContext <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;m_item&quot;</span>, BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span> <span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
fiContextItem<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span> renderContext, item <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>If you use multiple SPWeb&#8230;</strong><br />
In your Sharepoint code, you might need to use more than one SPWeb. In that case, you must absolutely take care of creating the right SPContext (using the SPWeb of the SPList of the source SPField) for each BaseFieldControl used.</p>
<p>And if the SPWeb used doesn&#8217;t have the same language as your current site, you can change it easily by doing something like that :</p>

<div class="wp_codebox"><table><tr id="p151928"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1519code28"><pre class="csharp" style="font-family:monospace;">web<span style="color: #008000;">.</span><span style="color: #0000FF;">Locale</span> <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Locale</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>If you create your own BaseFieldControl&#8230;</strong><br />
If you create your own BaseFieldControl, you should really take a look on the disassembled code of the default BaseFieldControls. It could make you save a lot of time and efforts. And please note that the LookupField (which directly inherits BaseFieldControl) isn&#8217;t sealed. Inheriting from it might be a good way to do your own custom lookup BaseFieldControl.</p>
<p><strong>If you enable the ControlMode = SPControlMode.Display</strong><br />
If you want to use your BFC with the Display ControlMode and want to set a value, you must take care of setting the Value BEFORE setting the ControlMode. If you don&#8217;t do that it will work on the first render but fail on the first PostBack : Your value will not be used (and displayed) by the BFC.</p>
<p><strong>If you check the value of the BFC</strong><br />
If you check the value of the BFC, you have to remember that each BFC create its value for the SPField from which it has been created. For instance, empty values can be returned as String.Empty, null or even the 0 integer.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-using-basefieldcontrol/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Debugging on Sharepoint 2007</title>
		<link>http://florent.clairambault.fr/debugging-on-sharepoint-2007?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=debugging-on-sharepoint-2007</link>
		<comments>http://florent.clairambault.fr/debugging-on-sharepoint-2007#comments</comments>
		<pubDate>Sun, 26 Jul 2009 20:45:48 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint 2007]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1361</guid>
		<description><![CDATA[Sharepoint debugging isn&#8217;t fully automated. So you should really know how to debug and diagnose your assemblies in any given situation. 1. Attaching to the process It only applies to a debugging environnement. This is the one that everybody knows (or should at least). You deploy your DLL in the GAC, restart your application pool, [...]]]></description>
			<content:encoded><![CDATA[<p>Sharepoint debugging isn&#8217;t fully automated. So you should really know how to debug and diagnose your assemblies in any given situation.</p>
<p><strong>1. Attaching to the process</strong><br />
It only applies to a debugging environnement. </p>
<p>This is the one that everybody knows (or should at least). You deploy your DLL in the GAC, restart your application pool, access your sharepoint web application in order to load the application pool and the DLL and then attach to the right w3wp.exe process (or every w3wp.exe process if you don&#8217;t really know which one to choose).</p>
<p><strong>2. Displaying where the exception happens</strong><br />
It should be used everywhere.</p>
<p>Just after deploying your DLL into the GAC, you should deploy the PDB file with it. In your exception management code, you have the exact line where the exception was thrown. Wether your users report it (with the exact line number), you see it in the logs or you have an automatic reporting system, the point is : You will know exactly where it fails.</p>
<p>If you have a WSP deployment method, you will have :</p>

<div class="wp_codebox"><table><tr id="p136133"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1361code33"><pre class="batch" style="font-family:monospace;">rem This WSP File contains the MyCorp.MyApp.MyLib library with the 0x123456789 public key token
stsadm -o addsolution -filename %WSPFILE%</pre></td></tr></table></div>

<p>If you have a DLL deployment method, you will have :</p>

<div class="wp_codebox"><table><tr id="p136134"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1361code34"><pre class="batch" style="font-family:monospace;">gacutil /if GAC\MyCorp.MyApp.MyLib.dll</pre></td></tr></table></div>

<p>Either way, you need to add the PDB with this command :</p>

<div class="wp_codebox"><table><tr id="p136135"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1361code35"><pre class="batch" style="font-family:monospace;">subst X: c:\windows\assembly\gac_msil
copy GAC\MyCorp.MyApp.MyLib.pdb X:\MyCorp.MyApp.MyLib\1.0.0.0_123456789\</pre></td></tr></table></div>

<p>If you&#8217;re not willing to give away your PDB file (it contains you complete code source and consumes space), you can find out where you app exactly failed just from the offset of the stacktrace reported by Sharepoint (with the <a href="http://blog.thekid.me.uk/archive/2007/02/15/a-solution-to-quot-an-unexpected-error-has-occurred-quot-in-wss-v3.aspx">CustomError=&#8221;Off&#8221; and StackTrace=&#8221;true&#8221;</a> in the web.config). Some people explain how to do it <a href="http://stackoverflow.com/questions/1037925/recreate-stack-trace-with-line-numbers-from-user-bug-report-in-net">here</a>. Answer &#8220;3&#8243; allows you to get the IL offset like ASP.Net does in its (non customized) error page.</p>
<p><strong>3. Launching the debugger from the code</strong><br />
This is very useful for features deactivation/uninstallation/installation/activation code.</p>
<p>You just have to add this line when you want to ask the debugger to attach to the assembly.</p>

<div class="wp_codebox"><table><tr id="p136136"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1361code36"><pre class="csharp" style="font-family:monospace;">Debugger<span style="color: #008000;">.</span><span style="color: #0000FF;">Launch</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>4. Other options</strong><br />
<a href="http://blog.thekid.me.uk/archive/2007/07/25/debugging-tips-for-sharepoint-and-wss-exceptions.aspx">This article</a> focuses on hardcore problems : Problems that occur inside sharepoint or weird problem that only appear on your production servers.</p>
<p>The WinDBG method seems a little bit overkill to me. Mostly because you still can&#8217;t analyze the state of the local variables with our current tools (but I hope it will be made available in a short future).</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/debugging-on-sharepoint-2007/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharepoint : SPWebConfigModification</title>
		<link>http://florent.clairambault.fr/sharepoint-spwebconfigmodification?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sharepoint-spwebconfigmodification</link>
		<comments>http://florent.clairambault.fr/sharepoint-spwebconfigmodification#comments</comments>
		<pubDate>Sun, 19 Jul 2009 08:30:10 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[binding redirect]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint 2007]]></category>
		<category><![CDATA[SPWebConfigModification]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1265</guid>
		<description><![CDATA[I&#8217;ve seen lots of Sharepoint software having an installation manual of at least 20 pages (sometimes 60). Most of the operations they describe could be fully automated. And these software were made by freaking big companies. They should be ashamed of themselves. Maybe they just forgot that computer science is all about saving some time [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen lots of Sharepoint software having an installation manual of at least 20 pages (sometimes 60). Most of the operations they describe could be fully automated. And these software were made by freaking big companies. They should be ashamed of themselves. Maybe they just forgot that computer science is all about saving some time (and not only making money).</p>
<p>One good example is MOSS Faceted search 2.5 (I haven&#8217;t tested the 3.0). It takes at least 40 minutes to uninstall this crap. Why isn&#8217;t it just ONE WSP or at least one BAT file launching the WSP installation and the other steps ? Is there any real reason for that ?</p>
<p>The SPWebConfigModification solves this web.config modification problem. It&#8217;s a pretty interesting feature of Sharepoint. You can edit the web.config file without any complex XML parsing. It doesn&#8217;t even matter that you add XML or not. The SPWebConfigModification class manages your add/mod/del operations easily. The only restriction is that you have to add your first configuration elements using the SPWebConfigModification.  You cannot modify existing elements this way.</p>

<div class="wp_codebox"><table><tr id="p126539"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p1265code39"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Source : http://sharethelearning.blogspot.com/2008/01/adding-bindingredirect-to-webconfig.html</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddBindingRedirect<span style="color: #008000;">&#40;</span> SPWebApplication webApp, <span style="color: #6666cc; font-weight: bold;">string</span> libraryName, <span style="color: #6666cc; font-weight: bold;">string</span> libraryPublicToken, <span style="color: #6666cc; font-weight: bold;">string</span> oldVersion, <span style="color: #6666cc; font-weight: bold;">string</span> newVersion <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	var ownerName <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;BindingRedirect.{0}&quot;</span>, libraryName <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// We delete last bindingRedirect</span>
&nbsp;
		var list <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>SPWebConfigModification<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> SPWebConfigModification mod <span style="color: #0600FF; font-weight: bold;">in</span> webApp<span style="color: #008000;">.</span><span style="color: #0000FF;">WebConfigModifications</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			list<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> mod <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> var mod <span style="color: #0600FF; font-weight: bold;">in</span> list <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> mod<span style="color: #008000;">.</span><span style="color: #0000FF;">Owner</span> <span style="color: #008000;">==</span> ownerName <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				LoggerCommon<span style="color: #008000;">.</span><span style="color: #0000FF;">LogVerbose</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Deleting: <span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, mod<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				webApp<span style="color: #008000;">.</span><span style="color: #0000FF;">WebConfigModifications</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span> mod <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// We add our redirection</span>
		<span style="color: #6666cc; font-weight: bold;">String</span> path <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;configuration/runtime/*[namespace-uri()='urn:schemas-microsoft-com:asm.v1' and local-name()='assemblyBinding']&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #6666cc; font-weight: bold;">String</span> name <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;*[namespace-uri()='urn:schemas-microsoft-com:asm.v1' and local-name()='dependentAssembly']/*[namespace-uri()='urn:schemas-microsoft-com:asm.v1' and local-name()='assemblyIdentity'][@name='{0}']/parent::*&quot;</span>, libraryName <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #6666cc; font-weight: bold;">String</span> webConfigValue <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">@&quot;
	&lt;dependentAssembly&gt;
		&lt;!-- Added automatically at {4} --&gt;
		&lt;assemblyIdentity name='{0}' publicKeyToken='{1}' culture='neutral' /&gt;
		&lt;bindingRedirect oldVersion='{2}' newVersion='{3}' /&gt;
	&lt;/dependentAssembly&gt;
&quot;</span>, libraryName, libraryPublicToken, oldVersion, newVersion, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		SPWebConfigModification mod <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPWebConfigModification<span style="color: #008000;">&#40;</span> name, path <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		mod<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> webConfigValue<span style="color: #008000;">;</span>
		mod<span style="color: #008000;">.</span><span style="color: #0000FF;">Owner</span> <span style="color: #008000;">=</span> ownerName<span style="color: #008000;">;</span>
		mod<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequence</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
		mod<span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">=</span> SPWebConfigModification<span style="color: #008000;">.</span><span style="color: #0000FF;">SPWebConfigModificationType</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EnsureChildNode</span><span style="color: #008000;">;</span>
&nbsp;
		webApp<span style="color: #008000;">.</span><span style="color: #0000FF;">WebConfigModifications</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> mod <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// We save our changes</span>
		webApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		SPFarm<span style="color: #008000;">.</span><span style="color: #0000FF;">Local</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Services</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&lt;</span>SPWebService<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ApplyWebConfigModifications</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If you do a binding redirect from 1.0.0.0 to 1.0.1.0 and your .webpart file references the 1.0.0.0 version, sharepoint will store your webpart as referencing the 1.0.1.0 assembly (and not 1.0.0.0 as you told him). So if you then chose to change the binding redirect from 1.0.0.0 to 1.0.2.0, without redirecting 1.0.1.0 to 1.0.2.0, your webpart will still be the 1.0.1.0 version.  </p>
<p>I haven&#8217;t tested this for event receivers, but the way they are registered (Sharepoint doesn&#8217;t check the assembly you add to the event receivers of a list), I would guess Sharepoint doesn&#8217;t change the assembly version.</p>
<p>To solve this webpart updating problem, you can use ranged binding redirect (.Net rules) :</p>

<div class="wp_codebox"><table><tr id="p126540"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1265code40"><pre class="csharp" style="font-family:monospace;">var site <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SPSite<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://localhost&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
AddBindingRedirect<span style="color: #008000;">&#40;</span> site<span style="color: #008000;">.</span><span style="color: #0000FF;">WebApplication</span>, <span style="color: #666666;">&quot;MyCorp.MyApp.MyLib&quot;</span>, <span style="color: #666666;">&quot;0x123456789&quot;</span>, <span style="color: #666666;">&quot;1.0.0.0-1.0.3.5&quot;</span>, <span style="color: #666666;">&quot;1.0.3.5&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>That means that any webpart using a previous version of the &#8220;MyCorp.MyApp.MyLib&#8221; assembly between 1.0.0.0 and 1.0.3.5 will be redirected to the 1.0.3.5 version.</p>
<p>If your assembly contains page code-behind classes, you should take care of updating the aspx files as well.</p>
<p><u>Reference :</u>
<ul>
<li><a href="http://sharethelearning.blogspot.com/2008/01/adding-bindingredirect-to-webconfig.html">The original code</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-spwebconfigmodification/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sharepoint 2010</title>
		<link>http://florent.clairambault.fr/sharepoint-2010?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sharepoint-2010</link>
		<comments>http://florent.clairambault.fr/sharepoint-2010#comments</comments>
		<pubDate>Sat, 18 Jul 2009 09:44:10 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[sharepoint 2010]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1273</guid>
		<description><![CDATA[Finally, Microsoft decided to communicate on Sharepoint 2010 on their website. Wow, they really seem to have understood all of the sharepoint developers problems. Look at the full developer video. Everything is incredible : Sharepoint Extensions seem complete and cutomizable, there is the new developper dashboard which displays for instance the number of opened SPRequest [...]]]></description>
			<content:encoded><![CDATA[<p>Finally, Microsoft decided to communicate on <a href="http://sharepoint.microsoft.com/2010/">Sharepoint 2010 on their website</a>.</p>
<p>Wow, they really seem to have understood all of the sharepoint developers problems. Look at the full <a href="http://sharepoint.microsoft.com/2010/Sneak_Peek/Pages/Developer-Video.aspx">developer video</a>. Everything is incredible : Sharepoint Extensions seem complete and cutomizable, there is the new developper dashboard which displays for instance the number of opened SPRequest the time spent on each loading stage of the page, the Business Data Catalog model allows to use any data inside sharepoint within &#8220;external lists&#8221;, the official LinQ for Sharepoint, the new WSP package which allow to specify which deployment actions to use. This runs on top of Windows Server 2008 and Visual Studio 2010.</p>
<p>The bad news is : CAML hasn&#8217;t be thrown away, non-generic types like ListItemCollection are still used. But I guess, they can&#8217;t change everything instantly.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-2010/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharepoint &#8211; Updating webparts</title>
		<link>http://florent.clairambault.fr/sharepoint-updating-webparts?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sharepoint-updating-webparts</link>
		<comments>http://florent.clairambault.fr/sharepoint-updating-webparts#comments</comments>
		<pubDate>Mon, 06 Jul 2009 22:13:15 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Webparts]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2144</guid>
		<description><![CDATA[I&#8217;ve talked about this before. When you create versions of webparts, you have two main solutions to update existing webparts : Create a binding redirect within the web.config file. Update each and every webpart instance of the collection site (or just the site). The first one is really the best solution : It&#8217;s simple and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about this <a href="http://florent.clairambault.fr/sharepoint-spwebconfigmodification">before</a>. When you create versions of webparts, you have two main solutions to update existing webparts :</p>
<ul>
<li>Create a binding redirect within the web.config file.</li>
<li>Update each and every webpart instance of the collection site (or just the site).</li>
</ul>
<p>The first one is really the best solution : It&#8217;s simple and very fast. But, it applies to the whole Web Application. Sometimes, the client might want to only update the site collection or even the website. In this case, we need to apply the second solution : update each webpart.</p>
<p>Updating each webpart requires you to create a solution that will go through all the pages of your website and replace each instance of a webpart by an other instance.</p>
<p>I can&#8217;t give the complete code (as it&#8217;s still not mine), but only some little hints that can help you solve this problem.</p>
<p>You have to analyse each an every file of your websites :</p>

<div class="wp_codebox"><table><tr id="p214443"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2144code43"><pre class="csharp" style="font-family:monospace;">		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AnalyseFolder<span style="color: #008000;">&#40;</span> List<span style="color: #008000;">&lt;</span>SPFile<span style="color: #008000;">&gt;</span> list, SPFolder folder <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> SPFile file <span style="color: #0600FF; font-weight: bold;">in</span> folder<span style="color: #008000;">.</span><span style="color: #0000FF;">Files</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				var fileName <span style="color: #008000;">=</span> file<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> fileName<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;.aspx&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> fileName<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;.master&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
					list<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> file <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> SPFolder subFolder <span style="color: #0600FF; font-weight: bold;">in</span> folder<span style="color: #008000;">.</span><span style="color: #0000FF;">SubFolders</span> <span style="color: #008000;">&#41;</span>
				AnalyseFolder<span style="color: #008000;">&#40;</span> list, subFolder <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>You could restrict youself to the &#8220;pages&#8221; folder, but you would miss some pages like the layouts.</p>
<p>Then for each file of the class :</p>

<div class="wp_codebox"><table><tr id="p214444"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code" id="p2144code44"><pre class="csharp" style="font-family:monospace;">		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> TreatPage<span style="color: #008000;">&#40;</span> SPWeb web, SPFile file <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> file<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;.master&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
					Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogVerbose</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> is a masterpage...&quot;</span>, file<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
				SPWeb managerWeb <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> file<span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
						Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogNotice</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> isn't part of a document library...&quot;</span>, file<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
						<span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
					<span style="color: #008000;">&#125;</span>
&nbsp;
					<span style="color: #008080; font-style: italic;">// Sometimes the code above works but this will still throw an exception</span>
					file<span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> SPException ex <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
					Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogNotice</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> isn't part of a document library...&quot;</span>, file<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
				<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> var manager <span style="color: #008000;">=</span> file<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLimitedWebPartManager</span><span style="color: #008000;">&#40;</span> PersonalizationScope<span style="color: #008000;">.</span><span style="color: #0000FF;">Shared</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
						managerWeb <span style="color: #008000;">=</span> manager<span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
						<span style="color: #008080; font-style: italic;">// we reload the same file but in the SPLimitedWebpartManager's web context</span>
						file <span style="color: #008000;">=</span> managerWeb<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFile</span><span style="color: #008000;">&#40;</span> file<span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UniqueId</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
						var collection <span style="color: #008000;">=</span> manager<span style="color: #008000;">.</span><span style="color: #0000FF;">WebParts</span><span style="color: #008000;">;</span>
&nbsp;
						var toDelete <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>WebPart<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
						var previousWebparts <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>WebPart<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
						<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> WebPart wp <span style="color: #0600FF; font-weight: bold;">in</span> collection <span style="color: #008000;">&#41;</span>
							previousWebparts<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> wp <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
						Boolean madeChanges <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
						var toDispose <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>WebPart<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
						<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> WebPart wp <span style="color: #0600FF; font-weight: bold;">in</span> previousWebparts <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
							Boolean deleteWebPart<span style="color: #008000;">;</span>
&nbsp;
							var newWebPart <span style="color: #008000;">=</span> GetNewWebPart<span style="color: #008000;">&#40;</span> wp, <span style="color: #0600FF; font-weight: bold;">out</span> deleteWebPart <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
							<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> newWebPart <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> deleteWebPart <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
								<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>madeChanges <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
									CheckOutPage<span style="color: #008000;">&#40;</span> file <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
									madeChanges <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
								<span style="color: #008000;">&#125;</span>
&nbsp;
								Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogVerbose</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Replacing webpart <span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> by <span style="color: #008080; font-weight: bold;">\&quot;</span>{1}<span style="color: #008080; font-weight: bold;">\&quot;</span>...&quot;</span>, wp<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span>, newWebPart <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">?</span> newWebPart<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;- none -&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
								<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> newWebPart <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span>
									manager<span style="color: #008000;">.</span><span style="color: #0000FF;">AddWebPart</span><span style="color: #008000;">&#40;</span> newWebPart, wp<span style="color: #008000;">.</span><span style="color: #0000FF;">Zone</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">?</span> wp<span style="color: #008000;">.</span><span style="color: #0000FF;">Zone</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;0&quot;</span>, wp<span style="color: #008000;">.</span><span style="color: #0000FF;">ZoneIndex</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
								<span style="color: #008080; font-style: italic;">// it's either replaced or deleted : it will need to be removed from the page</span>
								toDelete<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> wp <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
								<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> newWebPart <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span>
									toDispose<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> newWebPart <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
							<span style="color: #008000;">&#125;</span>
						<span style="color: #008000;">&#125;</span>
&nbsp;
						<span style="color: #008080; font-style: italic;">// If we made some changes to the file, we have to save it</span>
						<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> madeChanges <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
							<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> var wp <span style="color: #0600FF; font-weight: bold;">in</span> toDelete <span style="color: #008000;">&#41;</span>
								manager<span style="color: #008000;">.</span><span style="color: #0000FF;">DeleteWebPart</span><span style="color: #008000;">&#40;</span> wp <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
							file<span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
							CheckInPage<span style="color: #008000;">&#40;</span> file, <span style="color: #666666;">&quot;Webpart update&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
						<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
						<span style="color: #008080; font-style: italic;">// All the webparts found will need to be disposed</span>
						toDispose<span style="color: #008000;">.</span><span style="color: #0000FF;">AddRange</span><span style="color: #008000;">&#40;</span> previousWebparts <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
						<span style="color: #008080; font-style: italic;">// We dispose everyone</span>
						<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span> var wp <span style="color: #0600FF; font-weight: bold;">in</span> toDispose <span style="color: #008000;">&#41;</span>
							wp<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #008000;">&#125;</span>
				<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">finally</span> <span style="color: #008000;">&#123;</span>
					<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> managerWeb <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> managerWeb <span style="color: #008000;">!=</span> web <span style="color: #008000;">&#41;</span>
						MemoryManagement<span style="color: #008000;">.</span><span style="color: #0000FF;">MightDispose</span><span style="color: #008000;">&#40;</span> web <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span> Exception ex <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				Logger<span style="color: #008000;">.</span><span style="color: #0000FF;">LogException</span><span style="color: #008000;">&#40;</span> ex, <span style="color: #666666;">&quot;UpdateWebpartsInPage.TreatPage( SPFile.Url=<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> )&quot;</span>, file<span style="color: #008000;">.</span><span style="color: #0000FF;">Url</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-updating-webparts/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/48 queries in 0.023 seconds using disk: basic
Object Caching 1566/1662 objects using disk: basic

Served from: florent.clairambault.fr @ 2012-02-07 11:15:41 -->
