Thursday, April 05, 2007

WebService to Find Room and Equipment Mailboxes in Exchange 2007

In Exchange 2007 one of the new features is resource mailboxes out of the box you have two types of these mailboxes Room mailbox and Equipment mailbox. I’m rewriting some Intranet Meeting availability pages at the moment to work with Exchange Web Services and one thing you can’t do in EWS is run a query to find all these type of objects in your Exchange Organization. There are also a bunch of new features such as being able to set the resource capacity (eg how many people a room can hold) and custom resource properties (eg what type of things are in the room such as a whiteboard, projector etc). As this information is all stored in Active Directory you need to use LDAP to query this information. Because this is the type of thing I might want to use in multiple applications I thought I’d put together a little WebService that I could consume that would query this information on behalf of the requesting application and return information about the resource mailboxes firstly their email address's so i could then use this in a GetUserAvailability EWS operation and also where they are located and the extra resource capacity and resource custom properties.

The code to do this is pretty simple it’s just your standard System.DirectoryServices searching code the Ldap filter I used was to filter on the msExchRecipientDisplayType property which seems to get set to 7 for a Room Mailbox and 8 for a Equipment Mailbox. The WebService needs the rights to make these queries into the directory which you will need to solve with Impersonation and Delegation on your server. Alternatively you can hard code the alternate credential in your code (or web.config file) this is what I’m actually doing so If left that code in and just commend it out.

You can have a bit more fun with this if you expand your code eg you can use one of the Exchange custom attributes to store the Ip ranges of the local Ip subnets that are in close proximity to the Meeting Room's physical location. And then grab the IP of the client at the Intranet and use this information to return the meeting rooms close to where the client is making the request from (this depends a lot on your local setup).

I’ve put a downloadable copy of the Webserivce code here the code itself looks like.

using System;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Web.Services.Protocols;
using System.DirectoryServices;
using System.IO;

[WebService(Namespace = "http://msgdev.mvps.org/resourceFind")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
}

[WebMethod]
public XmlDocument FindRooms() {
return FindMailboxes(7);
}
[WebMethod]
public XmlDocument FindEquipment()
{
return FindMailboxes(8);
}

private XmlDocument FindMailboxes(int MailboxType) {
string sqSearchQuery = "";
string mtMailboxType = "";
switch(MailboxType){
case 7:
sqSearchQuery = "(&(&(&(mailNickname=*)(objectcategory=person)(objectclass=user)(msExchRecipientDisplayType=7))))";
mtMailboxType = "Room Mailbox";
break;
case 8:
sqSearchQuery = "(&(&(&(mailNickname=*)(objectcategory=person)(objectclass=user)(msExchRecipientDisplayType=8))))";
mtMailboxType = "Equipment";
break;
}
XmlDocument rdReturnResult = new XmlDocument();
StringWriter xsXmlString = new StringWriter();
XmlWriter xrXmlWritter = new XmlTextWriter(xsXmlString);
xrXmlWritter.WriteStartDocument();
xrXmlWritter.WriteStartElement("Resources");
xrXmlWritter.WriteAttributeString("type", mtMailboxType);
SearchResultCollection srSearchResults;
string roRootDSE = dsDirectorySearcher.SearchRoot.Path;
//string roRootDSE = "LDAP://dcName/DC=e2007dev,DC=domain,DC=com,DC=au";
//DirectoryEntry deDirectoryEntry = new DirectoryEntry(roRootDSE,
@"e2007dev\username", "password");
DirectoryEntry deDirectoryEntry = new DirectoryEntry(roRootDSE);
DirectorySearcher dsDirectorySearcher = new DirectorySearcher(deDirectoryEntry);
dsDirectorySearcher.SearchScope = SearchScope.Subtree;
dsDirectorySearcher.Filter = sqSearchQuery;
dsDirectorySearcher.PropertiesToLoad.Add("mail");
dsDirectorySearcher.PropertiesToLoad.Add("msExchResourceCapacity");
dsDirectorySearcher.PropertiesToLoad.Add("msExchResourceDisplay");
dsDirectorySearcher.PropertiesToLoad.Add("co");
dsDirectorySearcher.PropertiesToLoad.Add("displayName");
dsDirectorySearcher.PropertiesToLoad.Add("department");
dsDirectorySearcher.PropertiesToLoad.Add("description");
dsDirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
dsDirectorySearcher.PropertiesToLoad.Add("postalCode");
dsDirectorySearcher.PropertiesToLoad.Add("postOfficeBox");
dsDirectorySearcher.PropertiesToLoad.Add("st");
dsDirectorySearcher.PropertiesToLoad.Add("streetAddress");
dsDirectorySearcher.PropertiesToLoad.Add("telephoneNumber");
srSearchResults = dsDirectorySearcher.FindAll();
foreach (SearchResult srSearchResult in srSearchResults)
{
xrXmlWritter.WriteStartElement("Mailbox");
xrXmlWritter.WriteAttributeString("emailaddress",srSearchResult.Properties["mail"][0].ToString());
WriteAttributeValue(xrXmlWritter, srSearchResult, "msExchResourceCapacity");
WriteAttributeValue(xrXmlWritter, srSearchResult, "msExchResourceDisplay");
WriteAttributeValue(xrXmlWritter, srSearchResult, "displayName");
WriteAttributeValue(xrXmlWritter, srSearchResult, "co");
WriteAttributeValue(xrXmlWritter, srSearchResult, "department");
WriteAttributeValue(xrXmlWritter, srSearchResult, "physicalDeliveryOfficeName");
WriteAttributeValue(xrXmlWritter, srSearchResult, "postalCode");
WriteAttributeValue(xrXmlWritter, srSearchResult, "postOfficeBox");
WriteAttributeValue(xrXmlWritter, srSearchResult, "st");
WriteAttributeValue(xrXmlWritter, srSearchResult, "streetAddress");
WriteAttributeValue(xrXmlWritter, srSearchResult, "telephoneNumber");
xrXmlWritter.WriteEndElement();
}
xrXmlWritter.WriteEndElement();
xrXmlWritter.WriteEndDocument();
rdReturnResult.LoadXml(xsXmlString.ToString());
return rdReturnResult;
}
private void WriteAttributeValue(XmlWriter xrXmlWritter, SearchResult
srSearchResult, String atAttribute)
{
if (srSearchResult.Properties.Contains(atAttribute))
{
xrXmlWritter.WriteStartElement(atAttribute);
xrXmlWritter.WriteValue(srSearchResult.Properties[atAttribute][0].ToString());
xrXmlWritter.WriteEndElement();
}
else
{
xrXmlWritter.WriteStartElement(atAttribute);
xrXmlWritter.WriteEndElement();
}
}
}