0

I work on a script to recover information in a list and create a pdf with it. To create my PDF I use makePDF and JSOM to recover the information. I've already created my pdf and recover my data, but my function doesn't work with many list's items.

var id;
function pdfCtrl($scope){
$scope.form = { id:0};
var allItems;
var listeDef = new Array();

onQueryFailed = function(sender, args) {
    console.log('Request failed ' + args.get_message());
}

$scope.downloadPdf = function($event){
    var x = $scope.form;
    $event.preventDefault();
    var clientContext = SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var list = web.get_lists().getByTitle("Congés");
    var query = SP.CamlQuery.createAllItemsQuery();
    allItems = list.getItems(query);
    clientContext.load(allItems);
    id = x.id;
    clientContext.executeQueryAsync(
        Function.createDelegate(this, onQuerySucceededDown2), 
        Function.createDelegate(this, onQueryFailed) 
        );      
};

onQuerySucceededDown = function(){
    var listEnumerator = allItems.getEnumerator();  
    while (listEnumerator.moveNext()){
        var currentItem = listEnumerator.get_current();
        if (currentItem.get_id() == id){
            pdfMake.createPdf(genratepdfDefinition(currentItem)).download('optionalName.pdf'); }
        //else if (id == 0){
            //pdfMake.createPdf(genratepdfDefinition(currentItem)).print();
            //pdfMake.createPdf(definition).download(); 
//      }
        else {

        }
    }
}

}

I search the problem and I found the function always use the same "current item". So I want to wait for the end of the creation of my pdf and after that create the next. If somebody can help me, I will love him! Thanks!

3 Answers 3

0

Check with the vendor of that pdfMake API.

You can achieve it if it has a Callback function or returns a Promise

0

Put the createPdf functionality into a function, and call the function in the loop, passing the currentItem. This way currentItem is scoped to the function, and will not update as the currentItem in the onQuerySucceededDown function gets updated.

function createPdf(item) {
    if (item.get_id() == id){
        pdfMake.createPdf(genratepdfDefinition(item)).download('optionalName.pdf'); }
    //else if (id == 0){
        //pdfMake.createPdf(genratepdfDefinition(item)).print();
        //pdfMake.createPdf(definition).download(); 
    //}
    else {

    }
}

var onQuerySucceededDown = function(){
    var listEnumerator = allItems.getEnumerator();  
    while (listEnumerator.moveNext()){
        createPdf(listEnumerator.get_current());
    }
}

To help understand variable scopes, try the following in Dev tools:

for (var i = 0; i < 5; i++) {
    setTimeout(function() {
        console.log(i);
    }, 1000);
}
// Should see 5 outputted five times

var test = function(i) {
    setTimeout(function() {
        console.log(i);
    }, 1000);
};
for(var i = 0; i < 5; i++) {
    test(i);
}
// Should see 0, 1, 2, 3, 4 outputted
3
  • this solution work when i want to create a pdf with the item's. But when i want to create all the with (value 0) the problem is the same.. Jan 10, 2017 at 8:37
  • I don't understand what you're trying to do. Can you explain again?
    – wjervis
    Jan 10, 2017 at 12:23
  • I want to create many PDF thanks to a SharePoint list. But, with the asynchronus work, the create fucntion use always the same defintion. Jan 11, 2017 at 8:21
0

I resolve my problem thanks to this code :

function createPDF(item){  
    var def;
    var thisComptePromise = ++ comptePremise;
    if (item.get_id() == id){
        pdfMake.createPdf(genratepdfDefinition(item)).download('optionalName.pdf'); }
    else if (id == 0){
        var promiseGenerateDefinition = new Promise(
                function (resolve, reject){
                    console.log('on demarre le promise ' + thisComptePromise);
                    def = genratepdfDefinition(item);
            window.setTimeout(
                    function() {
                        // On tient la promesse !
                resolve(thisComptePromise);
            }, Math.random() * 2000 + 1000);
        });
        promiseGenerateDefinition.then(
                function(){
                   pdfMake.createPdf(def).download()
                   console.log('fin premise' + thisComptePromise); 
                }).catch(
               function(){
                   console.log('promesse rompue');
               }     
                );
    }
        //pdfMake.createPdf(genratepdfDefinition(item)).download();}
        else {
        }
    }

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.