1

Here's my code:

var items = [];
var imageGalleryUrl = "https://MYSITECOLLECTION/SiteCollectionImages";

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
  var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
  var oList=    clientContext.get_site().get_rootWeb().getList(imageGalleryUrl);
  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('<View><Query></Query></View>');
  var collListItem = oList.getItems(camlQuery);
  clientContext.load(collListItem, 'Include(Title, _Comments, FileRef)');

  clientContext.executeQueryAsync(function (sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
      var oListItem = listItemEnumerator.get_current();
      if ((oListItem.get_item('FileRef')).match(/.[jpg|gif|png]$/i)) {
        items.push({
          'Href':oListItem.get_item('FileRef'),
          'Title':oListItem.get_item('Title'),
          'Comments':oListItem.get_item('_Comments')
        });
      }
    }
    console.log(items);
  });
});

By passing in imageGalleryUrl, it outputs an array of image files stored in that library. It will not go looking for files inside folders.

Now suppose I change the value of imageGalleryUrl to "https://MYSITECOLLECTION/SiteCollectionImages/[Subfolder_1]/[Subfolder_2]"

What would I need to change in my code so it grabs files from inside [Subfolder_2] only? I'm having trouble detecting if the URL is the base library or if it's a folder inside it.

Obviously, I need this to work whether the URL is the actual library URL "https://MYSITECOLLECTION/SiteCollectionImages" or if it's deep like "https://MYSITECOLLECTION/SiteCollectionImages/[Subfolder_1]/[Subfolder_2]/[Subfolder_3]/[Subfolder_4]"

0

I got it working! The key lies in the field FileDirRef.

var items = [];
var imageGalleryUrl = "https://MYSITECOLLECTION/SiteCollectionImages/TestFolder";

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
  var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
  var oList=    clientContext.get_site().get_rootWeb().getList(imageGalleryUrl);
  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FileDirRef" /><Value Type="Text">'+imageGalleryUrl.replace(window.location.origin,"")+'</Value></Eq></Where></Query></View>');
  var collListItem = oList.getItems(camlQuery);
  clientContext.load(collListItem, 'Include(Title, _Comments, FileRef, FileDirRef)');

  clientContext.executeQueryAsync(function (sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
      var oListItem = listItemEnumerator.get_current();
      if ((oListItem.get_item('FileRef')).match(/.[jpg|gif|png]$/i)) {
        items.push({
          'Href':oListItem.get_item('FileRef'),
          'Title':oListItem.get_item('Title'),
          'Comments':oListItem.get_item('_Comments')
        });
      }
    }
    console.log(items);
  });
});

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.