Retrieve Images From SharePoint Document Library Particular Folder Using C#

Already we saw how to create a SharePoint document library with folders using vs 2015 .

What a SharePoint Document Library is,

  • A Document Library is a collection of files that you can share as documents with your team members.
  • A Document Library stores the files and you can maintain the data in various folders.
  • A SharePoint Document Library is a place on a site where you can create, collect, and update files with others.
  • You can set the permission to each Document Library for the users.
  • You can show a Document Library in web part pages as List View Web Parts.

Use the below code to retrieve the images from SP document library folder,

Step 1

Open the ascx file in your project add the below html, Make sure to refer the bootstrap files

  1. <div class="row">  
  2.     <div class="carousel slide" id="myCarousel" data-ride="carousel">  
  3.         <ul class="carousel-indicators" id="carouselIndicatordiv" runat="server"> </ul>  
  4.         <div class="carousel-inner" id="carousaldiv" runat="server"> </div> <a class="carousel-control left" href="#myCarousel" data-slide="prev" id="leftIcon" runat="server"></a> <a class="carousel-control right" href="#myCarousel" data-slide="next" id="rightIcon" runat="server"></a> </div>  
  5. </div>  

Step 2

Open the ascx.cs file in your project. On the page Load call your method like below,

Step 3

Add below code to your solution

  1. private void GetSliderImages() {  
  2.     try {  
  3.         _web = SPContext.Current.Web;  
  4.         string webUrl = _web.Url;  
  5.         using(SPSite site = new SPSite(webUrl)) {  
  6.             using(SPWeb web = site.OpenWeb()) {  
  7.                 SPList list = web.Lists[CommonConstants.List_DepartmentSliderLib];  
  8.                 if (list != null) {  
  9.                     SPListItemCollection spListItemCollection = null;  
  10.                     foreach(SPFolder subFolder in list.RootFolder.SubFolders) {  
  11.                         if (subFolder.Name.ToLower() == “folderName”) {  
  12.                             SPQuery spQuery = new SPQuery();  
  13.                             spQuery.Folder = subFolder;  
  14.                             spQuery.Query = @ "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";  
  15.                             spQuery.RowLimit = 4;  
  16.                             spListItemCollection = list.GetItems(spQuery);  
  17.                             StringBuilder stringCollection = new StringBuilder();  
  18.                             string carouselIndString = string.Empty;  
  19.                             int count = 0;  
  20.                             if (spListItemCollection.Count > 0) {  
  21.                                 foreach(SPListItem item in spListItemCollection) {  
  22.                                     string ImageUrl = Convert.ToString(item[CommonConstants.Field_EncodedAbsUrl]);  
  23.                                     string ImageName = Convert.ToString(item[CommonConstants.Field_Name]);  
  24.                                     if (count == 0) {  
  25.                                         stringCollection.AppendFormat("<div class='item active'><img src = '" + ImageUrl + "' alt = '" + ImageName + "' /></div>");  
  26.                                         carouselIndString += "<li class='active' data-slide-to='" + count + "' data-target='#myCarousel'></li>";  
  27.                                     } else {  
  28.                                         carouselIndString += "<li data-slide-to='" + count + "' data-target='#myCarousel'></li>";  
  29.                                         stringCollection.AppendFormat("<div class='item'><img src = '" + ImageUrl + "' alt = '" + ImageName + "' /></div >");  
  30.                                     }  
  31.                                     count++;  
  32.                                 }  
  33.                                 carouselIndicatordiv.InnerHtml = carouselIndString;  
  34.                                 carousaldiv.InnerHtml = stringCollection.ToString();  
  35.                                 leftIcon.InnerHtml = "<span class='glyphicon glyphicon-chevron-left'></span><span class='sr-only'>Previous</span>";  
  36.                                 rightIcon.InnerHtml = "<span class='glyphicon glyphicon-chevron-right'></span><span class='sr-only'>Next</span>";  
  37.                             }  
  38.                         }  
  39.                     }  
  40.                 } else {  
  41.                     _commonUtil.LogExceptionToULS(CommonConstants.List_DepartmentSliderLib + " - List is not available", Microsoft.SharePoint.Administration.TraceSeverity.High);  
  42.                 }  
  43.             }  
  44.         }  
  45.     } catch (Exception ex) {  
  46.         _commonUtil.LogExceptionToULS(ex.ToString(), Microsoft.SharePoint.Administration.TraceSeverity.High);  
  47.     }  
  48. }  

Finally, slider images will be shown from the particular folder.

Summary

In this article we have explored how to retrieve the images from particular folders in SharePoint document library. Happy coding!!

Ebook Download
View all
Learn
View all