BIll
BIll

Reputation: 51

CAML Query to retrieve list items Retrieves more data than expected

I'm attempting to learn the basics of CAML queries in SharePoint. For various non-technical reasons, I'm presently limited to using VBSCRIPT to send my requests to the site I wish to query. I am able to connect to the site and retrieve data, but all rows and all columns from the target list are being returned vs. the specific data requested. Any ideas as to what I've done wrong and how to go about fixing the issue would be appreciated. Here's the code:

dim soapEnv
dim oHttp
dim URL

set oHttp = CreateObject("Microsoft.XmlHttp")

URL     = "https://[MyCorporateSite]/_vti_bin/Lists.asmx?op=GetListItems"
soapEnv = "<?xml version='1.0' encoding='utf-8'?>" _
        & "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" _
        &    "<soap:Body>" _
        &       "<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" _    
        &          "<listName>{3C786148-E3E1-425B-84BE-6E44335D5645}</listName>" _
        &          "<Query>" _
        &             "<Where>" _
        &                "<Eq>" _
        &                   "<FieldRef Name='ApplicationID' />" _
        &                   "<Value Type='Text'>5Z4PWS</Value>" _
        &                 "</Eq>" _
        &             "</Where>" _
        &          "</Query>" _
        &          "<ViewFields xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" _
        &             "<FieldRef Name='ApplicationID' />" _
        &             "<FieldRef Name='ApplicationName' />" _
        &          "</ViewFields>" _
        &          "<RowLimit>1</RowLimit>" _
        &       "</GetListItems>" _
        &    "</soap:Body>" _
        & "</soap:Envelope>"

oHttp.Open "POST", URL, False, [MySiteUID], [MySitePWD]
oHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
oHttp.send soapEnv
WScript.Echo oHttp.responseText

I know I should assign the result to an XML document eventually vs. echoing return text, but I want to get the query to work properly before worrying about the presentation.

Also, downloading a CAML creation/formatting tool is not possible due to corporate restrictions.

Upvotes: 0

Views: 1776

Answers (1)

BIll
BIll

Reputation: 51

Found it. Here's the revised code:

dim soapEnv
dim oHttp
dim URL

set oHttp = CreateObject("Microsoft.XmlHttp")

URL     = "https://[MyCorporateSite]/_vti_bin/Lists.asmx?op=GetListItems"
soapEnv = "<?xml version='1.0' encoding='utf-8'?>" _
        & "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" _
        &    "<soap:Body>" _
        &       "<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" _
        &          "<listName>{3C786148-E3E1-425B-84BE-6E44335D5645}</listName>" _
        &          "<query>" _
        &             "<Query>" _
        &                "<Where>" _
        &                   "<Eq>" _
        &                      "<FieldRef Name='ApplicationID' />" _
        &                      "<Value Type='Text'>5Z4PWS</Value>" _
        &                    "</Eq>" _
        &                "</Where>" _
        &             "</Query>" _
        &          "</query>" _
        &          "<ViewFields>" _
        &             "<ViewFields xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" _
        &                "<FieldRef Name='ApplicationID' />" _
        &                "<FieldRef Name='ApplicationName' />" _
        &             "</ViewFields>" _
        &         "</ViewFields>" _
        &       "</GetListItems>" _
        &    "</soap:Body>" _
        & "</soap:Envelope>"

oHttp.Open "POST", URL, False, [MySiteUID], [MySitePWD]
oHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
oHttp.send soapEnv
WScript.Echo oHttp.responseText

Note the additional Query and ViewFields tags.

Upvotes: 0

Related Questions