none
get Busy,away,free etc status from outlook calendar in C# RRS feed

  • Question

  • Hello people,

    I have spend some hours looking for a way to get the status of a user from her/his calendar. I have found some info. Mainly the appointmentItem Properties, but there is no example code.

    Can you please provide an article or an example how to do this.

    As far as I have read  I will need to connected to the calendar and retrive the folder info is that correct?

    Best regards
    Gouzgounis

    Saturday, March 5, 2016 2:45 PM

Answers

  • Hello Gouzgounis,

    Do you develop an Outlook add-in or standalone application? Do you use EWS or REST API?

    You can use the FreeBusy method of the Recipient class which returns free/busy information for the recipient. The default is to return a string representing one month of free/busy information compatible with the Microsoft Schedule+ Automation format (that is, the string contains one character for each MinPerChar minute, up to one month of information from the specified Startdate).

    If the optional argument CompleteFormat is omitted or False, then "free" is indicated by the character 0 and all other states by the character 1.

    If CompleteFormat is True, then the same length string is returned as defined above, but the characters now correspond to the OlBusyStatus constants: olBusyolFreeolOutOfOffice, or olTentative.

    Public Sub GetFreeBusyInfo()
    	Dim myOlApp As New Outlook.Application
    	Dim myNameSpace As Outlook.NameSpace
    	Dim myRecipient As Outlook.Recipient
    	Dim myFBInfo As String
    	Set myNameSpace = myOlApp.GetNamespace("MAPI")
    	Set myRecipient = myNameSpace.CreateRecipient("Nate Sun")
    	On Error GoTo ErrorHandler
    	myFBInfo = myRecipient.FreeBusy(#11/11/2003#, 60 * 24)
    	MsgBox myFBInfo
    	Exit Sub
    	ErrorHandler:     
    		MsgBox "Cannot access the information. "
    End Sub

    Also you may find the How do I get the Outlook VBA “FreeBusy” method to return the truth? forum thread helpful. 

    The Find/FindNext and Restrict methods allows to get Outlook items that correspond to the specified conditions. So, you can use them to check whether a user has any appointments at the specified time. Read more about these methods in the following articles:

    How To: Use Restrict method in Outlook to get calendar items

    How To: Retrieve Outlook calendar items using Find and FindNext methods

    Saturday, March 5, 2016 7:35 PM

All replies

  • Hello Gouzgounis,

    Do you develop an Outlook add-in or standalone application? Do you use EWS or REST API?

    You can use the FreeBusy method of the Recipient class which returns free/busy information for the recipient. The default is to return a string representing one month of free/busy information compatible with the Microsoft Schedule+ Automation format (that is, the string contains one character for each MinPerChar minute, up to one month of information from the specified Startdate).

    If the optional argument CompleteFormat is omitted or False, then "free" is indicated by the character 0 and all other states by the character 1.

    If CompleteFormat is True, then the same length string is returned as defined above, but the characters now correspond to the OlBusyStatus constants: olBusyolFreeolOutOfOffice, or olTentative.

    Public Sub GetFreeBusyInfo()
    	Dim myOlApp As New Outlook.Application
    	Dim myNameSpace As Outlook.NameSpace
    	Dim myRecipient As Outlook.Recipient
    	Dim myFBInfo As String
    	Set myNameSpace = myOlApp.GetNamespace("MAPI")
    	Set myRecipient = myNameSpace.CreateRecipient("Nate Sun")
    	On Error GoTo ErrorHandler
    	myFBInfo = myRecipient.FreeBusy(#11/11/2003#, 60 * 24)
    	MsgBox myFBInfo
    	Exit Sub
    	ErrorHandler:     
    		MsgBox "Cannot access the information. "
    End Sub

    Also you may find the How do I get the Outlook VBA “FreeBusy” method to return the truth? forum thread helpful. 

    The Find/FindNext and Restrict methods allows to get Outlook items that correspond to the specified conditions. So, you can use them to check whether a user has any appointments at the specified time. Read more about these methods in the following articles:

    How To: Use Restrict method in Outlook to get calendar items

    How To: Retrieve Outlook calendar items using Find and FindNext methods

    Saturday, March 5, 2016 7:35 PM
  • Hello Eugene,

    Thank you for the answer I will have a look at the material you send. I am making the application on WPF (that means stand alone application) and I need the calendar data from the outlook instance, on the computer that the application is installed on. Your question about API made me realise I do not contact the exchange server. Do I need this or can I avoid contacting the exchange server and just find the data localy?

    thanks in advance.

    best regards
    Gouzgounis


    • Edited by Gouzgounis Sunday, March 6, 2016 12:40 PM text edit
    Sunday, March 6, 2016 12:40 PM
  • You can automate Outlook from your application to gather the required information. See C# app automates Outlook (CSAutomateOutlook) for more information. 
    Sunday, March 6, 2016 1:58 PM
  • Hello again Eugene,

    I used the example you send and created the following code.

    {
    Outlook.Application oOutlook = new Outlook.Application();
    Outlook.NameSpace oNS = oOutlook.GetNamespace("mapi");
    Outlook.Recipient recipient = oNS.CreateRecipient("myOutlook@domain.whatever");
    STATUS.Text = recipient.FreeBusy(DateTime.Now,5,false).Substring(0,1);
    
    }

    So essentially I want for the next 5 min to get one time answer of a 0 or 1 and then my logic will deal with it. But I am only getting 0, no matter what event I have set up in the calendar. I have also tried with CompleteFormat  true. Same result. Only 0.

    any suggestions?

    Thank you once again.

    EDIT: Okay now it says that a COMException was unhandled. It can not load free/busy information

    EDIT 2: COMException was due to Outlook not been able to clean it's free&busy status. Manual clean did it and I guess a restart of Outlook will do it as well. 

    EDIT 3: Okay the problem has to be the DateTime.Now because if I use a new DateTime(and a date) I get all the events I need. It is just pain in the ass to write more logic if I can get around it and have only a bit of simple code.

    Any input welcomed.

    • Edited by Gouzgounis Monday, March 7, 2016 10:28 AM more updates
    Monday, March 7, 2016 9:19 AM
  • Hi GouZounis,

    To handle the return result of the Recipient.FreeBusy is reiterative to the detail requirement of business. For example, in the code above you were passing 5 minutes to the MinPerChar parameter. And the result also depends on the time you want to know. 

    I also write a simple figure to demonstrate it:

    And the algorithm for this scenario should be convert (e-s) to minutes(m), then m/d+1 to get the correct position of the return string. Then we can get the status of the given time based on the position.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 8, 2016 9:51 AM
    Moderator
  • Hey Fei,

    I have solved the issue before I saw your post - I blame time deference.

    The solution I used it very similar to that you posted. I am posting some example for future reference

    int rule = DateTime.Now.Inminuted
    
    string freeStatus = revipient.FreeBusy(DateTime.Today,5,true).substring(0,24);
    
    char[] array = freeStatus.ToCharArray();
    
    for each character in array for rule +/- x
      if array[i] is what I need
      do logic here
    
    if (log is ok)
       STATUS busy or free


    This is very simple example of code not in an languish, just to illustrate the process. The most important is the first three lines of code that are actual code.

    Now I get a small problem. My code working on Outlook 2013 but not in Outlook2016. Does anyone know why?

    I get an error: Can not retrieve free busy status.

    Best regards Gouzgounis 

    EDIT: Okay the problem is that I am linking a web mail to my 2016 outlook. Does anyone know how to get data from outlook calendar that is looking into a web mail? I am trying ExchangeServices but I am not really good at this - never done Exchanges staff before.

    EDIT2: Okay solved! God damn it!! Outlook was blocking my app to access the data. And the 

    Outlook.Recipient recipient = oNS.CreateRecipient

    did not work as well. Outlook does not like it.

    I am using Address book instead and everything works. Code looks like this

    Outlook.AddressEntries book = oNS.GetGlobalAddressList().AddressEntries;
    
    Outlook.AddressEntry testSearch = book["User name"];


    Now I need to find out how to allow my app to have access to Outlook all the time.

    EDIT 3: And I was sooooo wrong. There is this reference https://msdn.microsoft.com/en-us/library/gg675578(v=exchg.80).aspx

    very useful, now I am on the XML code tracking. Any alternatives are welcomed.

    • Edited by Gouzgounis Wednesday, March 9, 2016 2:26 PM Edit of edit
    Wednesday, March 9, 2016 7:56 AM