1

I am trying to access the calendar exception for each resource, but I'm having some trouble with the nested executeQueryAsync() calls.

Here's a piece of my code:

App.js

// Get the collection of enterprise resources.
function GetResources()
{   
    // Display a message to the user to show we are reading the resources. 
    $('#spanMessage').text('Reading resources...');

    // Initialize the current client context.
    projContext = PS.ProjectContext.get_current();

    // Get the collection of enterprise resources.
    resources = projContext.get_enterpriseResources();

    // Register the request for information.
    projContext.load(resources);

    // Run the request on the server.
    projContext.executeQueryAsync(IterateThroughResources, QueryFailed);
}

function IterateThroughResources(response)
{
    // Get the enumerator and iterate through the collection.
    var enumerator = resources.getEnumerator();
    while (enumerator.moveNext()) 
    {
        var resource = enumerator.get_current();
        var resourceId = resource.get_id(); 
        var resourceName = resource.get_name();

        // Get the collection of calendar exceptions.
        var exceptions = resource.get_resourceCalendarExceptions();

        // Register the request for information that you want to run on the server.
        projContext.load(exceptions);

        projContext.executeQueryAsync(Function.createDelegate(this, function(){IterateThroughCalendarExceptions(resource, exceptions);}), QueryFailed);
    }
}

function IterateThroughCalendarExceptions(resource, exceptions)
{
    var enumerator = exceptions.getEnumerator();
    while (enumerator.moveNext()) 
    {
        var exception = enumerator.get_current();
        var name = exception.get_name();
    }
}

But the fact that it's running asynchronous it's a problem, because the IterateThroughCalendarExceptions seems to always receive the same params.

  • How to get EDR or RBS value of Resource? – Tiago Davi Jan 7 '14 at 16:49
1

The problem you're facing is a quite well-known JS inconvenience and described in detail for example here:

In brief, creating functions in loops is tricky. In your case the callback function function(){IterateThroughCalendarExceptions(resource, exceptions);} will always use resource & exceptions values from the last iteration of the loop.

To prevent that, you have to wrap your function like that:

projContext.executeQueryAsync(
  (function()
  {
     IterateThroughCalendarExceptions(resource, exceptions);
  })(resource, exceptions),
  QueryFailed);
  • Thanks Andrey. That was what I was looking for. But I was able to fix it in another way, which is what I should have done from the start. I'm loading calendar exceptions for every resource and running only one request on the server – Gonzalo Sep 6 '13 at 13:43
1

Try using an Include to eager load the exceptions:

projContext.load(resources, "Include(ID, Name, ResourceCalendarExceptions)");
  • Thanks Rob. I had to change "ID" to "Id" because I was getting the following error when calling resource.get_id(); "The property or field 'Id' has not been initialized sharepoint" – Gonzalo Sep 6 '13 at 13:41
1

I was able to fix by making a change in the code. Insted of making a request on the server for every resource, I register the request for information (.load) for every resource and then run one request on the server.

Hope this helps!

  • 1
    +1 Thanks for sharing, very good solution! I cannot believe that I haven't proposed it myself :) Got locked with this create-functions-inside-loop thing, it was one thing I had once been struggling a lot with and I noticed it from the first glance. Hopefully this still be a good hint for you for your farther JS development ;) P.S. Please mark this as an accepted answer so that people can see it is what they need if they have same problem. – Andrey Markeev Sep 6 '13 at 18:07
  • Thanks Andrey. I can't mark this as the accepted answer yet because of the few points I have. It seems I will be able to accept my own answer tomorrow. – Gonzalo Sep 6 '13 at 18:32
  • Can you post a sample code of that solution ? – Anish V Feb 4 '14 at 12:49

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.