Ever wanted to know when someone was busy so you could plan a meeting? In outlook you'll notice a nice graph that shows that information when creating a meeting. How can we get that same information on our website? This is actually pretty easy to do and uses OWA to accomplish it. All we need to do is query the Exchange server, get an XML doc that contains that information, parse said XML doc, and voilà we have our info. Without further ado, here's the code:
public static byte[] GetFreeBusy(string People, DateTime StartTime, DateTime EndTime, int Interval, string UserName, string Password)
{
string EmailAddresses = "&u=SMTP:" + People;
//Make sure to switch this to point to your exchange server
string Url = "exchangeserver/public/?cmd=freebusy&start=" + StartTime.Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss") + "-05:00&end=" + EndTime.AddDays(1).Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss") + "-05:00&interval=" + Interval.ToString() + EmailAddresses;
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
byte[] bytes = null;
System.IO.Stream ResponseStream = null;
XmlDocument ResponseXmlDoc = null;
XmlNodeList DisplayNameNodes = null;
try
{
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new System.Uri(Url),
"NTLM",
new System.Net.NetworkCredential(UserName, Password, "")
);
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(Url);
Request.Credentials = MyCredentialCache;
Request.Method = "GET";
Request.ContentType = "text/xml; encoding='utf-8'";
Request.UserAgent = "Mozilla/4.0(compatible;MSIE 6.0; " +
"Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)";
Request.KeepAlive = true;
Request.AllowAutoRedirect = false;
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream);
DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:item");
if (DisplayNameNodes.Count > 0)
{
for (int i = 0; i < DisplayNameNodes.Count; i++)
{
XmlNodeList Nodes = DisplayNameNodes[i].ChildNodes;
foreach (XmlNode Node in Nodes)
{
if (Node.Name == "a:fbdata")
{
bytes = new byte[Node.InnerText.Length];
for (int x = 0; x < Node.InnerText.Length; ++x)
{
bytes[x] = byte.Parse(Node.InnerText[x].ToString());
}
}
}
}
}
else
{
throw new Exception("Could not get free/busy data from the exchange server");
}
ResponseStream.Close();
Response.Close();
return bytes;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
And that's all there is to it. This function will query the Exchange server and return a byte array containing the information. A 0 means not busy, 2 means it doesn't know, and anything else pretty much means busy. Also note that the array is divided into minutes based on the interval you give. So if the start and end time are an hour away and you put it at 10 minute intervals, you'll get an array with the size of 6. Anyway, test out the code, leave feedback, etc.
3ca035ca-db07-411b-a272-0259ebb04f42|1|5.0