WebDAV Search Folder Creation Protocol Command

WebDAV Search Folder Creation Protocol Command

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

You create a search folder just as you would create a standard folder using the WebDAV MKCOL Method. However, along with the MKCOL Method request, you specify an XML request body containing a value for the folder's searchrequest Field. The value of this property is set to a Structured Query Language (SQL) query used to construct the contents of the persisted search folder. Note that when setting a value for the searchrequest Field, you specify the SQL statement within a DAV: sql subelement, as in the following example:

<a:searchrequest xmlns:a="DAV:">
  <a:sql>select * from scope(...)</a:sql>
</a:searchrequest>

The following is an example of a persisted search folder WebDAV request. The XML portion has been formatted with extra spaces to aid readability and need not be formatted this way in your applications. See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.

MKCOL /folderpath/search_folder/ HTTP/1.1
Host: somedomain.example.com
Content-Length: XXX
Content-type: text/xml

<?xml version="1.0"?>
<a:propertyupdate xmlns:a="DAV:" >
    <a:set>
        <a:prop>
           <a:searchrequest>
              <a:sql>
                   Select "DAV:displayname"
                   FROM Scope('shallow traversal of
                        "/exchange/user1/inbox"')
                 </a:sql>
            </a:searchrequest>
        </a:prop>
    </a:set>
</a:propertyupdate>

This topic contains Microsoft® Visual Basic® Scripting Edition (VBScript), Microsoft C#, and Visual Basic .NET code examples.

VBScript

The following example uses the MKCOL Method to create a search folder.

Option Explicit

' Variables.
Dim strSearchFolderURI		' As String
Dim strRootURI			' As String
Dim strUserName			' As String
Dim strPassword			' As String
Dim strBody			' As String
Dim req				' As Msxml2.XMLHTTP

' Initialize variables.
strSearchFolderURI = "https://server/TestStore/TestSearchFolder/"
strRootURI = "https://server/TestStore/Folder/"
strUserName = "Domain\user"
strPassword = "!Password"

' Build the search request body.
strBody = "<?xml version=""1.0""?><a:propertyupdate xmlns:a=""DAV:"" ><a:set>"
strBody = strBody & "<a:prop><a:searchrequest><a:sql>"
strBody = strBody & "SELECT ""DAV:displayname"" "
strBody = strBody & " FROM Scope('shallow traversal of """ & strRootURI & """')"
strBody = strBody & "</a:sql></a:searchrequest></a:prop></a:set></a:propertyupdate>"

' Create the XMLHTTP request object.
Set req = CreateObject("Microsoft.xmlhttp")

' Open the request object with the MKCOL method and
' specify that it will be sent asynchronously.
req.open "MKCOL", strSearchFolderURI, False, strUserName, strPassword

' Set request headers.
req.setRequestHeader "Content-Type", "text/xml"
req.setRequestHeader "Content-Length", "" & Len(strBody)

'Send the MKCOL method request.
req.send strBody

' An error occurred on the server.
If req.status >= 500 Then
   wscript.echo "Status: " & req.status
   wscript.echo "Status text: An error occurred on the server."

' Folder successfully created.
ElseIf req.status = 207 Then
   wscript.echo "Status: " & req.status
   wscript.echo "Status text: " & req.statustext
   wscript.echo "Search folder successfully created."

Else
   wscript.echo "Status: " & req.status
   wscript.echo "Status text: " & req.statustext

End If

' Clean up.
Set req = nothing

C#

The following example uses the MKCOL Method to create a search folder.

using System;
using System.Net;
using System.Text;


namespace ExchangeSDK.Snippets.CSharp
{
   class SearchFolderCreationWebDAV
   {

      [STAThread]
      static void Main(string[] args)
      {
         // Variables.
         System.Net.HttpWebRequest Request;
         System.Net.WebResponse Response;
         System.Net.CredentialCache MyCredentialCache;
         string strRootURI = "https://server/TestStore/TestStoreFolder/";
         string strSearchFolderURI = "https://server/TestStore/SearchFolder/";
         string strUserName = "UserName";
         string strPassword = "!Password";
         string strDomain = "Domain";
         string strBody = "";
         byte[] bytes = null;
         System.IO.Stream RequestStream = null;

         try
         {
            // Build the search request body.
            strBody = "<?xml version=\"1.0\"?><a:propertyupdate xmlns:a=\"DAV:\" ><a:set>"
                    + "<a:prop><a:searchrequest><a:sql>"
                    + "SELECT \"DAV:displayname\" "
                    + " FROM Scope('shallow traversal of \"" + strRootURI + "\"')"
                    + "</a:sql></a:searchrequest></a:prop></a:set></a:propertyupdate>";

            // Create a new CredentialCache object and fill it with the network
            // credentials required to access the server.
            MyCredentialCache = new System.Net.CredentialCache();
            MyCredentialCache.Add( new System.Uri(strSearchFolderURI),
               "NTLM",
               new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
               );

            // Create the HttpWebRequest object.
            Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSearchFolderURI);

            // Add the network credentials to the request.
            Request.Credentials = MyCredentialCache;

            // Specify the MKCOL method.
            Request.Method = "MKCOL";

            // Encode the body using UTF-8.
            bytes = Encoding.UTF8.GetBytes((string)strBody);

            // Set the content header length.  This must be
            // done before writing data to the request stream.
            Request.ContentLength = bytes.Length;

            // Get a reference to the request stream.
            RequestStream = Request.GetRequestStream();

            // Write the SQL query to the request stream.
            RequestStream.Write(bytes, 0, bytes.Length);

            // Close the Stream object to release the connection
            // for further use.
            RequestStream.Close();

            // Set the content type header.
            Request.ContentType = "text/xml";

            // Send the MKCOL method request, get the
            // method response from the server.
            Response = (System.Net.HttpWebResponse)Request.GetResponse();

	    Console.WriteLine("Search Folder created at " + strSearchFolderURI + ".");

            // Close the HttpWebResponse object.
            Response.Close();

         }
         catch(Exception ex)
         {
            // Catch any exceptions. Any error codes from the MKCOL
            // method request on the server will be caught here, also.
            Console.WriteLine(ex.Message);

         }
      }
   }
}

Visual Basic .NET

The following example uses the MKCOL Method to create a search folder.

Option Explicit On
Option Strict On

Module Module1

Sub Main()

   ' Variables.
   Dim Request As System.Net.HttpWebRequest
   Dim Response As System.Net.HttpWebResponse
   Dim MyCredentialCache As System.Net.CredentialCache
   Dim strPassword As String
   Dim strDomain As String
   Dim strUserName As String
   Dim strRootURI As String
   Dim strSearchFolderURI As String
   Dim strBody As String
   Dim bytes() As Byte
   Dim RequestStream As System.IO.Stream

   Try
      ' Initialize variables.
      strUserName = "UserName"
      strPassword = "!Password"
      strDomain = "Domain"
      strRootURI = "https://server/TestStore/TestStoreFolder/"
      strSearchFolderURI = "https://server/TestStore/TestSearchFolder2/"

      ' Build the SQL query.
      strBody = "<?xml version=""1.0""?><a:propertyupdate xmlns:a=""DAV:"" ><a:set>" & _
                "<a:prop><a:searchrequest><a:sql>" & _
                "SELECT ""DAV:displayname"" " & _
                " FROM Scope('shallow traversal of """ & strRootURI & """')" & _
                "</a:sql></a:searchrequest></a:prop></a:set></a:propertyupdate>"

      ' Create a new CredentialCache object and fill it with the network
      ' credentials required to access the server.
      MyCredentialCache = New System.Net.CredentialCache
      MyCredentialCache.Add(New System.Uri(strSearchFolderURI), _
         "NTLM", _
         New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
         )

      ' Create the MKCOL HttpWebRequest object.
      Request = CType(System.Net.WebRequest.Create(strSearchFolderURI), _
                      System.Net.HttpWebRequest)

      ' Add the network credentials to the request.
      Request.Credentials = MyCredentialCache

      ' Specify the MKCOL method.
      Request.Method = "MKCOL"

      ' Encode the body using UTF-8.
      bytes = System.Text.Encoding.UTF8.GetBytes(strBody)

      ' Set the content header length.  This must be
      ' done before writing data to the request stream.
      Request.ContentLength = bytes.Length

      ' Get a reference to the request stream.
      RequestStream = Request.GetRequestStream()

      ' Write the message body to the request stream.
      RequestStream.Write(bytes, 0, bytes.Length)

      ' Close the Stream object to release the connection
      ' for further use.
      RequestStream.Close()

      ' Set the Content Type header.
      Request.ContentType = "text/xml"

      ' Send the MKCOL method request and get the
      ' response from the server.
      Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)

      Console.WriteLine("Search Folder created at " + strSearchFolderURI + ".")

      ' Close the HttpWebResponse object.
      Response.Close()

   Catch ex As Exception

     ' Catch any exceptions. Any error codes from the
     ' MKCOL method requests on the server will be caught
     ' here, also.
     Console.WriteLine(ex.Message)

   End Try

End Sub

End Module

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

This topic last updated: March 2004

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.