Accessing User Defined Fields from EWS Managed API

Hello,

I am using the Exchange Web Services Managed API and I am trying to access user-defined fields created through Outlook related to contacts in a public folder.  The fields are created as "User-defined fields in folder" in Outlook from the Contact screen, from the All Fields window.  I can access the contacts through the EWS Managed API, but I can't seem to access these user-defined fields.  They don't appear in the extended properties collection.  Is there some way to access them?  If so, can I write to them as well as read them? 

If I can't access the user defined fields then is there some other way to define fields/extended properties that users will be able to see through a contact in Outlook and that I can read and update through the API?

Thanks, 

CS

October 21st, 2010 12:30am

I would love an answer to this question, anyone got a solution? I am working with Exchange Server 2007 SP1.
Free Windows Admin Tool Kit Click here and download it now
April 8th, 2011 11:01am

To access an Extended property you first need to define the propety you want to access eg see http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/887c0f4c-d32a-4622-9fb9-e9aee66943aa/

Cheers
Glen

 

April 8th, 2011 11:18am

Thanks for the fast reply!

I have actually looked at that example and it does not help me. Let me explain what I am trying to do:

 

I want to retrieve a contact from a public contacts folder, set an extended property, copy this contact into private contacts folder and then find this contact inside my contacts folder using the extended property.

ExtendedPropertyDefinition propDef = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "NewProp", MapiPropertyType.String);
//method Copy
Contact publicContact = //get public contact
publicContact.SetExtendedProperties(propDef, "my value");
publicContact.Copy(WellKnownFolderName.Contacts);

//method Retrieve
var privateContacts = //get private contacts
foreach (Contact contact in privateContacts)
{
  object answer;
  if (contact.TryGetProperty(propDef, out answer))
  {
     Console.WriteLine("Found my property");
   }
} 

 

 

In the retrieve method I can't find my extended property, but the "TryGetProperty"-method works fine right after I have added the property in the copy method.

Any suggestions to what I am doing wrong?


Free Windows Admin Tool Kit Click here and download it now
April 8th, 2011 12:29pm

Its not just a matter of creating the property whenever you want to use it you need to define and load the property via a propertyset eg

      ExtendedPropertyDefinition propDef = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "NewProp", MapiPropertyType.String);
      PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties){propDef};
      
      foreach (Contact contact in privateContacts)
      {
        contact.Load(psPropSet);
        object answer;
        if (contact.TryGetProperty(propDef, out answer))
        {
          Console.WriteLine("Found my property");
        }
      } 

 To make it more efficient you can just define it to be returned with the finditems query (which means you dont need the load()) or create a searchfilter based on this.

cheers
Glen

April 8th, 2011 1:06pm

Thanks Glen! Saved my day ;)
Free Windows Admin Tool Kit Click here and download it now
April 8th, 2011 2:18pm

Can the finditems query return all items that don't have the extendedproperty set? 

 

For example:

item1 - has extendedProperty
item2 - has extendedProperty
item3 - has extendedProperty
item4 - has extendedProperty
item5 - has no extendedProperty 

Is it possible to get only item5 with the finditems query?

April 20th, 2011 10:06am

Yes you need to write a searchfilter that searches for those items where the property exists then Negate it eg

      SearchFilter sfExist = new SearchFilter.Exists(Extendedprop);
      SearchFilter sfNot = new SearchFilter.Not(sfExist);
cheers<br/>Glen

Free Windows Admin Tool Kit Click here and download it now
April 20th, 2011 1:49pm

Hi, is there an equal code for Visual Basic?

I need to read/edit/set/delete user-defined fields for single contacts using EWS.

Thank you very much,

Michael

March 27th, 2012 3:00pm

In VB.NET you just define and use it the same eg

Dim propDef As ExtendedPropertyDefinition = New ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "MyProp", MapiPropertyType.String)
 Its best to try and write this yourself and if its doesn't work ask questions on about errors or what you dont understand maybe as a starter if you haven't used the Managed API read http://msdn.microsoft.com/en-us/openspecifications/hh546844 and http://blog.iphelion.com/post/2010/11/13/EWS-Managed-API-(VBNET)-Tutorial-Part-1.aspx

 

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
March 28th, 2012 7:17am

Hi Glen, thanks for your supply.

