<?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>Welcome to ShanthaKumar&#039;s Blog &#187; Attachment</title>
	<atom:link href="http://ktskumar.com/blog/tag/attachment/feed/" rel="self" type="application/rss+xml" />
	<link>http://ktskumar.com/blog</link>
	<description>SharePoint, Silverlight and more.........</description>
	<lastBuildDate>Thu, 10 Jun 2010 01:42:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Uploading File from Local Machine to SharePoint ListItem as an attachment</title>
		<link>http://ktskumar.com/blog/2008/03/31/uploading-file-from-local-machine-to-sharepoint-listitem-as-an-attachment/</link>
		<comments>http://ktskumar.com/blog/2008/03/31/uploading-file-from-local-machine-to-sharepoint-listitem-as-an-attachment/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 05:04:19 +0000</pubDate>
		<dc:creator>Shantha Kumar</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Attachment]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[ListIte]]></category>
		<category><![CDATA[MOSS]]></category>
		<category><![CDATA[Upload]]></category>

		<guid isPermaLink="false">http://ktskumar.wordpress.com/?p=17</guid>
		<description><![CDATA[To attach the file from Local Machine to SharePoint ListItem as an attachment, Import the System.IO namespace to use the Stream Class to read the file contents.

Use System.Web.UI.WebControls.FileUpload control is named as fileUpload in code. 
Sample code for uploading the file from Local Machine to ListItem attachments as follows,
//Check the FileUpload control has the file
if (fileUpload.PostedFile [...]]]></description>
			<content:encoded><![CDATA[<p style="margin:0 0 10pt;" class="MsoNormal"><font face="Times New Roman">To attach the file from Local Machine to SharePoint ListItem as an attachment, </font><span style="font-size:11pt;line-height:115%;font-family:Calibri;"><font face="Times New Roman">Import the <strong>System.IO</strong> namespace to use the <strong>Stream</strong> Class to read the file contents.</font></span></p>
<p><span style="font-size:11pt;line-height:115%;font-family:Calibri;"></span><span style="font-size:11pt;line-height:115%;font-family:Calibri;"></span><span style="font-size:11pt;line-height:115%;font-family:Calibri;"></span><span style="font-size:11pt;line-height:115%;font-family:Calibri;"></span><span style="font-size:11pt;line-height:115%;font-family:Calibri;"></p>
<p style="margin:0 0 10pt;" class="MsoNormal"><font face="Times New Roman">Use System.Web.UI.WebControls.FileUpload control is named as fileUpload in code. </font></p>
<p style="margin:0 0 10pt;" class="MsoNormal">Sample code for uploading the file from Local Machine to ListItem attachments as follows,</p>
<p><span style="font-size:10pt;color:green;font-family:'Courier New';">//Check the FileUpload control has the file</span></p>
<p><span style="font-size:10pt;color:green;font-family:'Courier New';"></span><span style="font-size:10pt;color:blue;font-family:'Courier New';">if</span><span style="font-size:10pt;font-family:'Courier New';"> (fileUpload.PostedFile != <span style="color:blue;">null</span>)</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;font-family:'Courier New';">{</span><span style="font-size:10pt;font-family:'Courier New';">item = list.Items.Add();</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;font-family:'Courier New';">item[<span style="color:#a31515;">"Title"</span>] = fileUpload.FileName;</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;color:green;font-family:'Courier New';">//Read the Contents from the file in Local machine</span></p>
<p><span style="font-size:10pt;color:green;font-family:'Courier New';"></span><span style="font-size:10pt;color:#2b91af;font-family:'Courier New';">Stream</span><span style="font-size:10pt;font-family:'Courier New';"> fs = fileUpload.PostedFile.InputStream;<span>                    </span></span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"><span></span></span><span style="font-size:10pt;color:blue;font-family:'Courier New';">byte</span><span style="font-size:10pt;font-family:'Courier New';">[] fileContents = <span style="color:blue;">new</span> <span style="color:blue;">byte</span>[fs.Length];</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;font-family:'Courier New';">fs.Read(fileContents, 0, (<span style="color:blue;">int</span>)fs.Length);</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;font-family:'Courier New';">fs.Close();</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;color:green;font-family:'Courier New';">// Add the file to the ListItem as an Attachment</span></p>
<p><span style="font-size:10pt;color:green;font-family:'Courier New';"></span><span style="font-size:10pt;color:#2b91af;font-family:'Courier New';">SPAttachmentCollection</span><span style="font-size:10pt;font-family:'Courier New';"> attachments = item.Attachments;</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;color:blue;font-family:'Courier New';">string</span><span style="font-size:10pt;font-family:'Courier New';"> fileName = <span style="color:#2b91af;">Path</span>.GetFileName(fileUpload.PostedFile.FileName);</span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"></span><span style="font-size:10pt;font-family:'Courier New';">attachments.Add(fileName, fileContents);</span><span style="font-size:10pt;font-family:'Courier New';"><span> </span></span></p>
<p><span style="font-size:10pt;font-family:'Courier New';"><span></span>item.Update();<span>                                            </span></span></p>
<p style="margin:0 0 10pt;" class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:'Courier New';">}</span></p>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://ktskumar.com/blog/2008/03/31/uploading-file-from-local-machine-to-sharepoint-listitem-as-an-attachment/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
