-1

I need the code to retrieve files from folders in document library

0

3 Answers 3

3

An example of how to Get Files from a Folder using Ecmascript\Javascript client object model in SharePoint 2010

<script type=”text/ecmascript”>
function ViewAllFiles()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(‘Shared Documents’);
var query = SP.CamlQuery.createAllItemsQuery();
query.set_folderServerRelativeUrl(‘/Shared%20Documents/TestFolder’);
allItems = list.getItems(query);
context.load(allItems, ‘Include(Title, ContentType, File)’);
context.executeQueryAsync(Function.createDelegate(this, this.success),   Function.createDelegate(this, this.failed));
}

function success()
{
  var fileUrls = “”;
  var ListEnumerator = this.allItems.getEnumerator();
   while(ListEnumerator.moveNext())
   {
    var currentItem = ListEnumerator.get_current();
    var _contentType = currentItem.get_contentType();
     if(_contentType.get_name() != “Folder”)
       {
        var File = currentItem.get_file();
        if(File != null)
          {
           fileUrls += File.get_serverRelativeUrl() + ‘\n';
          }
       }
   }
  alert(fileUrls);
 }
 function failed(sender, args) {
  alert(“failed. Message:” + args.get_message());
 }
 </script>
 <a href=”#” onclick=”Javascript:ViewAllFiles();”>View All Files in Test Folder</a>

Please replace Document Library name and folder as per your requirement.

6
  • I am creating folder dynamically,then what changes i need to make.
    – sherin
    Jan 7, 2015 at 5:22
  • if you are creating folders dynamically then in that case you will have to get folder Name and relace the folder name in set_folderServerRelativeUrl() method.
    – Amit
    Jan 7, 2015 at 5:28
  • 1
    you can also follow below article by Nadeem sharepointnadeem.com in this you don't need to pass folder name. Use createAllItemsQuery to get items inside subfolders as well. Otherwise use new SP.CamlQuery() to get items from a single folder only.
    – Amit
    Jan 7, 2015 at 5:31
  • 1
    Here is link to the post: sharepointnadeem.com/2014/11/… Jan 7, 2015 at 6:13
  • context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));.This statement is giving me an error saying "Uncaught TypeError: Cannot read property 'OnSuccess' of undefined".How will i resolve this?
    – sherin
    Jan 7, 2015 at 7:05
1

I have found the solution

var hostweburl;
var appweburl;
var allItems;
//Load the required SharePoint libraries.
$(document).ready(function () {
    //Get the URI decoded URLs.
    hostweburl = decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
GetFolders(); }); function getQueryStringParameter(paramToRetrieve) { var params = document.URL.split("?")[1].split("&"); for(var i = 0; i< params.length; i = i + 1) { var singleParam = params[i].split("="); if (singleParam[0] == paramToRetrieve) return singleParam[1]; } } function GetFolders() { var clientContext = new SP.ClientContext(appweburl); var appCtxSite = new SP.AppContextSite(clientContext, hostweburl); var web = appCtxSite.get_web(); var list = web.get_lists().getByTitle('TPSDocument'); var query = SP.CamlQuery.createAllItemsQuery(); // query.set_folderServerRelativeUrl('/TPSDocuments/TestFolders'); allItems = list.getItems(query); clientContext.load(allItems, 'Include(Title, ContentType, File)'); clientContext.executeQueryAsync(success, failed); } function success() { alert("success"); var fileUrls = ""; var Filename = new Array(); var i = 0; var ListEnumerator = allItems.getEnumerator(); while (ListEnumerator.moveNext()) { var currentItem = ListEnumerator.get_current(); var _contentType = currentItem.get_contentType(); if (_contentType.get_name() != "Folder") { var File = currentItem.get_file(); if (File != null) { fileUrls += File.get_serverRelativeUrl() + '\n'; var url = fileUrls.split('/'); var folder = url[url.length - 2]; if (folder == "TestFolder") { Filename[i] = File.get_name(); i++; } } } } alert(Filename); alert(fileUrls); } function failed(sender, args) { alert("failed. Message:" + args.get_message()); }

0

How to retrieve all the images inside a folder from a document lib using JSOM

      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js "></script>

        <script type="text/javascript" language="javascript">

    $(document).ready( function () { 
            ExecuteOrDelayUntilScriptLoaded(GetFilesFromFolder, "sp.js");
            });
var allItems;
function GetFilesFromFolder(listTitle, folderServerRelativeUrl)
{
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('libraryname');
    // Use createAllItemsQuery to get items inside subfolders as well. Otherwise use new SP.CamlQuery() to get items from a single folder only
    var query = SP.CamlQuery.createAllItemsQuery();
    query.set_folderServerRelativeUrl('libraryname/foldernmae');
    allItems = list.getItems(query);
    context.load(allItems, 'Include(File, FileSystemObjectType)');
    context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
}
function OnSuccess()
{
 pictureArray = new Array();
 var pictureCount = 0;  
 var txtHTML = "";
    var listItemEnumerator = allItems.getEnumerator();
    while(listItemEnumerator.moveNext())
    {
        var currentItem = listItemEnumerator.get_current();                                                                                                                     

            var file = currentItem.get_file();
            var path = file.get_serverRelativeUrl();
            pictureArray[pictureCount++] = path;
            }
            if(file != null)
            {
                var row = document.createElement("tr");
                    var j = 0;
                    for(i=0; i<this.pictureArray.length; i++) 
                    {
                    if (j == 0)
                    txtHTML += "<tr>";
                    txtHTML += "<td>";                       
                     txtHTML += "<a href='"+ this.pictureArray[i] +"'><image border='none' src='" + this.pictureArray[i] + "'></Image></a>"                              
                     txtHTML +="</td>";
                     j++;
                     if (j == 2)
                     {
                     txtHTML += "</tr>";
                     j = 0;
                     }
                    }




            }
        $("#tblCustomListData").append(txtHTML);

}

function OnFailure(sender, args) {
    alert("Failed. Message:" + args.get_message());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.