Right, I'm a starter at VB. Acually I'm able to read/edit contacts via EWS (Exchange 2007 / Outlook 2007).

I tried to "translate" the property-part into VB, but it seems that the TryGetProperty-function gets no results - even if there are user-defined fields set in a contact.

Here the code I used:

Dim propDef As New ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "NewProp", MapiPropertyType.String) Dim psPropSet As New PropertySet(BasePropertySet.FirstClassProperties) For Each Contact In ContactsResults Contact.Load(psPropSet) Dim answer As Object answer = "" If (Contact.TryGetProperty(propDef, answer) = True) Then MsgBox("found") End If

Next

>> I'm not sure, if the "translation" is correct

Thanks,

Michael




March 28th, 2012 8:50am

You haven't added the property that you want to retreive to the propertySet eg you need to have

psPropSet.Add(propDef)

before you do the Load.

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
March 28th, 2012 8:55am

Thanks, now I can set and also read the extendedProp of my test-contact. But the property I set is not listed in the user-defined fields ("all fields" in Outlook).

Some other contacts have such user-defined fields (viewable in Outlook), but TryGetProperty-function ignores these contacts. > Is that maybe another contact-property`?

Here the code, using Outlook: (but I need to access it from the Exchange-Server)

prop = CType(Contact.UserProperties.Add("NewProp", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText), Microsoft.Office.Interop.Outlook.UserProperty)

Many thanks,

Michael

March 28th, 2012 9:23am

I seems, that the properties are there, but not displayed. If I create a new field in "all fields" named "NewProp" it contains the value I've set before and if its once created I also see the changes.

So I need to create/activate the field ...

Free Windows Admin Tool Kit Click here and download it now
March 28th, 2012 2:07pm

That won't appear in Outlook as user-defined fields the reason for this is outlined in http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/2a98b4ab-0fbc-4863-8303-48711a18a050/. If you want to access them you need to access them programatiically via the PropertyAccessor.

Cheers
Glen

March 29th, 2012 8:53am

Its not just a matter of creating the property whenever you want to use it you need to define and load the property via a propertyset eg

      ExtendedPropertyDefinition propDef = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "NewProp", MapiPropertyType.String);
      PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties){propDef};
      
      foreach (Contact contact in privateContacts)
      {
        contact.Load(psPropSet);
        object answer;
        if (contact.TryGetProperty(propDef, out answer))
        {
          Console.WriteLine("Found my property");
        }
      } 

 To make it more efficient you can just define it to be returned with the finditems query (which means you dont need the load()) or create a searchfilter based on this.

cheers
Glen

Hey Glen, I see your name on a bunch of these posts and I was wondering if you could expand on the finditems and /or searchfilter. I spent the entire day yesterday trying to get a user-defined field from a contacts list and finally read it with this but the contact.Load(psPropSet); takes far too long for what I need. What Im really needing is to return a list of contacts that meet a search criteria based on this user-defined field, however, if I can just read it I can filter it out after it pulls the list.

Thanks in advanced. 

Free Windows Admin Tool Kit Click here and download it now
July 12th, 2013 1:13pm

To create a Search filter you can use something like

            ExtendedPropertyDefinition propDef = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "NewProp", MapiPropertyType.String);
            PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
            psPropSet.Add(propDef);
            String PropertyValueToSearch = "My Value";
            SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(propDef, PropertyValueToSearch);

The takes into account that you have a value you want an exact match on. Then you can use it like

            PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
            psPropSet.Add(propDef);
            String PropertyValueToSearch = "My Value";
            SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(propDef, PropertyValueToSearch);
            ItemView ivItemView = new ItemView(1000);
            ivItemView.PropertySet = psPropSet;
            FindItemsResults<Item> fiResults;
            do
            {
                fiResults = service.FindItems(WellKnownFolderName.Contacts, sfSearchFilter, ivItemView);
                foreach (Item itItem in fiResults) {
                    Object PropVal = null;
                    if (itItem.TryGetProperty(propDef, out PropVal))
                    {
                        Console.WriteLine(PropVal.ToString());
                    }
                }
                ivItemView.Offset += fiResults.Items.Count;
            } while (fiResults.MoreAvailable);

You should also be able to read the value without the load as long as you define the PropertySet for the ItemVeiw your using in the FindItem operation as above.

Cheers
Glen

July 15th, 2013 3:12am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics