Querying a SharePoint Portal Server (SPPS) Index using a WebDAV request






3.19/5 (13 votes)
An article on querying a SharePoint Portal Server (SPPS) Index using a WebDAV request.
Introduction
This article describes the format of WebDAV (Web Distributed Authoring and Versioning) request, and how it can be used to query a SharePoint Portal Server (SPPS) index.
Background
The SharePoint Portal Server uses the Microsoft Index Server to index various documents so that they can be made searchable. An index enables easier search of a document. The SPPS indexes various documents based on the various attributes of the document and its content, e.g., the document type (*.doc), and some keywords used within the content of the document.
A WebDAV request can be used to talk to an index created by SharePoint, and retrieve all the matching documents based on the keywords provided as a search criteria.
Using the code
Step-by-step approach of "How to send a WebDAV request to the SharePoint Portal Server (SPPS)".
- Create a SharePoint Portal server workspace. Please refer to the SharePoint documentation for the meaning of a workspace.
- Create a web request instance...
// The workspaceUrl is of the form // http://server_name/workspace_name HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(workspaceUrl);
The reference variable
httpWebRequest
now holds the instanceHttpWebRequest
object.// Setting the HTTP Protocol Version to '1.0' httpWebRequest.ProtocolVersion = HttpVersion.Version10; // Set the content type. httpWebRequest.ContentType = "text/xml"; // Set the method name. httpWebRequest.Method = "SEARCH"; // Form the required header collection. WebHeaderCollection whc = new WebHeaderCollection(); whc.Add("MS-SEARCH-MAXROWS", maxRows); whc.Add("MS-Search-UseContentIndex","t"); whc.Add("MS-Search-TotalHits","t"); // Set the headers for the web request. httpWebRequest.Headers = whc;
The code snippet listed above is fairly self explanatory. Let me quickly guide you through it. The protocol version is being set in the first line, followed by the content type and the
METHOD
verb. The keys in the codeMS-SEARCH-MAXROWS
is reserved and used by the SharePoint portal server to send extra information.// The body of the HTTP request will have the Web DAV request. byte[] queryData = Encoding.UTF8.GetBytes(webDavRequest); // Set the Content Length httpWebRequest.ContentLength = queryData.Length; // Network Credentials. NetworkCredential nwkCredential = new NetworkCredential( ConfigurationSettings.AppSettings["SPPSUserName"], ConfigurationSettings.AppSettings["SPPSPassword"], ConfigurationSettings.AppSettings["SPPSDomain"]); httpWebRequest.Credentials = nwkCredential;
In the above code snippet, we are encoding a WebDAV Request using UTF-8 standard and storing it into a byte array. The content length is then set using the byte-array's
Length
property. The network credentials are then picked up from the web.config fileappsettings
section.// Send the request. Stream requestStream = httpWebRequest.GetRequestStream(); requestStream.Write(queryData, 0, queryData.Length); requestStream.Close(); // Get the HTTP Web Response. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
In the above code snippet, we are sending the web request using the method
httpWebRequest.GetRequestStream()
. TherequestStream
is then closed. We retrieve the HTTP web response using the methodhttpWebRequest.GetResponse()
.// The 'Content-Range' name-value pair will be of the form // Content-Range: rows 0-63; total=64 OR // Content-Range: rows 0--1; total=0 string contentRange = httpWebResponse.Headers["Content-Range"]; // Get the repsonse stream. responseStream = httpWebResponse.GetResponseStream(); // Pipes the stream to a higher level stream reader //with the required encoding format. StreamReader readerRespStream = new StreamReader(responseStream, Encoding.UTF8); string responseStreamData = readerRespStream.ReadToEnd(); // Releases the resources of the response. readerRespStream.Close();
The
Content-Range
header is the most important and vital header in the SharePoint Portal Server. The format of the Content-Range header is described below:- Content-Range: rows 0-63; total=64: This means that there are a total of 64 rows required, starting from 0th to the 63rd row.
- Content-Range: rows 0--1; total=0: There is no result in this case. This actually means 0th to -1st row. The '-1' will be become 0--1.
- The next step is to get the response stream, extract it to a
string
object, and then close the response stream.
Points of interest
That's all there is to know how to send a WebDAV request to a SharePoint portal server. There is a lot more to know about how to create a WebDAV query, this will be discussed in my next article.