by Iphelion - Andy
22. May 2011 10:56
Meed to get at an item's extended properties in Exchange 2007/2010 using .NET? well here's how...
During a recent engagement a requirement to access extended properties of an exchange item arose when using Exchange 2007 SP1. I thought I would share with you the method I used to retrieve these properties in case it is something you ever need to do.
In the example below, we will be searching for extended properties for an exchange contact item (to be specific title - i.e. Mr/Mrs/Dr/Professor) which is not returned as part of the properties when a search is performed.
I have put all the code in a method you can put behind a button in a windows forms application to test, it obviously requires a reference to the EWS API DLLs and you will need an account that has permission to connect and do relevant searches. The code is commented to help you through step by step and there is a reference at the end containing a list of all extended properties that you should be able to retrieve from exchange.. enjoy..
// first place some includes at the top of your class or form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Dns;
public void GetContacts()
{
// first declare the exchange object
private ExchangeService exch = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
// now we have to create a definition for the extended property we want to return
ExtendedPropertyDefinition titleProperty = new ExtendedPropertyDefinition(0x3A45, MapiPropertyType.String);
// specify your exchange server
exch.Url = new Uri("https://<yourserver>/EWS/Exchange.aspx");
// specify credentials to use when accessing exchange if not the logged in user
exch.UseDefaultCredentials = false;
exch.Credentials = new NetworkCredential("<USERNAME>", "<PASSWORD>", "<DOMAIN>");
// now we need to create a property set which contains a list of properties we wish to retrieve from exchange
// above and beyond the first-class properties (i.e. the pre-defined properties on the object)
PropertySet ps = new PropertySet(BasePropertySet.FirstClassProperties) { titleProperty };
// next we define the folder we wish to search for items - in this case the contacts folder
FolderId folder = WellKnownFolderName.Contacts;
// now we need an ItemView object to store the results of the search we plan to do and we also need
// to define the propertyset of this view to our previously created PropertySet object to tell the
// API that we want to bring back additional properties in this view when the search is executed
FindItemsResults<Item> result = new ItemView(1000);
itemview.PropertySet = ps;
// Next we want to load the additional properties in the result set according to our PropertySet definition
// which is done by calling the LoadPropertiesForItems method on the exchange connector object
exch.LoadPropertiesForItems(result, ps);
// if we have some results ...
if (result.TotalCount > 0)
{
// loop through each item returned from the search
foreach (Item i in result)
{
// cast the item to a contact objec to get at the first-class properties.
Contact c = (Contact)i;
// lets set up a string to hold our title value inside the loop
string titleValue = string.empty;
// now we use the items TryGetProperty method passing in our property definition and our
// the variable we would like to populate if there is a value to be returned
i.TryGetProperty(titleProperty, titleValue);
// if titlevalue is not null then display it in a messagebox, otherwise show a blank messagebox
MessageBox.Show(titleValue != null ? titleValue : String.Empty);
}
}
// indicate the end of the loop to the user
MessageBox.Show("Complete!");
}
Extended properties ref: http://msdn.microsoft.com/en-us/library/gg274394(v=exchg.80).aspx
by Iphelion - Andy
4. February 2011 11:54
It might sound like a simple thing, and when you look at the code it really is, but finding this tiny little snippet has taken absolutley ages to track down. Anyway, Big big big thanks to Mostafa Elzoghbi and his blog entry which can be found here…
ARTICLE HERE
And, for the impatient, the code is basically...
// Use this to find the webbrowser control
Office.CommandBarComboBox webControl1 = (Office.CommandBarComboBox)
this.Application.ActiveExplorer().CommandBars.FindControl(26, 1740, missing, missing);
// use this to navigate to whatever site you want to
webControl1.Text = "http://www.iphelion.com";
And thats about it!