[EWS]How to retrieve the OOF (Out Of Facility) settings/message using EWS for an Exchange user?
Technorati Tags: EWS,Exchange 2010,GetMailTips,GetUserOofSettings,Out of Facility,Out of Office,EWS Managed API,Exchange 2013,Exchange Online,Exchange Web Services,DevMsg,HTTP EWS SOAP request
Our customers have a common ask wherein they would like to check if the organization’s employees are available in office or out of office on a given day. With Exchange Web Services (EWS) being the flagship API for Exchange related development, the relevant call they come across is GetUserOofSettingsin EWS Managed API.
However, when you make this call from a normal Exchange user account, you get an error message saying:
"Access is denied. Check credentials and try again".
The reason being GetUserOofSettings callworks only for the users with the Exchange impersonation permissions. As for example, assume that the EWS call is made using the User A’s credential and he’s trying to access User B’s OOF settings. In that case, User A needs to have Exchange impersonation permissions on User B’s mailbox.
In case if you’re requirement is not to run the EWS application under impersonation service account then we could use Mail Tips to fetch the automatic reply set by the Exchange user.
Currently, we don’t have a EWS Managed API call to retrieve the Mail Tips. However, we have the method GetMailTips operation available with Exchange Web Services to retrieve the out of office settings.
We can create the soap request for GetMailTips operation in the following ways:
Use the Visual Studio to generate the proxy classes for EWS and then use it write the code for GetMailTips operation.
Post the request directly from your EWS managed API application.
Here is a sample using EWS Managed API:
1: /* Disclaimer:
2: By using the following materials or sample code you agree to be bound by the license terms below and the Microsoft Partner Program Agreement the terms of which are incorporated herein by this reference. These license terms are an agreement between Microsoft Corporation (or, if applicable based on where you are located, one of its affiliates) and you. Any materials (other than sample code) we provide to you are for your internal use only. Any sample code is provided for the purpose of illustration only and is not intended to be used in a production environment. We grant you a nonexclusive, royalty-free right to use and modify the sample code and to reproduce and distribute the object code form of the sample code, provided that you agree: (i) to not use Microsoft's name, logo, or trademarks to market your software product in which the sample code is embedded; (ii) to include a valid copyright notice on your software product in which the sample code is embedded; (iii) to provide on behalf of and for the benefit of your subcontractors a disclaimer of warranties, exclusion of liability for indirect and consequential damages and a reasonable limitation of liability; and (iv) to indemnify, hold harmless, and defend Microsoft, its affiliates and suppliers from and against any third party claims or lawsuits, including attorneys' fees, that arise or result from the use or distribution of the sample code.*/
3:
4: string getMailTipssoapResponse = oofCall(service.Url.AbsoluteUri);
5:
6: private static string oofCall(String urlname)
7: {
8: WebRequest webRequest = WebRequest.Create(urlname);
9: HttpWebRequest httpwebRequest = (HttpWebRequest)webRequest;
10: httpwebRequest.Method = "POST";
11: httpwebRequest.ContentType = "text/xml; charset=utf-8";
12: httpwebRequest.ProtocolVersion = HttpVersion.Version11;
13: httpwebRequest.Credentials = CredentialCache.DefaultCredentials;
14: httpwebRequest.Timeout = 60000;
15: Stream requestStream = httpwebRequest.GetRequestStream();
16: StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
17:
18: StringBuilder getMailTipsSoapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"https://www.w3.org/2001/XMLSchema-instance\"");
19: getMailTipsSoapRequest.Append(" xmlns:xsd=\"https://www.w3.org/2001/XMLSchema\" ");
20: getMailTipsSoapRequest.Append("xmlns:soap=\"https://schemas.xmlsoap.org/soap/envelope/\" ");
21: getMailTipsSoapRequest.Append("xmlns:t=\"https://schemas.microsoft.com/exchange/services/2006/types\"><soap:Header>");
22: getMailTipsSoapRequest.Append(" <t:RequestServerVersion Version=\"Exchange2010\"/></soap:Header><soap:Body>");
23: getMailTipsSoapRequest.Append("<GetMailTips xmlns=\"https://schemas.microsoft.com/exchange/services/2006/messages\">");
24: getMailTipsSoapRequest.Append("<SendingAs>");
25: getMailTipsSoapRequest.Append("<t:EmailAddress>user1@contoso.com</t:EmailAddress>");
26: getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></SendingAs>");
27: getMailTipsSoapRequest.Append("<Recipients><t:Mailbox>");
28: getMailTipsSoapRequest.Append("<t:EmailAddress>user2@contoso.com</t:EmailAddress>");
29: getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></t:Mailbox></Recipients>");
30: getMailTipsSoapRequest.Append(" <MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips>");
31: getMailTipsSoapRequest.Append("</soap:Body></soap:Envelope>");
32:
33: streamWriter.Write(getMailTipsSoapRequest.ToString());
34: streamWriter.Close();
35: HttpWebResponse webResponse = (HttpWebResponse)httpwebRequest.GetResponse();
36:
37: StreamReader streamreader = new StreamReader(webResponse.GetResponseStream());
38: string result = streamreader.ReadToEnd();
39: return result;
40: }
Here is the response that I got from server:
1: <?xml version="1.0" encoding="utf-8" ?>
2: - <s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
3: - <s:Header>
4: <h:ServerVersionInfo MajorVersion="14" MinorVersion="3" MajorBuildNumber="136" MinorBuildNumber="1" Version="Exchange2010_SP2" xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types" xmlns="https://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" />
5: </s:Header>
6: - <s:Body xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
7: - <GetMailTipsResponse ResponseClass="Success" xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
8: <ResponseCode>NoError</ResponseCode>
9: - <ResponseMessages>
10: - <MailTipsResponseMessageType ResponseClass="Success">
11: <ResponseCode>NoError</ResponseCode>
12: - <m:MailTips xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages">
13: - <t:RecipientAddress xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
14: <t:Name />
15: <t:EmailAddress>user2@contoso.com</t:EmailAddress>
16: <t:RoutingType>SMTP</t:RoutingType>
17: </t:RecipientAddress>
18: <t:PendingMailTips xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" />
19: - <t:OutOfOffice xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
20: - <t:ReplyBody xml:lang="en-us">
21: <t:Message> I am Out of Office Today</t:Message>
22: </t:ReplyBody>
23: </t:OutOfOffice>
24: </m:MailTips>
25: </MailTipsResponseMessageType>
26: </ResponseMessages>
27: </GetMailTipsResponse>
28: </s:Body>
29: </s:Envelope>
Though the GetMailTips operation is NOT yet available with EWS Managed API, with the above sample using raw HTTP EWS SOAP request we can easily retrieve the required information. For more details, refer the following articles:
- GetMailTips Operation https://msdn.microsoft.com/en-us/library/exchange/dd877060(v=exchg.150).aspx
**MailTipsRequested**
[https://msdn.microsoft.com/en-us/library/exchange/dd899452(v=exchg.150).aspx](https://msdn.microsoft.com/en-us/library/exchange/dd899452\(v=exchg.150\).aspx)
Comments
- Anonymous
March 09, 2017
An important bit here seems to be that the SOAP response will only contain the information for whatever network you're doing the request from. So even if you specify that you're sending as an external address you won't get the external message. At least that is how it's currently looking for me.