none
[E2010][EWS] How to retrieve calendar items of a defined category? RRS feed

  • Question

  • Hi,

    I'm trying with VBScript to retrieve calendar items of a mailbox for a category.

    I managed with EWS to retrieve ALL the calendar items of a mailbox :

    const TARGETURL="https://.../ews/exchange.asmx" const USERNAME="username" const PASSWORD="pasword" Const strEmailAddress = "mail@mail.com" Dim objXmlHttp, xmlDoc Set objXmlHttp = CreateObject("Microsoft.XMLHTTP") Set objXmlDoc = CreateObject("MSXML2.DOMDocument")

    'XML request strXmlData = "" strXmlData = strXmlData & "<?xml version=""1.0"" encoding=""utf-8""?> " strXmlData = strXmlData & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" " strXmlData = strXmlData & " xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types""> " strXmlData = strXmlData & " <soap:Body> " strXmlData = strXmlData & " <FindItem xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages"" " strXmlData = strXmlData & " xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"" " strXmlData = strXmlData & " Traversal=""Shallow""> " strXmlData = strXmlData & " <ItemShape> " strXmlData = strXmlData & " <t:BaseShape>AllProperties</t:BaseShape> " strXmlData = strXmlData & " </ItemShape> " strXmlData = strXmlData & " <CalendarView StartDate=""2014-01-20T13:00:00"" EndDate=""2014-01-30T23:59:59""/>" strXmlData = strXmlData & " <ParentFolderIds> " strXmlData = strXmlData & " <t:DistinguishedFolderId Id=""calendar""> " strXmlData = strXmlData & " <t:Mailbox><t:EmailAddress>" & strEmailAddress & "</t:EmailAddress></t:Mailbox> " strXmlData = strXmlData & " </t:DistinguishedFolderId> " strXmlData = strXmlData & " </ParentFolderIds> " strXmlData = strXmlData & " </FindItem> " strXmlData = strXmlData & " </soap:Body> " strXmlData = strXmlData & "</soap:Envelope> " objXmlHttp.open "POST", TARGETURL, False, USERNAME, PASSWORD objXmlHttp.setRequestHeader "Content-Type", "text/xml" objXmlHttp.send strXmlData 'status 200 If objXmlHttp.Status = "200" Then If objXmlDoc.loadXML(objXmlHttp.responseText) Then Set soap_Body = objXmlDoc.documentElement.childNodes.item(1) Set m_FindItemResponse = soap_Body.childNodes.item(0) Set m_ResponseMessages = m_FindItemResponse.childNodes.item(0) Set m_FindItemResponseMessage = m_ResponseMessages.childNodes.item(0) Set m_RootFolder = m_FindItemResponseMessage.childNodes.item(1) Set t_Items = m_RootFolder.childNodes.item(0) For Each calendarItem In t_Items.childNodes Dim t_Subject t_Subject = calendarItem.getElementsByTagName("t:Subject").item(0).text msgbox t_Subject 'Show the subject of the item Next End If Else wscript.echo "Error: " & objXmlHttp.Status & " - " & objXmlHttp.statusText End If

    But now I would like to add a filter in the XML code to retrieve only the calendar items of a category called "TEST".

    How can I do this please?

    Thanks



    • Edited by Het44 Monday, January 27, 2014 12:44 PM
    Monday, January 27, 2014 9:02 AM

Answers

  • >> Do you have an example with ItemView to add a filter for a category? I didn't manage to make the query.

    Firstly I would suggest you use Powershell and the EWS Managed API, this will save you hours of work and many many lines of code.

    Because the category field is a multivalued property the best way to query this is to use a AQS query string the SOAP would look like

      <?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Header>
          <t:RequestServerVersion Version="Exchange2010_SP2" />      
        </soap:Header>
        <soap:Body>
          <m:FindItem Traversal="Shallow">
            <m:ItemShape>
              <t:BaseShape>AllProperties</t:BaseShape>
            </m:ItemShape>
            <m:IndexedPageItemView MaxEntriesReturned="1000" Offset="0" BasePoint="Beginning" />
            <m:ParentFolderIds>
              <t:DistinguishedFolderId Id="calendar" />
            </m:ParentFolderIds>
            <m:QueryString>System.Category:'Green Category'</m:QueryString>
          </m:FindItem>
        </soap:Body>
      </soap:Envelope>

    Because this is a 2010 feature versioning you request is critical.

    Cheers
    Glen

    • Marked as answer by Het44 Friday, January 31, 2014 1:00 PM
    Wednesday, January 29, 2014 4:17 AM

All replies

  • In EWS you can't use a Restriction with a CalendarView so the best you can do is just filter the results at the client side (the calendarView is already restricting the time range). 

    When you use a CalendarView what happens in on the Exchange server all the Recurring appointments are expanded. From a performance perspective the server is optimized to do this so these requests always perform well. You could use a ItemView and a Restriction but from a performance perspective it generally won't yield any quicker results only a smaller result set and you won't get Recurring appointment expansion. 

    Cheers
    Glen

    Tuesday, January 28, 2014 4:52 AM
  • Ok thank you.

    In my example I put a date, but I don't need to restrict the period.

    I would like to retrieve all the calendar items of a category of my choice since the beginning, that's why I would prefer to have a filter in the query, to avoid to have a large result with a lot of items I don't need.

    Do you have an example with ItemView to add a filter for a category? I didn't manage to make the query.

    Thanks

    Tuesday, January 28, 2014 1:18 PM
  • >> Do you have an example with ItemView to add a filter for a category? I didn't manage to make the query.

    Firstly I would suggest you use Powershell and the EWS Managed API, this will save you hours of work and many many lines of code.

    Because the category field is a multivalued property the best way to query this is to use a AQS query string the SOAP would look like

      <?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Header>
          <t:RequestServerVersion Version="Exchange2010_SP2" />      
        </soap:Header>
        <soap:Body>
          <m:FindItem Traversal="Shallow">
            <m:ItemShape>
              <t:BaseShape>AllProperties</t:BaseShape>
            </m:ItemShape>
            <m:IndexedPageItemView MaxEntriesReturned="1000" Offset="0" BasePoint="Beginning" />
            <m:ParentFolderIds>
              <t:DistinguishedFolderId Id="calendar" />
            </m:ParentFolderIds>
            <m:QueryString>System.Category:'Green Category'</m:QueryString>
          </m:FindItem>
        </soap:Body>
      </soap:Envelope>

    Because this is a 2010 feature versioning you request is critical.

    Cheers
    Glen

    • Marked as answer by Het44 Friday, January 31, 2014 1:00 PM
    Wednesday, January 29, 2014 4:17 AM
  • Thanks a lot, I will try.
    Friday, January 31, 2014 1:00 PM