Outlook doesn't have any built in methods to find the owner of an item.  The question was asked if it is possible to find this information when the user is looking at an item in a shared folder.  For example, if a user has delegate permissions to a folder and opens an item from that folder, is it possible to programmatically determine the owner of that item?

The answer lies in PR_STORE_ENTRYID (PidTagStoreEntryId).  This property is detailed in the Exchange Server Protocol Documents, and the definition can be found in MS-OXCDATA section 2.2.4.3.  Part of this property is MailboxDN, and this contains the legacyExchangeDN attribute of the owning mailbox.  It may not be entirely straight-forward, but we now have all the information needed to determine the owner of any item.

Attached is a sample Outlook 2010 add-in (written using Visual Studio 2010) that demonstrates how to find the alias and primary SMTP address of the owner of an item.  Basically, once the MailboxDN is extracted, this is used in an LDAP query to retrieve the other data.  If the item is not in a shared mailbox, then the Outlook Object Model can be used to find the information directly.  This add-in will only work with appointment items (as this is the most common sharing scenario), but the technique should work with all items.

The code that does the work is as follows:


private void RetrieveOwningMailbox()
{
if (_appt == null) return;

string sPrMdbProvider = (string)_appt.PropertyAccessor.BinaryToString(_appt.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x34140102"));

if (sPrMdbProvider == "5494A1C0297F101BA58708002B2A2517") // pbExchangeProviderPrimaryUserGuid
{
try
{
Outlook.ExchangeUser oExchUser = Globals.ThisAddIn.Application.GetNamespace("MAPI").CurrentUser.AddressEntry.GetExchangeUser();
textBoxAlias.Text = oExchUser.Alias;
textBoxPrimarySMTP.Text = oExchUser.PrimarySmtpAddress;
}
catch (Exception ex)
{
textBoxAlias.Text = "Error occurred";
textBoxPrimarySMTP.Text = ex.Message;
}
}
else if (sPrMdbProvider == "9EB4770074E411CE8C5E00AA004254E2") // pbExchangeProviderDelegateGuid
{
// We are going to retrieve the PR_STORE_ENTRYID and retrieve the owner's mailbox DN from that
byte[] bytStoreEntryId = _appt.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FFB0102");

// Find start of mailboxDN
int i = 63;
while ((bytStoreEntryId[i] != 0) && (i<bytStoreEntryId.Length))
i++;
i++;

if (i == bytStoreEntryId.Length)
{
// Failed to find start of mailbox DN
textBoxAlias.Text = "Shared folder: unable to retrieve mailbox CN";
return;
}

byte[] bytMailboxDN = new byte[bytStoreEntryId.Length - i+1];

int j=0;
while ((bytStoreEntryId[i] != 0) && (i<bytStoreEntryId.Length))
{
bytMailboxDN[j] = bytStoreEntryId[i];
i++;
j++;
}
string sMailboxDN = System.Text.ASCIIEncoding.ASCII.GetString(bytMailboxDN,0,j);

RetrieveFromLegacyExchangeDN(sMailboxDN);
}
else
{
textBoxAlias.Text = "Unknown provider";
}
}

private void RetrieveFromLegacyExchangeDN(string LegacyExchDN)
{
// Pull back details given that we have legacy Exchange DN
SearchResult oResult = null;
textBoxPrimarySMTP.Text = "User not found in Active Directory";

try
{
DirectorySearcher oSearcher = new DirectorySearcher();
oSearcher.Filter = "(&(objectClass=user)(legacyExchangeDN=" + LegacyExchDN + "))";
oSearcher.SearchScope = SearchScope.Subtree;
oSearcher.PropertiesToLoad.Add("sAMAccountName");
oSearcher.PropertiesToLoad.Add("mail");
oResult = oSearcher.FindOne();
}
catch (Exception ex)
{
textBoxPrimarySMTP.Text = ex.Message;
}

if (oResult != null)
{
textBoxAlias.Text = oResult.Properties["sAMAccountName"][0].ToString();
textBoxPrimarySMTP.Text = oResult.Properties["mail"][0].ToString();
}
else
{
textBoxAlias.Text = "Error occurred";
}
}