3

I'm trying to load in a SharePoint list to a Unordered List so I can create a simple search function (the Sharepoint Search is just horrible). The code I have borrowed and adapted is below:

$(document).ready(function() {
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>Metric_Audit</listName> \
                        <viewFields> \
                            <ViewFields> \
                <FieldRef Name='ReportName' /> \
                <FieldRef Name='Metric Name' /> \
                           </ViewFields> \
                        </viewFields> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";

        $.ajax({
            url: "http://teamspace.intranet.group/sites/CSI/ID/DB/_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: Result,
            contentType: "text/xml; charset=\"utf-8\""
        });
    });


    function Result(xData, status) {
        $(xData.responseXML).find("z\\:row").each(function() {
            var liHtml = "<li>" + $(this).attr("ows_ReportName") + "</li>";
            $("#MetricsUL").append(liHtml);
        });
    }
});

<ul id="MetricsUL"/> 

It populates the list as it should but all the items have the name 'undefined'. I have tried removing spaces etc. to no avail and when I change the list to the 'tasks' list it works just fine.

Been staring at this for AGES. Any tips you can suggest would be really appreciated! I'm sure it's something small that I just can't work out!

Thanks!

Lorenzo
  • 29,081
  • 49
  • 125
  • 222
James Osborn
  • 67
  • 2
  • 7

2 Answers2

4

Have you verified you're referencing the correct Field names?

The CAML query you've defined in soapEnv requires the use of the fields' internal names, not their display names. I guarantee Metric Name is not right; it's probably something like Metric_x0020_Name or similar.

How do you determine the internal names of your fields? There's a couple of methods.

Let me introduce you to a great utility called U2U CAML Query Builder. This should be in every SharePoint developer's toolbox. Fire it up, point it at the right list (connect via SharePoint Web Service if you're not on the server), and it will allow you to build CAML queries using the display names of the fields, but generate the correct underlying CAML for you. It's also great for constructing complex filters and boolean logic, plus it lets you run the query and get immediate feedback.

Another approach to getting the field name is to navigate into the list settings, then click on the name of a field you're interested in. Check out your URL and look for the querystring parameter named &Field; it will divulge the internal name of the field. This value will be URL Encoded if you're using Internet Explorer (a simple online decoder can help decipher the value), but if you use Firefox, you'll see the value unencoded.

Plug the correct Field names into your CAML query and give it another whirl.

CBono
  • 3,803
  • 1
  • 32
  • 40
  • 1
    Definitely sounds like you are accessing thte wrong attribute name in the result handler. Check the raw results set to see what you get. I.e... Alert($(this).HTML()); – brian brinley Nov 28 '10 at 22:07
  • @brian brinley: Good call; in addition to asking for the right fields from SharePoint, you have to address them correctly in the output. Though I see the O.P. is asking for `ows_ReportName` (which isn't intuitive), so it's possible they've already examined the attributes. – CBono Nov 28 '10 at 22:24
  • Thanks a lot for this! I was unable to install the tool on my work laptop but searching for the query string in the URL worked perfectly. You're a star! – James Osborn Dec 03 '10 at 13:58
0

On your Result function, add an alert(xData.responseXML.xml); to make sure you're getting the ows_ReportName attribute.

Perhaps its name is spelled slightly differently (they're case-sensitive). Alternately, try ows_Title to see if it returns defined data. If so, you're getting the XML back, it just can't find ReportName as a valid column in the returned data.