CAML查询以获取特定行

时间:2018-03-01 11:22:13

标签: excel-vba soap sharepoint vba excel

我正在使用Excel VBA使用SOAP请求获取SharePoint列表。

我可以获取所有记录。

如何获取具有特定卷号的名称?

以下是Excel VBA代码段。

Public Function retrieveSharepoint()
Dim currentId As String, ACP As String, DL As String
Dim returnedRow As Object
Dim setNode As Object
Dim sURL As String
    Dim sEnv As String
    Dim xmlhtp As New MSXML2.XMLHTTP60
    Dim xmlDoc As New DOMDocument60
    sURL = "https://sites.com/sites/project/project_ext/_vti_bin/lists.asmx"
    sEnv = "<?xml version=""1.0"" encoding=""utf-8""?>"
    sEnv = sEnv & "<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://schemas.xmlsoap.org/soap/envelope/"">"
    sEnv = sEnv & "  <soap12:Body>"
    sEnv = sEnv & "    <GetListItems xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">"
    sEnv = sEnv & "         <listName>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx}</listName>"
    sEnv = sEnv & "    </GetListItems>"
    sEnv = sEnv & "  </soap12:Body>"
    sEnv = sEnv & "</soap12:Envelope>"

    With xmlhtp
        .Open "post", sURL, False
        .setRequestHeader "Content-Type", "text/xml; charset=utf-8"
        .setRequestHeader "soapAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems"
        .send sEnv
        xmlDoc.LoadXML (.responseText)
        For Each returnedRow In .responseXML.getElementsByTagName("z:row")
            currentId = returnedRow.getAttribute("ows_Roll_x0020_Number")
            ACP = returnedRow.getAttribute("ows_Name")
            DL = returnedRow.getAttribute("ows_Last_x0020_Name")
        Next
        MsgBox .responseText
    End With

End Function

目前我可以获取所有记录。

1 个答案:

答案 0 :(得分:0)

只需使用Query元素:

/编辑

好的,我已经与Postman核实了,例如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soapenv:Body>
        <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
            <listName>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx}</listName>
            <query>
                <Query>
                    <Where>
                        <Eq>
                            <FieldRef Name='ID' />
                            <Value Type='Number'>260</Value>
                        </Eq>
                    </Where>
                </Query>
            </query>
        </GetListItems>
    </soapenv:Body>
</soapenv:Envelope>

因此,您的代码应如下所示:

sEnv = sEnv & "         <listName>{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx}</listName>"
sEnv = sEnv & "         <query><Query><Where><Eq><FieldRef Name='Roll_x0020_Number' /><Value Type='Number'>#YOUR NUMBER#</Value></Eq></Where></Query></query>"

其他参考:Lists.GetListItems