0

I have client-side Javascript on a SharePoint page. It must change the Title of every Document present in a specific Documents Set.

That Documents Set is one of many, in a huge documents library (before you ask: still within Microsoft's recommendations). Therefore I cannot afford to iterate on every item of the Library and only then filter the ones that are in the Documents Set. The query must make the Server do the filtering.

I have found that the general consensus is that the Documents Sets should be treated as folders, and that the filtering should be done as follows :

var libTitle = "My Library";
var subFolder = "My Subfolder";

var clientContext = new SP.ClientContext();
oLibrary = clientContext.get_web().get_lists().getByTitle(libTitle);

var query = SP.CamlQuery.createAllItemsQuery(); //This creates a Caml query that takes everything...
query.set_folderServerRelativeUrl(libTitle+"/"+subFolder); //...but then this should restrict to only the targetted Documents Set

var queryResults = oLibrary.getItems(query);
var allDocs = clientContext.loadQuery(queryResults , "Include(ID, Title, ContentType, ContentTypeId, File)" );



clientContext.executeQueryAsync(
    Function.createDelegate(this, function() {
        for (var i=0; i< allDocs.length; i++) {
            console.log("Found Item with ID '"+allDocs[i].get_item('ID')+"' ('"+allDocs[i].get_item('Title')+"').");
        }       
    }),
    null
);

My issue is that query.set_folderServerRelativeUrl seems to be completely ignored... I clearly see every library item in the results (or at least, all the root folders).

What am I missing?

1

The folderServerRelativeUrl doesn't seem to be properly created. Suppose you have a site at /sites/yoursite Url containing a library named TestLib with a folder TestFolder, then the folderServerRelativeUrl will be '/sites/yoursite/TestLib/TestFolder'. If the site is the root site not under any managed path then the folderServerRelativeUrl will be '/TestLib/TestFolder'.

The other way of testing whther a folder server relative Url is correct is by using var folder = web.getFolderByServerRelativeUrl(folderServerRelativeUrl);

  • Thank you that might be the reason because I'm on a managed path. I'll test it and come back to you. That said, I'm baffled that SharePoint returns EVERYTHING instead of NOTHING when it doesn't recognize the path. – jeancallisti May 29 '18 at 10:02
  • That was the solution. Instead of just "libTitle+"/"+subFolder", I used "/MyManagedPath/MySite/"+libTitle+"/"+subFolder" and it worked. I guess for anyone else, replacing "MyManagedPath" with "sites" should do the trick. Thanks! – jeancallisti May 29 '18 at 10:54

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.