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
- <div class="row">
- <div class="carousel slide" id="myCarousel" data-ride="carousel">
- <ul class="carousel-indicators" id="carouselIndicatordiv" runat="server"> </ul>
- <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>
- </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
- private void GetSliderImages() {
- try {
- _web = SPContext.Current.Web;
- string webUrl = _web.Url;
- using(SPSite site = new SPSite(webUrl)) {
- using(SPWeb web = site.OpenWeb()) {
- SPList list = web.Lists[CommonConstants.List_DepartmentSliderLib];
- if (list != null) {
- SPListItemCollection spListItemCollection = null;
- foreach(SPFolder subFolder in list.RootFolder.SubFolders) {
- if (subFolder.Name.ToLower() == “folderName”) {
- SPQuery spQuery = new SPQuery();
- spQuery.Folder = subFolder;
- spQuery.Query = @ "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";
- spQuery.RowLimit = 4;
- spListItemCollection = list.GetItems(spQuery);
- StringBuilder stringCollection = new StringBuilder();
- string carouselIndString = string.Empty;
- int count = 0;
- if (spListItemCollection.Count > 0) {
- foreach(SPListItem item in spListItemCollection) {
- string ImageUrl = Convert.ToString(item[CommonConstants.Field_EncodedAbsUrl]);
- string ImageName = Convert.ToString(item[CommonConstants.Field_Name]);
- if (count == 0) {
- stringCollection.AppendFormat("<div class='item active'><img src = '" + ImageUrl + "' alt = '" + ImageName + "' /></div>");
- carouselIndString += "<li class='active' data-slide-to='" + count + "' data-target='#myCarousel'></li>";
- } else {
- carouselIndString += "<li data-slide-to='" + count + "' data-target='#myCarousel'></li>";
- stringCollection.AppendFormat("<div class='item'><img src = '" + ImageUrl + "' alt = '" + ImageName + "' /></div >");
- }
- count++;
- }
- carouselIndicatordiv.InnerHtml = carouselIndString;
- carousaldiv.InnerHtml = stringCollection.ToString();
- leftIcon.InnerHtml = "<span class='glyphicon glyphicon-chevron-left'></span><span class='sr-only'>Previous</span>";
- rightIcon.InnerHtml = "<span class='glyphicon glyphicon-chevron-right'></span><span class='sr-only'>Next</span>";
- }
- }
- }
- } else {
- _commonUtil.LogExceptionToULS(CommonConstants.List_DepartmentSliderLib + " - List is not available", Microsoft.SharePoint.Administration.TraceSeverity.High);
- }
- }
- }
- } catch (Exception ex) {
- _commonUtil.LogExceptionToULS(ex.ToString(), Microsoft.SharePoint.Administration.TraceSeverity.High);
- }
- }
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!!