简体   繁体   中英

Access Outlook user properties from EWS

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.

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)});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM