Welcome to MSDN Blogs Sign in | Join | Help

Jive into Messaging world

Everyday is a Challenge, I am loving it...

News

HOWTO: EWS: Get OOF State for Self or any user in your organization

First thing first, this is just for demonstration purpose and might *not* be a supported solution by Microsoft. The setting we are reading from are hidden messages and not documented anywhere. I had to reverse engineer it for demonstration purpose.

Objective: To get OOF State of any user in your organization.

Challenges: You might ask me why not use GetUserOofSettings to query user’s OOF status with the help of Exchange Impersonation? This is due to the limitation of EWS’s GetUserOofSettings/SetUserOofSettings methods as they don’t support Impersonation.

Workaround: Directly reading the hidden messages from Mailbox Root where this Flag & OOF message is stored. Since we are read-only we should be good to do so, that’s my assumption.

We need to first make a FindItem call to the mailbox root and search for a message with ItemClass = “IPM.Microsoft.OOF.UserOOFSettings”. Assuming that there will be only one message with that message class at any given time, we get its ItemId and make a GetItem call to read the message body. The message body is plain text XML with following structure

 

For OOF enabled…

<UserOofSettings>
<OofState >Enabled</OofState>
</UserOofSettings>

For OOF disabled…

<UserOofSettings>
<OofState >Disabled</OofState>
</UserOofSettings>

There is also a state called SCHEDULED, please refer to the below mentioned article to know more about it

Reference: http://msdn.microsoft.com/en-us/library/aa580465.aspx

This code sample can do the job for you.

using System;
using GetOOFState.MyEWS;

namespace GetOOFState
{
    class Program
    {
        static void Main(string[] args)
        {

            
            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.Url = "https://server/ews/exchange.asmx";
            esb.Credentials = new System.Net.NetworkCredential("ServiceAccount", Password", "Domain");

            esb.ExchangeImpersonation = new ExchangeImpersonationType();
            esb.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType();
            esb.ExchangeImpersonation.ConnectingSID.PrimarySmtpAddress = "username@domain.com";
            

            FindItemType fit = new FindItemType();
            fit.ItemShape = new ItemResponseShapeType();
            fit.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
            fit.Restriction = new RestrictionType();

            IsEqualToType itemMessageClass = new IsEqualToType();
            
            PathToUnindexedFieldType msgClassProp = new PathToUnindexedFieldType();
            msgClassProp.FieldURI = UnindexedFieldURIType.itemItemClass;

            ConstantValueType msgClassValue = new ConstantValueType();
            msgClassValue.Value = "IPM.Microsoft.OOF.UserOOFSettings";

            itemMessageClass.Item = msgClassProp;
            itemMessageClass.FieldURIOrConstant = new FieldURIOrConstantType();
            itemMessageClass.FieldURIOrConstant.Item = msgClassValue;
            
            
            fit.Restriction = new RestrictionType();
            fit.Restriction.Item = itemMessageClass;

            DistinguishedFolderIdType rootFolder = new DistinguishedFolderIdType();
            rootFolder.Id = DistinguishedFolderIdNameType.root;

            fit.ParentFolderIds = new BaseFolderIdType[] { rootFolder };

            FindItemResponseType firt = esb.FindItem(fit);
            string itemID = ((ArrayOfRealItemsType)((FindItemResponseMessageType)firt.ResponseMessages.Items[0]).RootFolder.Item).Items[0].ItemId.Id;

            GetItemType git = new GetItemType();
            ItemIdType iit = new ItemIdType();
            iit.Id = itemID;

            git.ItemIds = new BaseItemIdType[] { iit };

            git.ItemShape = new ItemResponseShapeType();
            git.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;

            PathToUnindexedFieldType []propsToFetch = new PathToUnindexedFieldType[1];
            propsToFetch[0] = new PathToUnindexedFieldType();
            propsToFetch[0].FieldURI = UnindexedFieldURIType.itemBody;

            git.ItemShape.AdditionalProperties = propsToFetch;
            
   

            GetItemResponseType girt = esb.GetItem(git);
            ItemType itemOOF = ((ItemType)(((ItemInfoResponseMessageType)girt.ResponseMessages.Items[0]).Items.Items[0]));

            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.LoadXml(itemOOF.Body.Value);
            Console.WriteLine("OOF State: {0}", xDoc.GetElementsByTagName("OofState")[0].InnerText);
            

            

        }
    }
}

Happy Coding!!!

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker