Click here to Skip to main content
12,554,623 members (53,631 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

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

, 6 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on querying a SharePoint Portal Server (SPPS) Index using a WebDAV request.


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.


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 instance HttpWebRequest 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);
    // 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 code MS-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(
    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 file appsettings section.

    // Send the request.
    Stream requestStream = httpWebRequest.GetRequestStream();
    requestStream.Write(queryData, 0, queryData.Length);
    // 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(). The requestStream is then closed. We retrieve the HTTP web response using the method httpWebRequest.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.

    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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Naveen Karamchetti
Architect AT&T Wi-Fi Services
United States United States
Naveen has done his Masters (M.S.) in Computer science, has started his career programming the mainframes and now has more than a decade of programming, development and design experience. Naveen has a sharp eye and keen observation skills. Naveen has worked for several companies and strived hard to build large scale business applications and bringing better solutions to the table.
Quite recently Naveen has built a fairly complex integration platform for a large bank. His hobbies include training, mentoring and research. Naveen spends his free time visiting National Parks nationwide.

Naveen has developed the BizTalk Control Center (BCC)

You may also be interested in...


Comments and Discussions

General'GetTransformObject' does not exist in the current context Pin
jgama13-Apr-06 9:18
memberjgama13-Apr-06 9:18 
AnswerRe: 'GetTransformObject' does not exist in the current context Pin
Naveen Karamchetti13-Apr-06 18:10
memberNaveen Karamchetti13-Apr-06 18:10 
here it goes... sorry for the missing code....

/// <summary>
/// Creates the XSL transform object which loads the stylesheet.
/// </summary>
/// <returns>The XSL transform object.</returns>
private XslTransform GetTransformObject()
    // Holds the cache object.
    object cacheObj
        = HttpContext.Current.Cache.Get("XSLTransformObjectCache");
    // Check the cache object for null value.
    if (cacheObj != null)
        return (XslTransform)cacheObj;
            // Creating the XSL transformation object.
            m_xslTransform = new XslTransform();
            // Gets the xslFileName.
            string xslFileName
                = ConfigurationSettings.AppSettings["XSL"];
            if (xslFileName.Trim().Length > 0)
                // Holds the Xsl file path.
                xslFileName = Server.MapPath("Transform/" + xslFileName);
            else // The default results.xsl stylesheet.
                // Holds the Xsl file path.
                xslFileName = Server.MapPath("Transform/results.xsl");
            //Create a stream reader to read the xsl file.
            StreamReader xslStream = new StreamReader
            //Load the stylesheet.
            m_xslTransform.Load(new XmlTextReader(xslStream));
                    "XSLTransformObjectCache",        // Cache object name.
                    m_xslTransform,                   // Object to be cached.
                    new CacheDependency(xslFileName), // File dependency.
                    DateTime.MaxValue,                // Disabling absolute expiration.
                    TimeSpan.FromDays(7)              // Refresh after 7 days.
            return m_xslTransform;
            return null;
/// <summary>
/// Transforms the Xml repsonse received from the SPPS using the specified
/// stylesheet.
/// </summary>
/// <param name="modifiedQueryString">The modified user query</param>
/// <param name="contentRange">The content range header</param>
/// <param name="webDavResult">The Web DAV result</param>
/// <returns>The Xml data after transformation</returns>
private string TransformXMLData(
                                    string modifiedQueryString,
                                    string contentRange,
                                    string webDavResult)
        //Create XPathDocument & load the XML data to be transformed.
        XPathDocument xPathDoc =
            new XPathDocument(new XmlTextReader(
            new StringReader(webDavResult)));
        // 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[] rangeData = contentRange.Split(';');
        string total = "";  string rows = "";
        if ( rangeData != null)
            // Holds the starting index of the string "rows".
            int rowsIndex = rangeData[0].IndexOf("rows");
            if ( rowsIndex != -1 )
                // getting the row range. The length of "rows" = 4
                rows = rangeData[0].Substring(rowsIndex + 4).Trim();
            // Getting the total no of rows.
            string[] totalString = rangeData[1].Split('=');
            total = totalString[1].Trim();
        // Create an XsltArgumentList.
        XsltArgumentList xslArgs = new XsltArgumentList();
        // name, namespace, value
        xslArgs.AddParam("Rows", "", rows); // Attribute
        xslArgs.AddParam("Total", "", total); // Attribute
        xslArgs.AddParam("PrevQuery", "", modifiedQueryString);
        StringWriter resultWriter = new StringWriter();
        XmlTextWriter transformXml = new XmlTextWriter(resultWriter);
        if (m_xslTransform != null)
            // Transform the data and send the output to the string writer.
            m_xslTransform.Transform(xPathDoc, xslArgs, transformXml);
            throw new Exception("Stylesheet not loaded, XSL transformation object is null.");
        return resultWriter.ToString();
    catch(Exception exception)
        throw new Exception(exception.Message);

GeneralRe: 'GetTransformObject' does not exist in the current context Pin
jgama19-Apr-06 11:28
memberjgama19-Apr-06 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 6 Jun 2013
Article Copyright 2005 by Naveen Karamchetti
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid