1

I posted this to Stack Exchange as well, and I was wondering if some of my fellow SharePoint developers could help me out. This is not a SharePoint direct question, but I am building a site collection feature with a ribbon button. Here is my x-post:

I have created a feature on SharePoint to export selected items from a view to Excel. The code I have works when the alerts are present. I know I have an asynchronous problem within my function and I need some help figuring it out. I do not quite understand how to use the $.Deferred() methods, and when I try, I get errors. Most likely because I am doing it wrong. Here is my code, all works and populates fine when I activate my alerts:

function get_Data() {

var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
var i;



 context = GetCurrentCtx();
 list = context.ListTitle;
 view = context.view;

 viewFields(list, view);

    //for (i in items){

     for (i=0; i<items.length; i++){
        myItems = items[i].id;
        //alert(myItems);       

        $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: list,
                viewName: view,
                CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + myItems + "</Value></Eq></Where></Query>",


                completefunc: function(xdata, status){

                //alert(xdata.responseXML.xml);
                    $(xdata.responseXML).SPFilterNode("z:row").each(function() {

                            var xitem = $(this);

                            for (var z = 0; z < values.length; z++){                            
                            var variables = xitem.attr(""+ values[z] +"");
                            //alert(variables);     
                            dataItems.push("<td>" + variables + "</td>");           
                            }
                        tableRows.push("<tr>" + dataItems + "</tr>");
                        dataItems.length = 0;
                        //alert(tableRows);

                    });
                }
        });     


                    td = tableRows.toString();
                    td = td.replace(/,/g, "");

                    //alert(td);    
    }


    var th = headers.toString();
    th = th.replace(/,/g, "");


    //alert(th);


hidden = "<div class='my_hidden'><table class='hiddenTable' id='hiddenTable'><tbody><tr>" + th + "</tr>" + td + "</tbody></table></div>"    
$("#aspnetForm").append(hidden);

 success();

}

I need the success(); function to run after all to the nested loops have completed. It is firing before anything gets started it seems. The alerts keep it form skipping over the loops and .each functions. How do I get the loops to finish before I run the success(); function? This is my last piece and I am done with this code. Any help would be greatly appreciated. Thanks!

  • What error are you getting when you run without alerts()? There are a few variables above that are global... like 'values', 'dataItems', 'headers'... where are they defined and to what? (values seems to be an array of columns? – Paul T. Dec 27 '14 at 21:12
  • Call success after the function $(xdata.responseXML).SPFilterNode("z:row").each(function(). – XristosK Dec 31 '14 at 1:50
1

This behavior is due to the fact that the spservices call is asynchronous. Can you set the property async: false

Place it like this in your spservices call

async: false, 

completefunc: function (xData, Status) {

This will ensure that your JavaScript will wait for the XML to return from your spservices call before it moves forward. Try this and let me know if it worked.

0

I had a similar issue in my JQuery too and the only workaround which I found was that to have a placeholder which is already available on the page. Most probably you might be adding the script on a content editor source. Please try to add another div which is already present on the page i.e. in an empty place where you can add the content, add the div. Example: see the image below: enter image description here

If you see try adding the div with a unique id on that content area highlighted in the image. that's how my jquery worked. Forgot to mention that add the content whatever you are trying to add to this div by $('divUniqueId').html(yourcontentdiv);

Hope this helps and please consider this as a workaround and it worked for me.

  • Thank you for the suggestion, but I am not going to be able to put a CEWP on all of the list which have this feature. My jQuery is sitting in the _layouts folder and it needs to stay there. I have no problem getting the information into my append table (with the alerts). The loops are being skipped by the code. Thank you for the thought, I will look into it. – lazoDev Dec 27 '14 at 19:39

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.