2

Currently I am working on sort of a JavaScript function library for all things SharePoint to pass around to some of the SP developers at my work. The idea is to just take some simple features I see being repeated enough and stuffing the Ajax call into a single well documented function for ease of use. The problem is, this stuff is ALL new to me and my boss doesn't know that. <:( Here is an example of one I wrote up from my "ListHelper.js".

function getValueByID(field, refID)
{
if(setReady())
{   
    var ajaxCall = $.getJSON(SiteURL+"_vti_bin/listdata.svc/"+currentList);
    ajaxCall.success(function (jsonData)
    {
        var data = jsonData.d.results;
        for(i = 0; i < data.length; i++)
        {
            if(data[i].Id == refID)
            {
                console.log(data[i][field]);
                return data[i][field];
                break;
            }
        }
    });
    ajaxCall.error(function () { alert("GetValueByTitle Failed"); });
}
}

setReady() just returns a boolean based on if SiteURL and currentList have been set properly.

Anyway the logic here works fine. (As in I can see the proper value in console) But it takes like a second to process and its a pain because I haven't been able to make JavaScript WAIT until it actually returns a value before continuing on after the function call. So if i try to call this function in a separate .html file I often get 'undefined' by the time i need to use it. So I have been trying to think of ways to speed it up. (It seems like there's GOTTA be a faster way to do what I'm trying to do anyways) Here's what I came up with by poking around on the web.

//TODO figure out why this filtering system isn't working!
function getValueByID(field, refID)
{
if(setReady())
{   
    var url = SiteURL+"_vti_bin/listdata.svc/"+currentList+"?filter=Id eq "+refID;
    console.log(url);
    var ajaxCall = $.getJSON(url);
    ajaxCall.success(function (jsonData)
    {
        var data = jsonData.d.results
        console.log(data[0][field]);
        return data[0][field];
    });
    ajaxCall.error(function () { alert("GetValueByTitle Failed"); });
}
}

In this case the filtering doesn't work at all. It always just returns the first item in the list like it would if i removed the "?filter=Id eq #" all together. Any ideas on why this filtering thing isn't working? Or even.. if I am approaching this wrong, maybe a point in the right direction of what i SHOULD be doing? Any help is greatly appreciated. Thanks!

1 Answer 1

0

For filtering you are missing a $ in your URL:

var url = SiteURL+"_vti_bin/listdata.svc/"+currentList+"?$filter=Id eq '"+refID + "'";

But, to get item by ID with ListData.svc you should provide an URL like this:

http://thesiteUrl/_vti_bin/listdata.svc/listName(1)

Where listName is the name of the list, and 1 is the ID of the item to retrieve, so in your case something like this:

var ajaxCall = $.getJSON(SiteURL + "_vti_bin/listdata.svc/" + currentList + "(" + refID + ")");

On a side note regarding this type of timing issues (that will happen all the time in an asynchronous language like javascript), you should work on providing a callback so that when a user calls the getValueById method they can pass along the function that should be executed when your call is completed:

function getValueByID(field, refID, callback)
{
if(setReady())
{   
    var ajaxCall = $.getJSON(SiteURL + "_vti_bin/listdata.svc/" + currentList + "(" + refID + ")");
    ajaxCall.success(function (jsonData)
    {            
        if(typeof(callback) !== "undefined"){ // Here we execute the method passed in
            callback(jsonData);
        }
    });
    ajaxCall.error(function () { alert("GetValueByTitle Failed"); });
}
}

So you can call it like this

function MyCallback(jsonData){
    // Do stuff here
}

getValueByID("The field", "The ref", MyCallback);

Or, take a look at the jQuery.Deferred pattern outlined here, that gives you the ability to do for example getValueByID("The field", "The ref").then(function(){ //Do stuff}});

4
  • Oh awesome. I will look into Callbacks. Thanks you. However in regards to the Filtering. Not all of my functions are just looking for ID so it would be nice to be able to do something like "?filter=Phone eq 5555555555" if for some reason you wanted to look some one up by phone number. Any ideas on why the filtering isn't working?
    – Riggy
    Commented Jul 14, 2015 at 13:48
  • 2010. I have not been able to get the filtering working. It simply returns a JSON object with all the fields just as if I hadn't included any filtering. As far as I can tell my syntax is correct. I will mark solved once I have figured out the filtering issue.
    – Riggy
    Commented Jul 14, 2015 at 14:02
  • SOLVED: I forgot to add a "$" between "?" and "filter". Filtering is working now. Thanks for the help with the other issues!
    – Riggy
    Commented Jul 14, 2015 at 14:51
  • Oops. One additional edit. The syntax requires single quotes around the latter variable. Like so. "?$filter=Id eq '"+refID+"'"; Hard to see but refID is surrounded by ' '
    – Riggy
    Commented Jul 14, 2015 at 15:22

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.