Tavi Truman
Tavi Truman

Reputation: 107

How to Create and Associate a Category to a Contact using EWS Managed API 2.2

Is it possible to create a Category Item Object and Associate it with a Contact using the EWS Managed API?

Upvotes: 1

Views: 3556

Answers (2)

nblivingston
nblivingston

Reputation: 151

I came across this post while looking to update the master category list using EWS, but not the Managed API. In case anyone else is looking to do the same thing, here's the solution I came up with, which uses Python and requests:

import base64
import requests
from requests_ntlm import HttpNtlmAuth


# Create the XML request template for EWS
payload = r"""<?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="Exchange2013" />
  </soap:Header>
  <soap:Body>
    <m:UpdateUserConfiguration>
      <t:UserConfigurationName Name="CategoryList">
        <t:DistinguishedFolderId Id="calendar">
        <t:Mailbox>
        <t:EmailAddress>your_email@your_domain.com</t:EmailAddress>
        <t:RoutingType>SMTP</t:RoutingType>
        <t:MailboxType>Mailbox</t:MailboxType>
        </t:Mailbox>
        </t:DistinguishedFolderId>
      </t:UserConfigurationName>
      <t:XmlData>{}</t:XmlData>
    </m:UpdateUserConfiguration>
  </soap:Body>
</soap:Envelope>"""

# Create the categories XML
# Colors: https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxocfg/eb7cac90-6200-4ac3-8f3c-6c808c681c8b
xml_data = r"""<?xml version="1.0"?>
  <categories default="First category" xmlns="CategoryList.xsd">
  <category name="First category" color="1" />
  <category name="Second category" color="2" />
  </categories>"""

# Encode the payload and insert it into the XML request
xml = base64.b64encode(xml_data.encode())
payload = payload.replace('{}', str(xml).strip('b\''))

# Use requests with the NTLM authentication libary to submit the request
headers = {'content-type': 'text/xml'}
ews_url = 'https://mail.your-organizations-exchange-server.com/EWS/Exchange.asmx'
session = requests.Session()
session.auth = HttpNtlmAuth(exhange_username, exchange_password)
response = session.post(ews_url, data=payload, headers=headers)
print(response.text)

Upvotes: 0

Glen Scales
Glen Scales

Reputation: 22032

You can assign a Category to any Object in a Mailbox using the Categories property https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.item.categories(v=exchg.80).aspx .

For a particular color/description to be show in Outlook or OWA the category you pass in must match an Item in the Master category list. You can read/modify the Master category list in a Mailbox using EWS eg https://social.msdn.microsoft.com/Forums/en-US/e5c5f072-0b5c-49ce-9db7-57f76f5e011e/edit-master-category-list-in-exchange-2010-via-ews?forum=exchangesvrdevelopment and https://social.msdn.microsoft.com/Forums/en-US/a3917500-2bbc-4def-98b4-696e49efed6f/adding-categories-to-a-users-master-category-list-in-exchange-2010-using-ews?forum=exchangesvrdevelopment

Upvotes: 2

Related Questions