Outlook web add-in How to save info to customer properties in EWS

百般思念 提交于 2021-02-07 10:23:21

问题


Using Office.js API we can easily save extra info to the CustomProperties object via customProps.set. The information is specific to the mail item.

The is the doc Save to custom properties using office.js

Can I achieve the same thing using EWS API?

I tried Creating custom extended properties by using the EWS Managed API 2.0 But whatever info saved via this method I cannot retrieve it using Office.js 's customProps.get.

The User case is that my add-in will save the email body and its attachments binary to the external application. Upon finish the client side will use Office.js 's customProps.set to save the success info to the server, and if you click the same email next time the app will use customProps.get to show you the email has been saved. But if during the lengthy save process(maybe save a big attachment, the user close the task pane, before the customProps.set is executed, the customProps.set will simply not fire because the browser environment (task pane) is closed. So I need to implement this using EWS's API.

My C# code:

Guid PS_PUBLIC_STRINGS = new Guid("a8e14732-37cf-4a46-b69f-1111111111");//add-in manifest id

        ExtendedPropertyDefinition extendedPropertyDefinition =
        new ExtendedPropertyDefinition(PS_PUBLIC_STRINGS, "Expiration Date", MapiPropertyType.String);
        Email.SetExtendedProperty(extendedPropertyDefinition, DateTime.Now.AddDays(2).ToString());
        Email.Update(ConflictResolutionMode.AlwaysOverwrite);//'The requested web method is unavailable to this caller or application.'

JS Code:

Office.context.mailbox.item.loadCustomPropertiesAsync(function (asyncResult) {
                var customProps = asyncResult.value;
                console.log(customProps);
                console.log(customProps.get('Expiration Date')); //undefined
})

回答1:


Yes you can do that but these type of properties follow a specific format because they are application specific. This is documented in https://msdn.microsoft.com/en-us/library/hh968549(v=exchg.80).aspx. They are a named property within the PS_PUBLIC_STRING propset of type String. The property name of the property is prefixed with cecp- and rest of the property name is the GUID of your Mail Apps as defined in the application's manifest Id . The best way to see this is look at item where you have set the property in a MAPI editor like OutlookSpy of MFCMapi where you can see the property (easy to identify with the Cecp prefix) and the format for the value (JSON)

Edit Code example

        ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "cecp-a8e14732-37cf-4a46-b69f-1111111111", MapiPropertyType.String);
        Email.SetExtendedProperty(extendedPropertyDefinition, "{\"Expiration Date\":\"blah\"}");



回答2:


I ended up with this code to save info to extended custom properties that can be read using Office.js.

ExchangeService service = new ExchangeService();

service.Credentials = new WebCredentials("{email}", "{password}");

Guid PS_PUBLIC_STRINGS = new Guid("00020329-0000-0000-C000-000000000046"); //PS_PUBLIC_STRINGS' GUID
ExtendedPropertyDefinition extendedPropertyDefinition =
            new ExtendedPropertyDefinition(PS_PUBLIC_STRINGS, "cecp-{add-in manifest id}", MapiPropertyType.String);
Email.SetExtendedProperty(extendedPropertyDefinition, {JSON});

Email.Update(ConflictResolutionMode.AlwaysOverwrite);

It works. Note that to be able to perform this "write" action, I need to change the authentication from OAUTH to basic(email+password). And I do not think this is a robust way and it should not be recommend for production environment. Why not recommended?



来源:https://stackoverflow.com/questions/53056097/outlook-web-add-in-how-to-save-info-to-customer-properties-in-ews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!