4

I'm trying to determine whether a document set exists - if it doesn't I'll send it off to another function to create but I'm not getting the correct value back from my query.

I think it has to do with the document set behaving more like a folder than a list but I'm unsure how I'd go about a similar query to a folder.

function checkIfDocSetExists() {
    var docSetExists = false;
    var listItems;

    function checkForDocSet() {
        var clientContext = new SP.ClientContext();
        var targetList = clientContext.get_web().get_lists().getByTitle('Source Files');
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + thisItemID + "</Value></Contains></Where></Query></View>");
        listItems = targetList.getItems(query);
        clientContext.load(listItems);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {
        var listEnumerator = listItems.getEnumerator();
        while (listEnumerator.moveNext()) {
            console.log("Item found! \nTitle: " + listEnumerator.get_current().get_item("Title"));
        }
        docSetExists = true;
    }

    function onQueryFailed(sender, args) {
        console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
        docSetExists = false;
    }
    checkForDocSet();
    (docSetExists == false) ? CreateDocumentSet() : false;

}

1 Answer 1

4

You could try the following method to determine whether a document set exists.

How to find a Document Set via JSOM

function getDocSet(listTitle,docSetName, success,error) {
    var context = SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle(listTitle);
    var items = list.getItems(createGetDocSetQuery(docSetName));
    context.load(items);
    context.executeQueryAsync(
      function() {
         var docSetItem = (items.get_count() > 0 ? items.getItemAtIndex(0) : null);
         success(docSetItem);
      },
      error 
    );


    function createGetDocSetQuery(docSetName)
    {
        var query = new SP.CamlQuery();
        var viewXml =
        "<View>" +
        "<Query>" +
           "<Where>" +
               "<And>" +
                   "<Eq>" +
                      "<FieldRef Name=\"FSObjType\"/>" +
                      "<Value Type=\"Integer\">1</Value>" +
                   "</Eq>" +
                   "<Eq>" +
                      "<FieldRef Name=\"FileLeafRef\" />" +
                      "<Value Type=\"Text\">" + docSetName + "</Value>" +
                   "</Eq>" +
               "</And>" +
           "</Where>" +
        "</Query>" +
        "<RowLimit>1</RowLimit>" +
        "</View>";
        query.set_viewXml(viewXml);
        return query;
    }
}

Usage

getDocSet('Documents','Orders',printDocSetInfo,logError);

function printDocSetInfo(item)
{
   if(item == null) {
      console.log('Doc Set not found');
   }   
   else {
      console.log('Doc Set ' + item.get_item('Title') + ' was found');    
   }
}

function logError(sender,args)
{
    console.log(args.get_message());
}
2
  • 1
    This is perfect. It's a very different approach than I was trying - thanks so much.
    – Aaron
    Commented Sep 9, 2014 at 19:21
  • Aaron, glad this helped you :) Commented Sep 9, 2014 at 19:23

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.