Share via


Finding Folders in Exchange 2010

Last modified: May 21, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

You can use Exchange Web Services to find subfolders of an identified folder.

Example

The following example shows you how to search for all subfolders in the root folder.

static void FindFolders(ExchangeServiceBinding esb)
{
    // Create the request and specify the travesal type.
    FindFolderType findFolderRequest = new FindFolderType();
    findFolderRequest.Traversal = FolderQueryTraversalType.Deep;

    // Define the properties that are returned in the response.
    FolderResponseShapeType responseShape = new FolderResponseShapeType();
    responseShape.BaseShape = DefaultShapeNamesType.Default;
    findFolderRequest.FolderShape = responseShape;

    // Identify which folders to search.
    DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
    folderIDArray[0] = new DistinguishedFolderIdType();
    folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

    // Add the folders to search to the request.
    findFolderRequest.ParentFolderIds = folderIDArray;

    try
    {
        // Send the request and get the response.
        FindFolderResponseType findFolderResponse = esb.FindFolder(findFolderRequest);

        // Get the response messages.
        ResponseMessageType[] rmta = findFolderResponse.ResponseMessages.Items;

        foreach (ResponseMessageType rmt in rmta)
        { 
            // Cast to the correct response message type.
            if (((FindFolderResponseMessageType)rmt).ResponseClass == ResponseClassType.Success)
                Console.WriteLine("Folder found");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

You can also create a folder by using an XML message. For more information, see FindFolder Operation. The FindFolder operation is also used to find managed custom folders.

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by MicrosoftVisual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.

Compiling the Code

For information about compiling the code, see EWS Client Development in Exchange 2010.

Robust Programming

The namespace for the proxy assembly is defined when the proxy assembly is created.

See Also

Other Resources