Search code examples
c#exchangewebservicescontactsexchange-server-2010

Accessing Shared Contacts with EWS Managed API (Exchange 2010)


I'm currently working on a project where I need to get Information from shared contacts (name, mailadress, etc) to compare multiple shared folders and find duplicate entries.

I'm coding in Visual Studio 2015. The program is just a console Application that creates an excel sheet, where the duplicate contacts are marked.

The Exchange Server is 2010 SP1.

The contacts are shared via E-Mail.

I have no problem to get acces to my own contacts. But I can't get to the folders that are shared to me.

I tried to follow this Guide. But unfortunately this line gives me a nullpointer exception.

NameResolutionCollection ncCol = service.ResolveName(lnLegDN, ResolveNameSearchLocation.DirectoryOnly, true);
                        if (ncCol.Count > 0)

And further on I don't really understand what this part is supposed to to anyways, so I can't really fix it. It's my first time working with the EWS Managed API.

After changing the code I am able to get acces to the contacts folder from another mailbox. But that is not the folder that is shared to me.

So my question is, how can I get a contactsfolder that is named "CheckForDuplicates" for example. Is there a way?

Here is the code I changed in the above example. I'm getting the name of the mail box owner and than look for their contacts. After that I call a routine that gets the needed information from all contacts within that folder.

 foreach (Item itItem in fiResults.Items)
                {
                    object WlinkStoreEntryId = null;
                    if (itItem.TryGetProperty(PidTagWlinkStoreEntryId, out WlinkStoreEntryId))
                    {
                        string lookup = itItem.Subject;
                        string[] lookupArr = lookup.Trim().Split(new Char[] {'-'});
                        NameResolutionCollection ncCol = service.ResolveName(lookupArr[0], ResolveNameSearchLocation.DirectoryOnly, true);
                        if (ncCol.Count > 0)
                        {
                            try
                            {
                                Console.WriteLine(itItem.Subject);
                                FolderId SharedContactsId = new FolderId(WellKnownFolderName.Contacts, ncCol[0].Mailbox.Address);
                                Folder SharedContactFolder = Folder.Bind(service, SharedContactsId);
                                rtList.Add(ncCol[0].Mailbox.Address, SharedContactFolder);
                            }
                            catch (Exception exception)
                            {
                                Console.WriteLine(exception.Message);
                            }
                        }

                    }

                }

Solution

  • I found the solution. And it's way easier than expected.

    I realized that I should look for Folders in the WellKnownFolderName.Root and not in WellKnownFolderName.Contacts. Because the WellKnownFolderName.Contacts is reserved for the Contacts-Folder. And not like I thought for all contactfolders.

    FolderId sharedRootFolderid = new FolderId(WellKnownFolderName.Root, ncCol[0].Mailbox.Address);
    FolderView sharedFolderView = new FolderView(1000);
    sharedFolderView .PropertySet = new PropertySet(BasePropertySet.IdOnly);
    sharedFolderView .PropertySet.Add(FolderSchema.DisplayName);
    sharedFolderView .Traversal = FolderTraversal.Deep;
    SearchFilter testSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "CheckForDuplicates");
    FindFoldersResults sharedContacts = service.FindFolders(sharedRootFolderid , testSearchFilter, sharedFolderView);
    

    And that's already it.