SharePoint Pals
 | Sign In
Programmatically upload and retrieve image from SharePoint Picture Library
by Gopal Muthukrishnan 22 Jan 2013
Author
SharePoint Developer, SharePoint Frontier
Visits   
Today  :   1     Total  :    25905

In this blog post we can see how to programmatically upload and retrieve images in SharePoint Picture Library. The uploaded file is read as stream and inserted into a picture library along with the additional attributes of the file.

Follow the below Steps to perform the upload and download functionality

Step 1

Create one picture library and named it “MySamplePictureLib“ in a SharePoint 2013

Step 2

Open the visual studio and create a new SharePoint Project and add a Visual web Part and name it as “PictureSamples

Step 3

Click the VisualWebPart1 paste the below code inside the VisualWebPart1UserControl.ascx

 <table style="width: 424px">
     <tr>
         <td>
             <asp:FileUpload ID="FileUpload1" runat="server" />
         </td>
     </tr>
     <tr>
         <td>
             <asp:Button ID="Btnupload" runat="server" Text="Upload" OnClick="Btnupload_Click"
                 Width="90px" />
         </td>
     </tr>
     <asp:GridView ID="GrdImage" runat="server" AutoGenerateColumns="false">
         <Columns>
             <asp:TemplateField HeaderText="Image">
                 <ItemTemplate>
                     <img id="img" src='<%# Eval("URL") %>' alt="" />
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Imageurl">
                 <ItemTemplate>
                     <asp:Label ID="Lblurl" runat="server" Text='<%# Eval("URL") %>'></asp:Label>
                 </ItemTemplate>
             </asp:TemplateField>
         </Columns>
     </asp:GridView>
 </table>

Step 4

Click VisualWebPart1UserControl.ascx.cs paste the below inside the partial Class

 public class ImageCollection
 {
     public ImageCollection(string ImageUrl, string ImageName) 
     {
         _URL = ImageUrl;
         _Name = ImageName; 
     }
 
     private string  _Name;
     private string _URL;
 
     public string Name
     {
         get { return _Name; }
         set { _Name = value; } 
     }
 
     public string URL
     {
         get { return _URL; }
         set { _URL = value; }
     }
 }
 
 protected void Page_Load(object sender, EventArgs e)
 {
     GridDatabind();
 }
 
 public void GridDatabind()
 {
     SPSecurity.RunWithElevatedPrivileges(delegate()
         {
             using (SPSite site = new Microsoft.SharePoint.SPSite(SPContext.Current.Site.Url))
             {
                 using (SPWeb web = site.OpenWeb())
                 {
                     SPList LstPicture = web.Lists["MySamplePictureLib"];
                     List<ImageCollection> lsts = new List<ImageCollection>();
                     foreach (SPListItem item in LstPicture.Items)  
                     {
                         string ImageUrl = Convert.ToString(item["Thumbnail URL"]);
                         string ImageName = Convert.ToString(item["Name"]);
                         lsts.Add(new ImageCollection(ImageUrl, ImageName));
                     }
                     GrdImage.DataSource = lsts;
                     GrdImage.DataBind();
                 }
             }
         });
 }
 
 protected void Btnupload_Click(object sender, EventArgs e)
 {
     using (SPSite site = new Microsoft.SharePoint.SPSite(SPContext.Current.Site.Url))
     {
         using (SPWeb web = site.OpenWeb())
         {
             web.AllowUnsafeUpdates = true;
             Stream StreamImage = null;
             if (FileUpload1.HasFile)
             {
                 StreamImage = FileUpload1.PostedFile.InputStream;
             }
             SPList pics = web.Lists["MySamplePictureLib"];
             pics.RootFolder.Files.Add(FileUpload1.FileName, StreamImage);
             GridDatabind();
         }
     }
 }

Step 5

Deploy and run your project you will see the output like this

clip_image002

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation