Upload an Image to your SharePoint Site

24 Apr

It’s very easy to add a file/image to your SharePoint site via the Object Model.


            System.IO.MemoryStream str = <span class="kwrd">new</span> System.IO.MemoryStream();<br />            SPSite site = <span class="kwrd">new</span> SPSite(Page.Request.Url.ToString());<br />            SPWeb web = site.OpenWeb(); <br /><br />            Bitmap.FromFile(<span class="str">"Source URL"</span>).Save(str,Imaging.ImageFormat.Gif);<br />            web.AllowUnsafeUpdates = <span class="kwrd">true</span>;<br />            <br />            web.Folders[<span class="str">"PublishingImages"</span>].Files.Add(<span class="str">"filename"</span>,str);<br />            web.Folders[<span class="str">"PublishingImages"</span>].Update();<br />  <br />            web.Close();<br />            site.Close();

I was also interested in adding an image to an image library via the OM, but haven’t had a chance to play around with it yet, let me know if you’ve done this already.

Tweet about this on TwitterShare on Google+Share on FacebookShare on RedditEmail this to someoneShare on LinkedInShare on StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *