Let me tell you,I am not an expert on integrating Outlook calendar in .NET application.Recently,In one of my project.I have come across integrating outlook in our application.So,Thought of sharing the snippet of code with you.That may save a piece of your time.
The below code,will explian you how to integrate the outlook by EWS method.Exchange Web Services (EWS) for Microsoft Exchange 2007 provides a quick an easy way to interface with Exchange data with your custom application.The Exchange Web Services module works on the concept of creating a soap message and sending it to the Exchange that will tell the server what you’re looking for and then give you back a soap response.
Right click on your ASP.NET application and choose add web reference option.
http://<YourExchangeServer>/EWS/Services.wsdl
The below snippet of code reads the Outlook appoitnments.
Just to keep the code clean and make it reusable,I kept the exchage server details in config file.
Ex :-
Settings from web.config file
<add key="ExServer" value="http://<YourExchangeServer>/EWS/Exchange.asmx"/>
The below code reads the appointments fom the exchange server.
string username = "Username";
string password = "Password";
string email = "Username@example.com";
DateTime startdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime temp = new DateTime(startdate.Year, startdate.Month + 1, 1);
DateTime enddate = temp.AddDays(-1);
private void OutlookCalendar(string username, string password, DateTime startdate, DateTime enddate, string outlook_email)
{
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Url = Convert.ToString(ConfigurationManager.AppSettings["ExServer"]);
esb.Credentials = new NetworkCredential(username, password, Convert.ToString(ConfigurationManager.AppSettings["domain"]));
// Form the FindItem request.
FindItemType findItemRequest = new FindItemType();
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = startdate;//DateTime.Now.AddDays(-1);
calendarView.EndDate = enddate; //DateTime.Now.AddDays(1);
calendarView.MaxEntriesReturned = 100;
calendarView.MaxEntriesReturnedSpecified = true;
findItemRequest.Item = calendarView;
// Define which item properties are returned in the response.
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
//itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties;
DistinguishedFolderIdType[] folderIDArray =
new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;
folderIDArray[0].Mailbox = new EmailAddressType();
folderIDArray[0].Mailbox.EmailAddress = outlook_email;
//}
findItemRequest.ParentFolderIds = folderIDArray;
// Define the traversal type.
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
try
{
// Send the FindItem request and get the response.
FindItemResponseType findItemResponse =
esb.FindItem(findItemRequest);
// Access the response message.
ArrayOfResponseMessagesType responseMessages =
findItemResponse.ResponseMessages;
ResponseMessageType[] rmta = responseMessages.Items;
int folderNumber = 0;
foreach (ResponseMessageType rmt in rmta)
{
// One FindItemResponseMessageType per folder searched.
FindItemResponseMessageType firmt =
rmt as FindItemResponseMessageType;
if (firmt.RootFolder == null)
continue;
FindItemParentType fipt = firmt.RootFolder;
object obj = fipt.Item;
// FindItem contains an array of items.
if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items =
(obj as ArrayOfRealItemsType);
if (items.Items == null)
{
folderNumber++;
}
else
{
foreach (ItemType it in items.Items)
{
if (it is CalendarItemType)
{
CalendarItemType cal = (CalendarItemType)it;
CalendarInfo ce = new CalendarInfo();
ce.Location = cal.Location;
ce.StartTime = cal.Start;
ce.EndTime = cal.End;
ce.Subject = cal.Subject;
ce.Body = GetMeetingBody(esb, cal);
_calendarEvents.Add(ce);
}
}
folderNumber++;
}
}
}
}
catch (Exception e)
{
throw;
}
finally
{
}
}
References-
1. http://blogs.visoftinc.com/2008/03/20/Using-Exchange-Web-Services-2007-The-Basics/
2. http://weblogs.asp.net/psperanza/archive/2008/03/18/getting-calendar-items-using-exchange-web-services.aspx
The below code,will explian you how to integrate the outlook by EWS method.Exchange Web Services (EWS) for Microsoft Exchange 2007 provides a quick an easy way to interface with Exchange data with your custom application.The Exchange Web Services module works on the concept of creating a soap message and sending it to the Exchange that will tell the server what you’re looking for and then give you back a soap response.
Right click on your ASP.NET application and choose add web reference option.
http://<YourExchangeServer>/EWS/Services.wsdl
The below snippet of code reads the Outlook appoitnments.
Just to keep the code clean and make it reusable,I kept the exchage server details in config file.
Ex :-
Settings from web.config file
<add key="ExServer" value="http://<YourExchangeServer>/EWS/Exchange.asmx"/>
The below code reads the appointments fom the exchange server.
string username = "Username";
string password = "Password";
string email = "Username@example.com";
DateTime startdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime temp = new DateTime(startdate.Year, startdate.Month + 1, 1);
DateTime enddate = temp.AddDays(-1);
private void OutlookCalendar(string username, string password, DateTime startdate, DateTime enddate, string outlook_email)
{
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Url = Convert.ToString(ConfigurationManager.AppSettings["ExServer"]);
esb.Credentials = new NetworkCredential(username, password, Convert.ToString(ConfigurationManager.AppSettings["domain"]));
// Form the FindItem request.
FindItemType findItemRequest = new FindItemType();
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = startdate;//DateTime.Now.AddDays(-1);
calendarView.EndDate = enddate; //DateTime.Now.AddDays(1);
calendarView.MaxEntriesReturned = 100;
calendarView.MaxEntriesReturnedSpecified = true;
findItemRequest.Item = calendarView;
// Define which item properties are returned in the response.
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
//itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties;
DistinguishedFolderIdType[] folderIDArray =
new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;
folderIDArray[0].Mailbox = new EmailAddressType();
folderIDArray[0].Mailbox.EmailAddress = outlook_email;
//}
findItemRequest.ParentFolderIds = folderIDArray;
// Define the traversal type.
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
try
{
// Send the FindItem request and get the response.
FindItemResponseType findItemResponse =
esb.FindItem(findItemRequest);
// Access the response message.
ArrayOfResponseMessagesType responseMessages =
findItemResponse.ResponseMessages;
ResponseMessageType[] rmta = responseMessages.Items;
int folderNumber = 0;
foreach (ResponseMessageType rmt in rmta)
{
// One FindItemResponseMessageType per folder searched.
FindItemResponseMessageType firmt =
rmt as FindItemResponseMessageType;
if (firmt.RootFolder == null)
continue;
FindItemParentType fipt = firmt.RootFolder;
object obj = fipt.Item;
// FindItem contains an array of items.
if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items =
(obj as ArrayOfRealItemsType);
if (items.Items == null)
{
folderNumber++;
}
else
{
foreach (ItemType it in items.Items)
{
if (it is CalendarItemType)
{
CalendarItemType cal = (CalendarItemType)it;
CalendarInfo ce = new CalendarInfo();
ce.Location = cal.Location;
ce.StartTime = cal.Start;
ce.EndTime = cal.End;
ce.Subject = cal.Subject;
ce.Body = GetMeetingBody(esb, cal);
_calendarEvents.Add(ce);
}
}
folderNumber++;
}
}
}
}
catch (Exception e)
{
throw;
}
finally
{
}
}
References-
1. http://blogs.visoftinc.com/2008/03/20/Using-Exchange-Web-Services-2007-The-Basics/
2. http://weblogs.asp.net/psperanza/archive/2008/03/18/getting-calendar-items-using-exchange-web-services.aspx
No comments:
Post a Comment