0

I've a picture Library which contains to folders i've retrieved them by this code

getListItems('myList');
function getListItems(listName) {
var clientContext = SP.ClientContext.get_current();
var lists = clientContext.get_web().get_lists();
var oList = clientContext.get_web().get_lists().getByTitle(listName);
var collListItem;

var camlQuery = new SP.CamlQuery();
var viewXML = "<View><ViewFields>" +
    "<FieldRef Name='Title' />" +
    "<FieldRef Name='Modified' />" +
    "<FieldRef Name='Created' />"+
"</ViewFields></View>";

collListItem = oList.getItems(camlQuery);
clientContext.load(oList);
clientContext.load(collListItem);

clientContext.executeQueryAsync(
    function () {
        // We have the elements
        var listItemEnumerator = collListItem.getEnumerator();
         // It's easy to iterate and get the column values of an item
        while (listItemEnumerator.moveNext()) {
            var cItem = listItemEnumerator.get_current();
            console.log('Id: ' + cItem.get_id() + ', Title: ' +cItem.get_item('Title')); 

        }

        // Run callback function if needed
        //if (onSuccess) onSuccess(collListItem);
    },
    function (sender, args) {
        console.log('Error getting list items: ' + args.get_message());
        }
    );
 }

How Can i retrieve all files inside of each folder ? im working on a project and i didnt work before with Jsom i'll be thankful for Help Thanks

0

Since you are not using any where clause in the query you can also use SP.CamlQuery.createAllItemsQuery() to get all items from the list.

This method creates a Collaborative Application Markup Language (CAML) string that can be used to recursively get all of the items in a list, including the items in the subfolders.

Moreover, you can include the columns that you need in the load method like clientContext.load(collListItem, 'Include(Title, Id)'). This way only the fields that are needed will be returned hence improving the performance. So, the code to query list will look like this:

var camlQuery = SP.CamlQuery.createAllItemsQuery();
collListItem = oList.getItems(camlQuery);

However, if you still want to use the view, then you need to set the viewXml of CamlQuery object like:

var camlQuery = new SP.CamlQuery();
 var viewXML = "<View Scope='RecursiveAll'><ViewFields>" +
"<FieldRef Name='Title' />" +
"<FieldRef Name='Modified' />" +
"<FieldRef Name='Created' />"+
"</ViewFields></View>";
 camlQuery.set_viewXml(viewXML)
| improve this answer | |
  • Im still getting Id: 1, Title: Gallery01 Id: 2, Title: Gallery02 as output Gallery01 and 02 is the folders inside the list i wanna know how to get the items inside them – Ahmed Aborady Jan 29 '15 at 14:56
  • I Got all Files inside of both of them and thats Good What if i want to get Just the items thats in Gallery01 only ? – Ahmed Aborady Jan 29 '15 at 15:00
  • In that case you may want to set the folderServerRelativeUrl of the camlQuery object. See my blog post. This may be helpful: sharepointnadeem.com/2014/11/… – Nadeem Yousuf-AIS Jan 29 '15 at 15:04
  • Thanks very Much Nadeem your Blog was the best Help pastebin.com/kh1S7dB0 Heres the code that worked with me – Ahmed Aborady Jan 29 '15 at 15:26
  • I think You 'll be the best help for me for couple of days :D my project almost 10 days to the deadline – Ahmed Aborady Jan 29 '15 at 15:28
0

You can use View Scope="RecursiveAll"

var viewXML = "<View Scope='RecursiveAll'><ViewFields>" +
"<FieldRef Name='Title' />" +
"<FieldRef Name='Modified' />" +
"<FieldRef Name='Created' />"+
"</ViewFields></View>";
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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