Using SharePoint SOAP with jQuery GetListItems - Simple but can't work it out!

≯℡__Kan透↙ 提交于 2019-12-19 09:26:38

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4287384/using-sharepoint-soap-with-jquery-getlistitems-simple-but-cant-work-it-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!