Search code examples
sharepoint-2010spservices

How can I get formated values from lookup field using SPServices - GetListItems?


When I read a lookup field from a list using SPServices - GetListItems (code below), the value returned is always on the standard ID;#Value, but I need only the value.

How can I get formated values from lookup field (only ID or only value)?

  function GetListItems(itemId) {
      var query = "<Query>" +
                "<Where>" +
                    "<Eq>" +
                        "<FieldRef Name='ID' />" +
                        "<Value Type='Counter'>" + itemId + "</Value>" +
                    "</Eq>" +
                "</Where>" +
            "</Query>";

    $().SPServices({
        operation: "GetListItems",
        async: true,
        listName: "MyList",
        CAMLRowLimit: 1,
        CAMLQuery: query,
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                alert($(this).attr("ows_LookupField"));
            });
        }
    });

Solution

  • The split method breaks a string into substrings in an array.

    alert($(this).attr("ows_LookupField").split(";#")[1]);