3

Could somebody give a JavaScript CSOM example of how to move/copy a document set from one document library to another? The document set has a derived content type with a list of metadata, and it contains files in subfolders.

1

You can use below snippet to get the document set

Check if a document set exists

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;
}
}

Once you have that, you can create/move using

Create a document set with JSOM

function createDocumentSet(siteUrl,listTitle,docSetName,success,error) {
var ctx = new SP.ClientContext(siteUrl);
var web = ctx.get_web();
var list = web.get_lists().getByTitle(listTitle);
ctx.load(list);

var parentFolder = list.get_rootFolder();
ctx.load(parentFolder);

var docSetContentTypeID = "0x0120D520";
var docSetContentType = ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);
ctx.load(docSetContentType);

ctx.executeQueryAsync(function (){ 
    SP.DocumentSet.DocumentSet.create(ctx, parentFolder, docSetName, docSetContentType.get_id());
    ctx.executeQueryAsync(success,error);
}, 
error);
}
  • Thank you very much. This gives me some idea of how to handle document sets by CSOM JavaScript. The crucial point, however, is the move operation, which is not clear to me from your example. The move operation is to be called through a ribbon button. The user selects a list of document set items in a document library, then hits a ribbon button to move the selected list of document sets from this library to another library. – Franz Kruse Dec 16 '14 at 20:27
  • As part of creating the ribbon custom action you can get the selected item.. Move is same as Create..after creating you can just delete the old one. – Amal Hashim Dec 16 '14 at 20:31
  • 1
    But creating a new document set is not the same as moving a document set. The newly created one will necessarily have a diffenent ID (thus no be the same document set). And what about the contents of the document set (files, subfolders)? – Franz Kruse Dec 16 '14 at 22:08
  • 1
    In my understanding, moving a document set means to move THE SAME document set from one position to another, with the same unique ID and with all its contents. – Franz Kruse Dec 16 '14 at 22:10

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.