Access Outlook user properties from EWS

邮差的信 提交于 2019-12-19 10:18:28

问题


I'm trying to create an application that use the EWS api to access contacts.

I need to look at one of the outlook user properties in this process but I cant see how to get it at using EWS. At the moment ive just tried...

service.Url = new Uri("https://url/ews/Exchange.asmx");
service.Credentials = new WebCredentials("credentials");
var results = service.FindItems(folderId, new ItemView(100));
foreach (var item in results)
{
     Contact contact = item as Contact;
     foreach (var prop in contact.ExtendedProperties)
     {
            Console.WriteLine(prop.Value.ToString());
     }
}

Which compiles and executes without a problem, but for every contact the ExtendedProperties count is 0 which in outlook its about 30.

So how can I get the properties I'm looking for?

Just an FYI. Im using exhcnage 2007.

Thanks.


回答1:


You need to define the properties you want to get - EWS does not permit you to enumerate user properties.

The Userproperties are in the namespace PublicStrings.

private static readonly ExtendedPropertyDefinition CustomProperty = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "MyCustomProperty", MapiPropertyType.String);

You can then use the definition in a FindItems request:

var items = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100) { PropertySet =   new PropertySet(BasePropertySet.FirstClassProperties, CustomProperty)});



回答2:


I had the same problem, solved.

var customProp1 = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings,
                        "myCustomPropOne", MapiPropertyType.String);

var customProp2 = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings,
                      "myCustomPropTwo", MapiPropertyType.String);

var userFields = new ExtendedPropertyDefinition[] { customProp1, customProp2 };

var extendedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, userFields);

var contactItems = service.FindItems(WellKnownFolderName.Contacts, new ItemView(100)
            { PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, extendedPropertySet) });
// Looping contacts
    foreach (Item item in contactItems){
        object firstProp;              
        if (item.TryGetProperty(customProp1, out firstProp) && firstProp != null)
        {
               var val = firstProp.ToString();
        }
        object secondProp;
        if (item.TryGetProperty(customProp2, out secondProp) && secondProp != null)
        {
               var val = secondProp.ToString();
        }
     } // loop ends

myCustomPropOne & myCustomPropTwo are names of user defined properties you set in outlook/editor. ref



来源:https://stackoverflow.com/questions/6745452/access-outlook-user-properties-from-ews

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