0
ExchangeService service = this.GetService();
FolderId folderID = GetPublicFolderID(service, "My Address Book");
ContactsFolder folder = ContactsFolder.Bind(service, folderID);
int folderCount = folder.TotalCount;

var guid       = DefaultExtendedPropertySet.PublicStrings;
var epdCP      = new ExtendedPropertyDefinition(guid, "CustomProp", MapiPropertyType.Boolean);
var epdAccount = new ExtendedPropertyDefinition(guid, "Account", MapiPropertyType.String);
var epdCID     = new ExtendedPropertyDefinition(guid, "CustomerID", MapiPropertyType.Integer);

var view = new ItemView(folderCount);
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
view.PropertySet.Add(epdCP);
view.PropertySet.Add(epdAccount);
view.PropertySet.Add(epdCID);
var contacts = service.FindItems(folderID, view);

foreach (Contact contact in contacts)
{
    bool CP;
    string Account;
    int  CID;

    contact.GetLoadedPropertyDefinitions();
    contact.TryGetProperty(epdCP, out CP);
    contact.TryGetProperty(epdAccuont, out Account);
    contact.TryGetProperty(epdCID, out CID);

    Console.WriteLine(String.Format("{0, -20} - {1} - {2}"
                                    , contact.DisplayName
                                    , contact.EmailAddresses[EmailAddressKey.EmailAddress1]
                                    , CP
                                    , Account
                                    , CID
                            ));
}

Goal is to get the Contact information out of a Public Address Book so I can sync it with another program we have.

For each Contact in the Public Address Book, this prints out DisplayName, EmailAddress and my Custom Property. No issues there.

The problem I have is I can't seem to get the right incantation to pull certain properties. CustomerID and Account are two examples that I can't seem to get to pull/print. They aren't "Custom", in as much as I haven't created them.

How can I get CustomerID and Account out of a contact via EWS?

WernerCD
  • 2,137
  • 6
  • 31
  • 51

1 Answers1

0

The GUID isn't needed for the Account and CustomerID.. and the name needs to be the Unique ID:

var guid       = DefaultExtendedPropertySet.PublicStrings;
var epdAccount = new ExtendedPropertyDefinition(0x3A00, MapiPropertyType.String);
var epdCID     = new ExtendedPropertyDefinition(0x3A4A, MapiPropertyType.String);
var epdCP      = new ExtendedPropertyDefinition(guid, "CustomBln", MapiPropertyType.Boolean);
var epdCI      = new ExtendedPropertyDefinition(guid, "CustomInt", MapiPropertyType.Integer);

Now if only I can figure out why MapiPropertyType.Integer isn't mapping correctly.... Custom Boolean pulls properly, but Custom Integers aren't.

edit: Found that double works, where Integer doesn't.

var epdCI      = new ExtendedPropertyDefinition(guid, "CustomInt", MapiPropertyType.Double);

Edited Example:

ExchangeService service = this.GetService();
FolderId folderID = GetPublicFolderID(service, "My Address Book");
ContactsFolder folder = ContactsFolder.Bind(service, folderID);
int folderCount = folder.TotalCount;

var guid       = DefaultExtendedPropertySet.PublicStrings;
var epdAccount = new ExtendedPropertyDefinition(0x3A00, MapiPropertyType.String);
var epdCID     = new ExtendedPropertyDefinition(0x3A4A, MapiPropertyType.String);
var epdCBLN    = new ExtendedPropertyDefinition(guid, "CustomBln", MapiPropertyType.Boolean);
var epdCDBL    = new ExtendedPropertyDefinition(guid, "CustomDbl", MapiPropertyType.Double);

var view = new ItemView(folderCount);
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
view.PropertySet.Add(epdAccount);
view.PropertySet.Add(epdCID);
view.PropertySet.Add(epdCBLN);
view.PropertySet.Add(epdCDBL);

//var searchOrFilterCollection = new List<SearchFilter>();
//searchOrFilterCollection.Add(new SearchFilter.IsEqualTo(epdCBLN, true));
//searchOrFilterCollection.Add(new SearchFilter.IsEqualTo(epdAccount, "user"));
//var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchOrFilterCollection);
var filter = new SearchFilter.IsEqualTo(epdAccount, "user");

var contacts = service.FindItems(folderID, filter, view);

foreach (Contact contact in contacts)
{
    string Account;
    int  CID;
    bool CBLN;
    double CDBL;

    contact.GetLoadedPropertyDefinitions();
    contact.TryGetProperty(epdAccuont, out Account);
    contact.TryGetProperty(epdCID, out CID);
    contact.TryGetProperty(epdCBLN, out CBLN);
    contact.TryGetProperty(epdCDBL, out CDBL);

    Console.WriteLine(String.Format("{0, -20} - {1} - {2} - {3} - {4}"
                    , contact.DisplayName
                    , contact.EmailAddresses[EmailAddressKey.EmailAddress1]
                    , Account
                    , CID
                    , CBLN
                    , CDBL
            ));
}
WernerCD
  • 2,137
  • 6
  • 31
  • 51