I am developing a WPF-C# application and fetching MS Outlook 2010 contact items using Redemption. It is working fine if my Outlook has only one SMTP account. But if I configure another account which is exchange server account then I don't get any contact item from the same code. Following is my code:
Interop.Redemption.RDOItems folderItems = null;
Interop.Redemption.RDOFolder folderContacts = null;
Interop.Redemption.RDOFolder folderSuggestedContacts = null;
List<GMContactItem> allOutlookContacts = null;
object itemObj = null;
List<Interop.Redemption.RDOContactItem> contactItemsList = null;
try
{
folderContacts = (RDOFolder)RDOSessionItem.GetDefaultFolder(Interop.Redemption.rdoDefaultFolders.olFolderContacts);
contactItemsList = new List<RDOContactItem>();
folderItems = folderContacts.Items;
for (int i = 1; folderItems.Count >= i; i++)
{
itemObj = folderItems[i];
if (itemObj is Interop.Redemption.RDOContactItem)
contactItemsList.Add(itemObj as RDOContactItem);
else
Marshal.ReleaseComObject(itemObj);
}
Marshal.ReleaseComObject(folderItems);
folderItems = null;
// getting items from the Suggested Contacts folder in Outlook
folderSuggestedContacts = RDOSessionItem.GetDefaultFolder(
rdoDefaultFolders.olFolderSuggestedContacts);
if (folderSuggestedContacts != null)
{
folderItems = folderSuggestedContacts.Items;
for (int i = 1; folderItems.Count >= i; i++)
{
itemObj = folderItems[i];
if (itemObj is Interop.Redemption.RDOContactItem)
contactItemsList.Add(itemObj as Interop.Redemption.RDOContactItem);
else
Marshal.ReleaseComObject(itemObj);
}
}
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
When I delete my exchange server account then it work fine and if I add exchange server account in Outlook then this code has no exception but don't give any contact item. Can anybody suggest me that what could be the issue here. Thanks in advance.
-Surya
Are you looking at the GAL entries? These address entries exist in GAL (AD based), not in the Contacts folder of the Exchange mailbox.
If you need to open the Contacts folder of the (non-default) PST store, call RDOStore.GetDefaultFolder(olFolderContacts) instead of RDOSession.GetDefaultFolder (which returns a folder from the default store).
The secondary PST store can be opened using the RDOSession.Stores collection.
Related
I'm developing application which uses EWS managed API to send appointments to outlook recipients,
Now there are requirement to add attachments to appointment, I'm able to attach attachments to emails, but when I use same technique as attaching item attachments to email ,but attachments are not attaching, my code as below
public string sendCalanderEvntAsReply( EntityLayer.Data_Contracts.AppointmentDTO appointment)
{
Appointment app = new Appointment(service);
app.Subject = appointment.Subject;
app.Body = appointment.Body;
app.Start = Convert.ToDateTime(appointment.Start);
app.End = Convert.ToDateTime(appointment.End);
app.Location = appointment.Location;
foreach (string obj in appointment.Attendees)
{
app.RequiredAttendees.Add(obj);
}
if (appointment.Attachments != null &&
appointment.Attachments.Count > 0)
{
foreach (var att in appointment.Attachments)
{
app.Attachments.AddFileAttachment(att.FileName);
}
}
app.Save(SendInvitationsMode.SendToAllAndSaveCopy);
}
is there any issue in my code?
please help.
thanks
With EWS when you want to send an Attachment with the Meeting invitation you need to save the appointment first before you send the message else you will only get the attachment on the owners copy so with your code you should use something like
Appointment app = new Appointment(service);
app.Subject = appointment.Subject;
app.Body = appointment.Body;
app.Start = Convert.ToDateTime(appointment.Start);
app.End = Convert.ToDateTime(appointment.End);
app.Location = appointment.Location;
if (appointment.Attachments != null &&
appointment.Attachments.Count > 0)
{
foreach (var att in appointment.Attachments)
{
app.Attachments.AddFileAttachment(att.FileName);
}
}
app.Save(SendInvitationsMode.SendToNone);
foreach (string obj in appointment.Attendees)
{
app.RequiredAttendees.Add(obj);
}
app.Update(ConflictResolutionMode.AutoResolve, SendInvitationsOrCancellationsMode.SendToAllAndSaveCopy);
How can I get list of all available calendars in Outlook?
I have two users logged in, and I can find them with this code:
Outlook.NameSpace session = Globals.ThisAddIn.Application.Session;
Outlook.Accounts accounts = session.Accounts;
Outlook.Account account = null;
List<string> accountList = new List<string>();
for (int i = 1; i <= accounts.Count; i++)
{
account = accounts[i];
if (account != null)
accountList.Add(account.DisplayName);
}
How can I now get list of calendars per user? Or at least get list of all calendars mixed together?
You can use next code for solve this problem:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.NameSpace session = Globals.ThisAddIn.Application.Session;
Outlook.Accounts accounts = session.Accounts;
List<Outlook.MAPIFolder> calendars = new List<Outlook.MAPIFolder>();
foreach (Outlook.Account account in accounts)
{
Outlook.Recipient recipient = session.CreateRecipient(account.DisplayName);
Outlook.MAPIFolder calendar = session.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderCalendar);
if (calendar != null)
{
calendars.Add(calendar);
}
}
}
The strategy of the suggested answer works quite well. I tried a similar solution to get the correct calendar for a specific user, like this:
var session = Application.Session;
var accounts = session.Accounts;
//get default calendar
var calendar = (MSOutlook.Folder)session.GetDefaultFolder(MSOutlook.OlDefaultFolders.olFolderCalendar);
//get calendar for specific user
foreach (MSOutlook.Account account in accounts)
{
if (string.Equals(account.SmtpAddress, "someone#somewhere.com", StringComparison.InvariantCultureIgnoreCase))
{
var recipient = session.CreateRecipient(account.DisplayName);
calendar = (MSOutlook.Folder)session.GetSharedDefaultFolder(recipient,
MSOutlook.OlDefaultFolders.olFolderCalendar);
break;
}
}
Marshal.ReleaseComObject(session);
Marshal.ReleaseComObject(accounts);
Hello i try to write an application for a club that makes it easier for older member to use outlook and to send emails. Just a friendly way so elderly People can easy write and see the stuff on screen. But i am a novice programmer and never used the EWS managed API before. What i want is to Synchronice the Global Address List to my programm so i can assign them to an object and do more stuff. But i dont have any clues anymore.
What i tried:
For my own local Contact List (works)
private void AsignValuetoClass(object sender, RoutedEventArgs e)
{
//For the connection
es.UseDefaultCredentials = true;
es.AutodiscoverUrl("max.mustermann#muster.at", RedirectionUrlValidationCallback);
//How many Contacts are in the folder
ContactsFolder contactsfolder = ContactsFolder.Bind(es, WellKnownFolderName.Contacts);
//To get a specific number of contacts
int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;
//object of the Itemview
ItemView view = new ItemView(numItems);
//return the stuff
FindItemsResults<Item> contactIds = es.FindItems(WellKnownFolderName.Contacts, view);
//loop throug the item
foreach (Item item in contactIds)
{
if (item is Contact)
{
//assign of the contact items
Contact contact = item as Contact;
//new list
List<Contact> testlist = new List<Contact>();
//Add the contacts
testlist.Add(contact);
//loop through contact list
foreach (Contact Liste in testlist)
{
//new object on every run
TestKlasse test = new TestKlasse();
//assign
test.id = Convert.ToString(contact.Id);
test.Vorname = contact.GivenName;
test.Nachname = contact.Surname;
}
Console.WriteLine("Some stupid Text");
}
}
}
To get the contacts from the GAL (dont work).
private void SearchContacts(object sender, EventArgs e)
{
//For the connection
es.UseDefaultCredentials = true;
es.AutodiscoverUrl("max.mustermann#muster.at", RedirectionUrlValidationCallback);
NameResolutionCollection nameResolutions = es.ResolveName(
"Contacts",
ResolveNameSearchLocation.DirectoryThenContacts,
true);
foreach (NameResolution nameResolution in nameResolutions)
{
ExpandGroupResults groupResults = es.ExpandGroup(nameResolution.Mailbox.Address);
foreach (EmailAddress member in groupResults.Members)
{
Console.WriteLine(member.Name + " <" + member.Address + ">");
}
}
}
I tried also the resolvename() stuff but its only for one contact or matching contacts. I need every Contact. Here is the code:
private void SearchContacts(object sender, EventArgs e)
{
//For the connection
es.UseDefaultCredentials = true;
es.AutodiscoverUrl("max.mustermann#muster.at", RedirectionUrlValidationCallback);
// Identify the mailbox folders to search for potential name resolution matches.
List<FolderId> folders = new List<FolderId>() { new FolderId(WellKnownFolderName.Contacts) };
// Search for all contact entries in the default mailbox contacts folder and in Active Directory Domain Services (AD DS). This results in a call to EWS.
NameResolutionCollection coll = es.ResolveName("Anderl", folders, ResolveNameSearchLocation.ContactsThenDirectory, false);
foreach (NameResolution nameRes in coll)
{
Console.WriteLine("Contact name: " + nameRes.Mailbox.Name);
Console.WriteLine("Contact e-mail address: " + nameRes.Mailbox.Address);
Console.WriteLine("Mailbox type: " + nameRes.Mailbox.MailboxType);
}
}
Any help would be great so thx for your time. And sorry for my bad english.
This might come a bit late but one way to overcome the 100 user limit would be to simply append each character of the alphabet to "SMTP:" within a loop like this:
private _exchangeSvc = new ExchangeService();
const string SMTP_PREFIX = "SMTP:";
const string ABC = "abcdefghijklmnopqrstuvwxyz";
public List<NameResolution> GetGAL()
{
var gal = new List<NameResolution>();
foreach (char c in ABC)
{
string ambiguousName = SMTP_PREFIX + c;
var nameResCollection = _exchangeSvc.ResolveName(
ambiguousName,
ResolveNameSearchLocation.DirectoryOnly,
false);
gal.AddRange(nameResCollection);
}
//Uncomment the line below if you find duplicates.
// gal = gal.Distict().ToList()
return gal;
}
This worked for me when I needed the GAL via EWS, I only had to retrieve ~400 users though.
I want to import contacts from Outllok via Mapi.
First step with standard contact is no problem:
MAPIFolder contactObjects =
outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
foreach (ContactItem contactObject in contactObjects.Items) {
... import contact ...
}
In a second step I additionally want to import shared contacts. Only thing I found was using
OpenSharedItem(sharedContacts.vcf)
but I don't know the name of the file (shared item) I want to open.
Does someone know how to access shared contacts and can help me out?
Tobi
Update:
Thanks for the hint with the vcf-Files. But where do I find them?
Update2:
I played around with OutlookSpy. I got access to the folder with shared contacts, but only by knowing the id (which is of course different for other users):
var ns = outlookObj.GetNamespace("MAPI");
var flr = ns.GetFolderFromID("00000000176A90DED92CE6439C1CB89AFE3668F90100D1AD8F66B576B54FB731302D9BB9F6C40007E4BAC5020000");
foreach (var contactObject in flr.Items) {
...
}
How do I get access to the folder without knowing the id?
You will need to either explicitly parse the vCard files or you can use Redemption (I am its author) - it allows to import vCard files using RDOContactItem.Import - http://www.dimastr.com/redemption/RDOMail.htm#methods
Well the solution to the question as it is asked in the title is almost simple.
You just need to call:
Recipient recip = Application.Session.CreateRecipient("Firstname Lastname");
MAPIFolder sharedContactFolder = Application.Session.GetSharedDefaultFolder(recip, OlDefaultFolders.olFolderContacts);
Because this does not solve my problem I will ask another question!
I have done some programming to get contact from outlook.
I am giving you some example code to help you up with this ..It is not excatly want you want but i think this will help you with your problem...
using System.Collections.Generic;
// ...
private List<Outlook.ContactItem> GetListOfContacts(Outlook._Application OutlookApp)
{
List<Outlook.ContactItem> contItemLst = null;
Outlook.Items folderItems =null;
Outlook.MAPIFolder mapiFoldSuggestedConts = null;
Outlook.NameSpace nameSpc = null;
Outlook.MAPIFolder mapiFoldrConts = null;
object itemObj = null;
try
{
contItemLst = new List<Outlook.ContactItem>();
nameSpc = OutlookApp.GetNamespace("MAPI");
// getting items from the Contacts folder in Outlook
mapiFoldrConts =
nameSpc.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
folderItems = mapiFoldrConts.Items;
for (int i = 1; folderItems.Count >= i; i++)
{
itemObj = folderItems[i];
if (itemObj is Outlook.ContactItem)
contItemLst.Add(itemObj as Outlook.ContactItem);
else
Marshal.ReleaseComObject(itemObj);
}
Marshal.ReleaseComObject(folderItems);
folderItems = null;
// getting items from the Suggested Contacts folder in Outlook
mapiFoldSuggestedConts = nameSpc.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderSuggestedContacts);
folderItems = mapiFoldSuggestedConts.Items;
for (int i = 1; folderItems.Count >= i; i++)
{
itemObj = folderItems[i];
if (itemObj is Outlook.ContactItem)
contItemLst.Add(itemObj as Outlook.ContactItem);
else
Marshal.ReleaseComObject(itemObj);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (folderItems != null)
Marshal.ReleaseComObject(folderItems);
if (mapiFoldrConts != null)
Marshal.ReleaseComObject(mapiFoldrConts);
if (mapiFoldSuggestedConts != null)
Marshal.ReleaseComObject(mapiFoldSuggestedConts);
if (nameSpc != null) Marshal.ReleaseComObject(nameSpc);
}
return contItemLst;
}
var outlook = new Microsoft.Office.Interop.Outlook.Application();
NameSpace mapiNamespace = outlook.Application.GetNamespace("MAPI");
foreach (Store store in mapiNamespace.Stores)
{
try
{
var folder = store.GetRootFolder();
foreach(MAPIFolder subfolder in folder.Folders)
{
if ( subfolder.Name == "Inbox")
{
foreach(dynamic message in subfolder.Items.Restrict("[MessageClass]='IPM.Sharing'"))
{
if (message.Class == 104)//SharingItem
{
Folder sharedFolder = message.OpenSharedFolder();
if (sharedFolder.DefaultMessageClass == "IPM.Contact")
{
//this is your folder
}
}
}
}
}
}
catch (System.Exception ex)
{
continue;
}
}
We have a public calendar for our company set up in an Exchange 2007 Public Folder. I am able to retrieve my personal calendar appointments for the current day using the code below. I have searched high and low online and I cannot find one example of someone retrieving calendar information from a Public Folder calendar.
It seems like it should be doable, but I cannot for the life of me get it working. How can I modify the code below to access the calendar? I am not interested in creating any appointments through asp.net, just retrieving a simple list. I am open to any other suggestions as well. Thanks.
ADDED BOUNTY
- I can't be the only person that ever needed to do this. Let's get this problem solved for future generations.
UPDATED AGAIN DUE TO IGNORANCE
- I failed to mention that the project I am working on is .NET 2.0 (very important don't you think?).
* ADDED MY CODE SOLUTION BELOW *
- I have replaced my original code example with the code that ended up working. Many thanks to Oleg for providing the code to find the public folder, which was the hardest part.. I have modified the code using the example from here http://msexchangeteam.com/archive/2009/04/21/451126.aspx to use the simpler FindAppointments method.
This simple example returns an html string with the appointments, but you can use it as a base to customize as needed. You can see our back and forth under his answer below.
using System;
using Microsoft.Exchange.WebServices.Data;
using System.Net;
namespace ExchangePublicFolders
{
public class Program
{
public static FolderId FindPublicFolder(ExchangeService myService, FolderId baseFolderId,
string folderName)
{
FolderView folderView = new FolderView(10, 0);
folderView.OffsetBasePoint = OffsetBasePoint.Beginning;
folderView.PropertySet = new PropertySet(FolderSchema.DisplayName, FolderSchema.Id);
FindFoldersResults folderResults;
do
{
folderResults = myService.FindFolders(baseFolderId, folderView);
foreach (Folder folder in folderResults)
if (String.Compare(folder.DisplayName, folderName, StringComparison.OrdinalIgnoreCase) == 0)
return folder.Id;
if (folderResults.NextPageOffset.HasValue)
folderView.Offset = folderResults.NextPageOffset.Value;
}
while (folderResults.MoreAvailable);
return null;
}
public static string MyTest()
{
ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
myService.Credentials = new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
myService.Url = new Uri("https://MAILSERVER/ews/exchange.asmx");
Folder myPublicFoldersRoot = Folder.Bind(myService, WellKnownFolderName.PublicFoldersRoot);
string myPublicFolderPath = #"PUBLIC_FOLDER_CALENDAR_NAME";
string[] folderPath = myPublicFolderPath.Split('\\');
FolderId fId = myPublicFoldersRoot.Id;
foreach (string subFolderName in folderPath)
{
fId = Program.FindPublicFolder(myService, fId, subFolderName);
if (fId == null)
{
return string.Format("ERROR: Can't find public folder {0}", myPublicFolderPath);
}
}
Folder folderFound = Folder.Bind(myService, fId);
if (String.Compare(folderFound.FolderClass, "IPF.Appointment", StringComparison.Ordinal) != 0)
{
return string.Format("ERROR: Public folder {0} is not a Calendar", myPublicFolderPath);
}
CalendarFolder AK_Calendar = CalendarFolder.Bind(myService, fId, BasePropertySet.FirstClassProperties);
FindItemsResults<Appointment> AK_appointments = AK_Calendar.FindAppointments(new CalendarView(DateTime.Now,DateTime.Now.AddDays(1)));
string rString = string.Empty;
foreach (Appointment AK_appoint in AK_appointments)
{
rString += string.Format("Subject: {0}<br />Date: {1}<br /><br />", AK_appoint.Subject, AK_appoint.Start);
}
return rString;
}
}
}
Like promised here is a code example. I used the Microsoft Exchange Web Services (EWS) Managed API 1.0 and recommend you to do the same. The most comments I included in the code
using System;
using Microsoft.Exchange.WebServices.Data;
using System.Net;
namespace ExchangePublicFolders {
class Program {
static FolderId FindPublicFolder (ExchangeService myService, FolderId baseFolderId,
string folderName) {
// We will search using paging. We will use page size 10
FolderView folderView = new FolderView (10,0);
folderView.OffsetBasePoint = OffsetBasePoint.Beginning;
// we will need only DisplayName and Id of every folder
// se we'll reduce the property set to the properties
folderView.PropertySet = new PropertySet (FolderSchema.DisplayName,
FolderSchema.Id);
FindFoldersResults folderResults;
do {
folderResults = myService.FindFolders (baseFolderId, folderView);
foreach (Folder folder in folderResults)
if (String.Compare (folder.DisplayName, folderName, StringComparison.OrdinalIgnoreCase) == 0)
return folder.Id;
if (folderResults.NextPageOffset.HasValue)
// go to the next page
folderView.Offset = folderResults.NextPageOffset.Value;
}
while (folderResults.MoreAvailable);
return null;
}
static void MyTest () {
// IMPORTANT: ExchangeService is NOT thread safe, so one should create an instance of
// ExchangeService whenever one needs it.
ExchangeService myService = new ExchangeService (ExchangeVersion.Exchange2007_SP1);
myService.Credentials = new NetworkCredential ("MyUser#corp.local", "myPassword00");
myService.Url = new Uri ("http://mailwebsvc-t.services.local/ews/exchange.asmx");
// next line is very practical during development phase or for debugging
myService.TraceEnabled = true;
Folder myPublicFoldersRoot = Folder.Bind (myService, WellKnownFolderName.PublicFoldersRoot);
string myPublicFolderPath = #"OK soft GmbH (DE)\Gruppenpostfächer\_Template - Gruppenpostfach\_Template - Kalender";
string[] folderPath = myPublicFolderPath.Split('\\');
FolderId fId = myPublicFoldersRoot.Id;
foreach (string subFolderName in folderPath) {
fId = FindPublicFolder (myService, fId, subFolderName);
if (fId == null) {
Console.WriteLine ("ERROR: Can't find public folder {0}", myPublicFolderPath);
return;
}
}
// verify that we found
Folder folderFound = Folder.Bind (myService, fId);
if (String.Compare (folderFound.FolderClass, "IPF.Appointment", StringComparison.Ordinal) != 0) {
Console.WriteLine ("ERROR: Public folder {0} is not a Calendar", myPublicFolderPath);
return;
}
CalendarFolder myPublicFolder = CalendarFolder.Bind (myService,
//WellKnownFolderName.Calendar,
fId,
PropertySet.FirstClassProperties);
if (myPublicFolder.TotalCount == 0) {
Console.WriteLine ("Warning: Public folder {0} has no appointment. We try to create one.", myPublicFolderPath);
Appointment app = new Appointment (myService);
app.Subject = "Writing a code example";
app.Start = new DateTime (2010, 9, 9);
app.End = new DateTime (2010, 9, 10);
app.RequiredAttendees.Add ("oleg.kiriljuk#ok-soft-gmbh.com");
app.Culture = "de-DE";
app.Save (myPublicFolder.Id, SendInvitationsMode.SendToNone);
}
// We will search using paging. We will use page size 10
ItemView viewCalendar = new ItemView (10);
// we can include all properties which we need in the view
// If we comment the next line then ALL properties will be
// read from the server. We can see there in the debug output
viewCalendar.PropertySet = new PropertySet (ItemSchema.Subject);
viewCalendar.Offset = 0;
viewCalendar.OffsetBasePoint = OffsetBasePoint.Beginning;
viewCalendar.OrderBy.Add (ContactSchema.DateTimeCreated, SortDirection.Descending);
FindItemsResults<Item> findResultsCalendar;
do {
findResultsCalendar = myPublicFolder.FindItems (viewCalendar);
foreach (Item item in findResultsCalendar) {
if (item is Appointment) {
Appointment appoint = item as Appointment;
Console.WriteLine ("Subject: \"{0}\"", appoint.Subject);
}
}
if (findResultsCalendar.NextPageOffset.HasValue)
// go to the next page
viewCalendar.Offset = findResultsCalendar.NextPageOffset.Value;
}
while (findResultsCalendar.MoreAvailable);
}
static void Main (string[] args) {
MyTest();
}
}
}
You should update the string myPublicFolderPath to the value with your public calender folder. I set myService.TraceEnabled = true which produce long output with debug information. You should of cause remove the line for production.
UPDATED: Some additional links you could find in Create new calendar system support in Exchange OWA. If you not yet seen the videos and you want to use Exchange Web Services I would recommend you to watch there. It could save your time in the future.
I did similar thing for Exchange 2003, but using WebDAV Search method (http://msdn.microsoft.com/en-us/library/aa143053%28v=EXCHG.65%29.aspx).
http://geekswithblogs.net/cskardon/archive/2008/12/01/hunting-those-elusive-public-folders-using-exchange-web-services-part.aspx may help.