'How to create a category and apply it to an email

Is it possible create a category in outlook programmatically?

I set up a hello world outlook-addin by following MS's tutorials. And I see how I have access to all the different properties of a particular email. However, I'm stumped about how to work with categories.



Solution 1:[1]

I had to pass in the following SOAP request through Office.context.mailbox.makeEwsRequestAsync() to create a category called "Muktader" and apply it to an email identified by the item id.

<?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_SP1" />
  </soap:Header>
  <soap:Body>
    <m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite">
      <m:ItemChanges>
        <t:ItemChange>
          <t:ItemId Id="AAMkAGVlOTZjNTM3LWVjNjgtNGZlNi04MTBkLWIyNjNjNWEyY2VlNABGAAAAAABpsgv3HB+wQJRg4K+r7AmBBwBJi9ckXu/REb74AIBfn0G8AAAUrOs1AACN8cPrPdSYR5RdhR69ULJ0AAACOkAqAAA=" ChangeKey="CQAAABYAAACN8cPrPdSYR5RdhR69ULJ0AAACR0YO" />
          <t:Updates>
            <t:SetItemField>
              <t:FieldURI FieldURI="item:Categories" />
              <t:Message>
                <t:Categories>
                  <t:String>Muktader</t:String>
                </t:Categories>
              </t:Message>
            </t:SetItemField>
          </t:Updates>
        </t:ItemChange>
      </m:ItemChanges>
    </m:UpdateItem>
  </soap:Body>
</soap:Envelope>

Solution 2:[2]

To create a category, you will either have to use EWS or REST APIs. The Office.js library does not provide a way to directly set a category via javascript today.

Solution 3:[3]

To create category in outlook using EWS SOAP method:

  1. Use Office.context.mailbox.makeEwsRequestAsync() method to call SOAP async request.
  2. Use SOAP request to set custom category to the email
  3. Use SOAP request response for result
  4. Also it is possible to create master category and then use that category for email with color required. Note: Also update .xml file with ReadWriteMailbox permissions. Also pass current email itemID and ChangeKey to SOAP request.

Use Office.context.mailbox.makeEwsRequestAsync() method to call SOAP async request.

Office.context.mailbox.makeEwsRequestAsync(updateItemRequest(itemID, changeKey), function (updateAsyncResult) {
        if (updateAsyncResult.status === "failed") {
            var error = updateAsyncResult.error;
            console.log("error " + error.name + ": " + error.code + " - " + error.message);
        }
        else {
            console.log("Result: " + updateAsyncResult.value);
        }
    });
Use SOAP request to set custom category to the email

function updateItemRequest(id, changeKey) {
    var request =
        '<?xml version="1.0" encoding="utf-8"?>\
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
        xmlns:xsd = "http://www.w3.org/2001/XMLSchema"\
        xmlns:soap = "http://schemas.xmlsoap.org/soap/envelope/"\
        xmlns:t = "http://schemas.microsoft.com/exchange/services/2006/types">\
                <soap:Header>\
                    <t:RequestServerVersion Version="Exchange2013" />\
                </soap:Header>\
                <soap:Body>\
                   <UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite" xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">\
                      <ItemChanges>\
                        <t:ItemChange>\
                          <t:ItemId Id =\"' + id + '\"  ChangeKey=\"' + changeKey + '\"   />\
                          <t:Updates>\
                            <t:SetItemField>\
                              <t:FieldURI FieldURI = "item:Categories" />\
                              <t:Message>\
                                <t:Categories>\
                                  <t:String>CategoryName</t:String>\
                                </t:Categories>\
                              </t:Message>\
                            </t:SetItemField>\
                          </t:Updates>\
                        </t:ItemChange>\
                      </ItemChanges>\
                    </UpdateItem>\
                </soap:Body >\
            </soap:Envelope >';

    console.log("Log update request: " + request);
    return request;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rayhan Muktader
Solution 2 AndrewS
Solution 3 Sanket