0

I need to copy a document to a document set using JSOM. The below code create the file in my Document library succesfully. However I cannot figure out how to go a step further to change from document library to document set.

oList = oWebsite.get_lists().getByTitle("mydocLibrary");  // LOCATION OF MY DOCMENT LIBRARY

This is my actual Doc Set location -- I need help here please!!

var documentSetFolder = "/sites/mytest/mydocLibrary/test01";  // LOCATION OF MY DOCUMENT SET

// calling fn to create a copy of the file as stored in Site Assets. createfile('http://mySPHost/sites/mytest/SiteAssets/myfile.txt');"

function createfile (url)   
{  
    var clientContext;  
    var oWebsite;  
    var oList;  
    var fileCreateInfo;  
    var fileContent;  

    clientContext = new SP.ClientContext.get_current();  
    oWebsite = clientContext.get_web();  

     var documentSetFolder = "/sites/mytest/mydocLibrary/test01";  // LOCATION OF MY DOCUMENT SET

    oList = oWebsite.get_lists().getByTitle("mydocLibrary");  // LOCATION OF MY DOCMENT LIBRARY

    fileCreateInfo = new SP.FileCreationInformation();  


    fileCreateInfo.set_url("myfile.txt");  
    fileCreateInfo.set_content(new SP.Base64EncodedByteArray());  
    fileContent = url;  // current location of file to copy from

    for (var i = 0; i < fileContent.length; i++)   
    {  
        fileCreateInfo.get_content().append(fileContent.charCodeAt(i));  
    }

// THIS NEEDS TO BE UPDATED TO POINT TO DOC SET LOCATION, BUT HOW?
    this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);  


    clientContext.load(this.newFile);  
    clientContext.executeQueryAsync( Function.createDelegate(this, successHandler), Function.createDelegate(this, errorHandler));  



    function successHandler()   
    {  
       alert("FILE CREATED!");
    }  

    function errorHandler()   
    {  
       alert("File Creation Failed: " + arguments[1].get_message());  
    }  
}  

I would appreciate your help please as stuck in this logic for long now :(

1

In the end, this was simply achieved by updating the SP.FileCreationInformation object set url to

fileCreateInfo.set_url("http://mySPServer/sites/mytest/mydocLibrary/MyDocSetName/myfile.txt");  

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.