<?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>SharePoint Fun &#187; Features</title>
	<atom:link href="http://blog.qumsieh.ca/tag/features/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.qumsieh.ca</link>
	<description>Developer's blog related to ASP.NET, SharePoint and Telerik Web Controls</description>
	<lastBuildDate>Tue, 27 Jul 2010 03:07:36 +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>How to Overwrite Existing SharePoint Files Using a Feature</title>
		<link>http://blog.qumsieh.ca/2008/02/01/how-to-overwrite-existing-sharepoint-files-using-a-feature/</link>
		<comments>http://blog.qumsieh.ca/2008/02/01/how-to-overwrite-existing-sharepoint-files-using-a-feature/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 00:47:14 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Features]]></category>
		<category><![CDATA[Object Model]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=59</guid>
		<description><![CDATA[In this example, I will focus specifically on a scenario where I had to write code that would overwrite the CustomQuickAccess.xml file located within the masterpage gallery. Typically, up until this point, I had been dealing with simply uploading new files to a SharePoint library when a feature was activated. Simple enough, however, what happens [...]]]></description>
			<content:encoded><![CDATA[<p>In this example, I will focus specifically on a scenario where I had to write code that would overwrite the CustomQuickAccess.xml file located within the masterpage gallery. </p>
<p>Typically, up until this point, I had been dealing with simply uploading new files to a SharePoint library when a feature was activated. Simple enough, however, what happens if you&#8217;re trying to overwrite a file that already exists? I&#8217;ve attached the file to this post <a href='http://blog.qumsieh.ca/wp-content/uploads/2008/12/overwrite.zip'>here</a> for reference, but let&#8217;s go over the basics for doing something of this sort.</p>
<ol>
<li>The first step was to create a new feature and overwrite the FeatureActivated method. I&#8217;ll assume you&#8217;re familiar with this already, but if not, feel free to drop me a note and I&#8217;ll assist where I can.</li>
<li>The code inside of the FeatureActivated method is pretty straightforward. Create a web object, declare a variable to store the file system path where the feature was activated so that we can grab the file we want to overwrite with and finally, call another method to do the file overwriting, with elevated privileges.

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><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>
&nbsp;
<span style="color: #000000;">&#123;</span>
&nbsp;
WriteMessageToEventLog<span style="color: #000000;">&#40;</span>â€œFeature Activatedâ€<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">try</span>
&nbsp;
<span style="color: #000000;">&#123;</span>
&nbsp;
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: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// set the directory path on the file system where the feature was activated</span>
&nbsp;
directoryPath <span style="color: #008000;">=</span> properties.<span style="color: #0000FF;">Definition</span>.<span style="color: #0000FF;">RootDirectory</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// run with elevated permissions so we can overwrite the file</span>
&nbsp;
SPSecurity.<span style="color: #0000FF;">RunWithElevatedPrivileges</span><span style="color: #000000;">&#40;</span>OverwriteFile<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#123;</span>
&nbsp;
WriteMessageToEventLog<span style="color: #000000;">&#40;</span>ex.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Now let&#8217;s look at the OverwriteFile method. What we&#8217;re doing here is reading the contents of the file into a byte array and uploading that file into the<strong> Editing Menu</strong> folder, specifiying that we want to overwrite the file.

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> OverwriteFile<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// create a new site object so that elevation works properly</span>
&nbsp;
SPSite site <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite<span style="color: #000000;">&#40;</span>web.<span style="color: #0000FF;">Site</span>.<span style="color: #0000FF;">ID</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// copy to location</span>
&nbsp;
<span style="color: #FF0000;">string</span> url <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// get the url to the local file</span>
&nbsp;
<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> localFile <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">Directory</span>.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span>directoryPath, â€<span style="color: #008000;">*</span>.<span style="color: #0000FF;">xml</span>â€, <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">SearchOption</span>.<span style="color: #0000FF;">TopDirectoryOnly</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// define a fstream object so we can read the contents of the file into a byte array</span>
&nbsp;
FileStream fstream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">OpenRead</span><span style="color: #000000;">&#40;</span>localFile<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> contents <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>fstream.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
fstream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>contents, <span style="color: #FF0000;">0</span>, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>fstream.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
fstream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// get a handle to the master page gallery</span>
&nbsp;
SPList masterPageGallery <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: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span>â€œMaster Page Galleryâ€<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// get a handle to the folder we want to upload the file to</span>
&nbsp;
SPFolder editingMenuFolder <span style="color: #008000;">=</span> masterPageGallery.<span style="color: #0000FF;">RootFolder</span>.<span style="color: #0000FF;">SubFolders</span><span style="color: #000000;">&#91;</span>â€œEditing Menuâ€<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
SPFile customQuickAccessFile <span style="color: #008000;">=</span> editingMenuFolder.<span style="color: #0000FF;">Files</span><span style="color: #000000;">&#91;</span>â€œCustomQuickAccess.<span style="color: #0000FF;">xml</span>â€<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// build the destination copy url</span>
&nbsp;
url <span style="color: #008000;">=</span> site.<span style="color: #0000FF;">Url</span> <span style="color: #008000;">+</span> â€<span style="color: #008000;">/</span>â€ <span style="color: #008000;">+</span> editingMenuFolder.<span style="color: #0000FF;">Url</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&gt;</span> â€<span style="color: #008000;">/</span>â€<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// check out the file, replace it with the modified one, and check it back in, publish and approve</span>
&nbsp;
customQuickAccessFile.<span style="color: #0000FF;">CheckOut</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
customQuickAccessFile.<span style="color: #0000FF;">CopyTo</span><span style="color: #000000;">&#40;</span>url <span style="color: #008000;">+</span> â€œCustomQuickAccess_Original.<span style="color: #0000FF;">xml</span>â€, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
WriteMessageToEventLog<span style="color: #000000;">&#40;</span>editingMenuFolder.<span style="color: #0000FF;">ServerRelativeUrl</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
WriteMessageToEventLog<span style="color: #000000;">&#40;</span>editingMenuFolder.<span style="color: #0000FF;">Url</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// check in new file</span>
&nbsp;
customQuickAccessFile <span style="color: #008000;">=</span> editingMenuFolder.<span style="color: #0000FF;">Files</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>url <span style="color: #008000;">+</span> â€œCustomQuickAccess.<span style="color: #0000FF;">xml</span>â€, contents, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
customQuickAccessFile.<span style="color: #0000FF;">CheckIn</span><span style="color: #000000;">&#40;</span>â€œFile over<span style="color: #008000;">-</span>written by activiating the Published Page View Featureâ€<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
customQuickAccessFile.<span style="color: #0000FF;">Publish</span><span style="color: #000000;">&#40;</span>â€œFile published by activating the Published Page View Featureâ€<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
customQuickAccessFile.<span style="color: #0000FF;">Approve</span><span style="color: #000000;">&#40;</span>â€œApproved by Published Page View Featureâ€<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
site.<span style="color: #0000FF;">Close</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></pre></td></tr></table></div>

</li>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2008/02/01/how-to-overwrite-existing-sharepoint-files-using-a-feature/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
