0

I'm using SharePoint 2010. I need to get some SharePoint data on a list item hyperlink click. Inside the JavaScript file there is a method getLibraryGuid() I get the targetLibrary value with no issues using the executeQueryAsync().

But there seems to be an issue with the second call. The targetLibraryGuid call does not happen on the first item click. I observed that it is retrieved on the second click attempt on the item in the list view. Am i missing any point here?

Also, in the status bar in IE i get 2 error messages saying:

1) The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly executed 2) Object doesn't support this property or method

[from the sp.runtime.debug.js]

function getLibraryGuid(libraryName) {
      //1st call
      var targetLibrary = context.get_web().get_lists().getByTitle(libraryName);
      context.load(targetLibrary);
      context.executeQueryAsync(
        function(sender, args) {

        },
        function(sender, args) {
            alert('Request Failed' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
        }, "sp.js");

        //2nd call
        targetLibraryGuid = targetLibrary.get_id().toString();
        context.load(targetLibraryGuid);
        context.executeQueryAsync(
        function(sender, args) {

        },
        function(sender, args) {
            alert('Request Failed' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
        }, "sp.js");


      alert('library guid : ' + targetLibraryGuid);          
  }
0

The reason this happens is because of the asynchronous behavior of CSOM. That is, when you make a second call to get the id of the list, at this time targetLibrary is yet to be initialized. You can make use of jQuery Deferred to avert this issue. For more information on how to use jQuery Deferred, you can follow this post.

http://sharepointobservations.wordpress.com/2013/09/04/using-jquery-deferred-object-with-sharepoint-javascript-object-model/

  • I will try this out. In the meantime i attempted the 2nd call inside the function in the executeQueryAsync() of the first call. In my case seems alright. However needs more testing to see how it works. – Shaamil Jan 3 '14 at 11:48

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.