Share via


Searching with XMLHTTP

This example demonstrates how to use the Microsoft SeverXMLHTTP object to search documents stored in a SharePoint Portal Server workspace.

' Constants for HttpStatus property.
Const HTTPSTATUS_OK = 200
Const HTTPSTATUS_CREATED = 201
Const HTTPSTATUS_ACCEPTED = 202
Const HTTPSTATUS_MULTISTATUS = 207
Const HTTPSTATUS_BADREQUEST = 400
Const HTTPSTATUS_UNAUTHORIZED = 401
Const HTTPSTATUS_FORBIDDEN = 403
Const HTTPSTATUS_NOTFOUND = 404
Const HTTPSTATUS_INTERNALSERVERERROR = 500

' Holds the Microsoft.XMLHTTP object used to perform the query
Dim m_xh as Object
' Stores the HTTP command specified in the request. 
' This parameter is case sensitive
Dim m_strHttpCommand as String
' Stores the URL that was specified in the request
Dim m_strRequestUrl as String
' Range of result rows in DAV query headers
Dim m_iRequestRangeStart as Integer
Dim m_iRequestRangeEnd as Integer
' Offset of the first row in the result set
Dim m_iResponseRangeStart as Integer
' Offset of the last row in the result set
Dim m_iResponseRangeEnd as Integer
' Total number of rows returned from the query. 0 means none
Dim m_cTotalHits as Integer
' Flag that indicates if there are any remaining rows
Dim m_fHasMore as Boolean
' Flag that indicates the query is performed Asynchronously
Dim m_fIsAsync as Boolean

Dim strRequest
strRequest = _
    "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCRLF & _
    "<a:searchrequest xmlns:a=""DAV:"">" & vbCRLF & _
    "  <a:sql>" & vbCRLF & _
    "select ""DAV:href"" from scope('deep traversal of ""/myworkspace/documents""') where freetext('word doc')" & vbCRLF & _
    "  </a:sql>" & vbCRLF & _
    "</a:searchrequest>"

' Instantiate an XMLHTTP object used to perform query.
Set m_xh = CreateObject("Msxml2.ServerXMLHTTP.3.0")

' Start the query. Mode is controlled by m_fIsAsync
m_xh.Open "SEARCH", "http://myserver/myworkspace", FALSE, userid, password
m_xh.SetRequestHeader "content-type", "text/xml"

' Specify range in request head
m_xh.SetRequestHeader "Range", "rows=0-19"
m_xh.SetRequestHeader "MS-Search-TotalHits", "t"
m_xh.SetRequestHeader "MS-Search-UseContentIndex", "t"
m_xh.SetRequestHeader "MS-SEARCH-MAXROWS", "200"
m_xh.Send strRequest

' Confirm HTTP request succeeded.
If (m_xh.Status = HTTPSTATUS_OK) or (m_xh.Status = HTTPSTATUS_MULTISTATUS) then
    ' Return the response string back to the caller.
    Msgbox m_xh.ResponseText
end if

Important

When developing server-side applications with XMLHTTP, the ServerXMLHTTP object must be used. Use of the XMLHTTP object jeopardizes the stability of your server.

Searching with ADO

SharePoint Portal Server Search SQL Syntax

WebDAV/DASL Request and Response Syntax Syntax

See Also

Using the WebDAV Protocol

Microsoft XML SDK