Tips Centre Sunday, May 28, 2017

Home Home | About Us About Us | Contact Us Contact Us

vrPakistanis Technologies - Web Development, Logo Designing, Web Designing

Home > ASP.Net

Code To Get Folder, Find Item, Get Item From Exchange Server 2007



Below is the sample code to get folder, get item and find item from the exchange server 2007:

class EWS_SAMPLE
{
static void Main(string[] args)
{
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new System.Net.NetworkCredential("Username", "Password");
esb.Url = "https://server-name-here/EWS/Exchange.asmx";
BaseFolderType bft = GetFolderByPath(esb, "/Inbox/myFolder");
if (null == bft)
Console.WriteLine("Folder not found.");
else
{
Console.WriteLine("Folder Found: " + bft.DisplayName);
ItemType[] its = FindAllItems(esb, bft.FolderId.Id, DateTime.Parse("07/25/2007 00:00"), DateTime.Parse("07/30/2007 00:00"));
if (null == its)
return;
foreach (ItemType it in its)
{
MessageType mt;
mt = GetItem(esb, it.ItemId.Id) as MessageType;
if (null == mt)
Console.WriteLine("Item not found");
else
Console.WriteLine(mt.DateTimeSent + " >> " + mt.Subject + " >> " + mt.Sender.Item.EmailAddress);
}
}
return;
}
static public BaseFolderType GetFolderByPath(ExchangeServiceBinding esb, string szFolderPath)
{
if (null == szFolderPath)
return null;
if (szFolderPath.IndexOf("/") == -1)
return null;
if (szFolderPath.Substring(0, 1).CompareTo("/") == 0)
szFolderPath = szFolderPath.Substring(1);
string[] Path = szFolderPath.Split('/');
string szParentFolderId = null;
BaseFolderType bft = null;
for (int i = 0; i < Path.GetLength(0); i++)
{
if (null == szParentFolderId)
szParentFolderId = GetRootFolderId(esb);
bft = GetFolder(esb, szParentFolderId, Path[i]);
if (null == bft)
return null;
else
szParentFolderId = bft.FolderId.Id;
}
return bft;
}
static public BaseFolderType GetFolder(ExchangeServiceBinding esb, string szParentFolderId, string szFolderName)
{
if (null == esb || null == szFolderName)
return null;
//get the root folder ID
FolderIdType[] fit = new FolderIdType[1];
fit[0] = new FolderIdType();
fit[0].Id = szParentFolderId;
//set the props that we want to retrieve
FolderResponseShapeType frst = new FolderResponseShapeType();
frst.BaseShape = DefaultShapeNamesType.AllProperties;
//restrict the search on the folder name
PathToUnindexedFieldType ftFolderName = new
PathToUnindexedFieldType();
ftFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
ConstantValueType cvt = new ConstantValueType();
cvt.Value = szFolderName;
FieldURIOrConstantType ctFolderName = new FieldURIOrConstantType();
ctFolderName.Item = cvt;
ContainsExpressionType cet = new ContainsExpressionType();
cet.Constant = cvt;
cet.Item = ftFolderName;
cet.ContainmentComparison = ContainmentComparisonType.IgnoreCase;
cet.ContainmentComparisonSpecified = true;
cet.ContainmentMode = ContainmentModeType.FullString;
cet.ContainmentModeSpecified = true;
RestrictionType rt = new RestrictionType();
rt.Item = cet;
//find the folder
FindFolderType fft = new FindFolderType();
fft.Traversal = FolderQueryTraversalType.Deep;
fft.ParentFolderIds = fit;
fft.FolderShape = frst;
fft.Restriction = rt;
FindFolderResponseType ffrt = esb.FindFolder(fft);
ResponseMessageType rmt =
((ResponseMessageType)ffrt.ResponseMessages.Items[0]);
if (rmt.ResponseClass == ResponseClassType.Success)
{
BaseFolderType[] bfts = ((FindFolderResponseMessageType)ffrt.ResponseMessages.Items[0]).RootFolder.Folders;
if (bfts.GetLength(0) > 0)
return bfts[0];
else
return null;
}
else
return null;
}
static public ItemType GetItem(ExchangeServiceBinding esb, string szItemId)
{
if (null == esb || null == szItemId)
return null;
//specify the content that we want to retrieve, we are retrieving AllProperties
ItemResponseShapeType irst = new ItemResponseShapeType();
irst.BaseShape = DefaultShapeNamesType.AllProperties;
irst.IncludeMimeContent = true;
irst.IncludeMimeContentSpecified = true;
ItemIdType iit = new ItemIdType();
iit.Id = szItemId;
BaseItemIdType[] biit = new BaseItemIdType[1];
biit[0] = iit;
//get the item
GetItemType git = new GetItemType();
git.ItemShape = irst;
git.ItemIds = biit;
GetItemResponseType girt = esb.GetItem(git);
if (girt.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
return ((ItemType)(((ItemInfoResponseMessageType)girt.ResponseMessages.Items[0]).Items.Items[0]));
else
return null;
}
static public string GetRootFolderId(ExchangeServiceBinding esb)
{
if (null == esb)
return null;
DistinguishedFolderIdType[] dfit = new DistinguishedFolderIdType[1];
//get the root folder ID
dfit[0] = new DistinguishedFolderIdType();
dfit[0].Id = DistinguishedFolderIdNameType.root;
//set the props that we want to retrieve
FolderResponseShapeType frst = new FolderResponseShapeType();
frst.BaseShape = DefaultShapeNamesType.AllProperties;
//get the folder
GetFolderType gftRoot = new GetFolderType();
gftRoot.FolderIds = dfit;
gftRoot.FolderShape = frst;
GetFolderResponseType gfrt = esb.GetFolder(gftRoot);
FolderInfoResponseMessageType firmt =
((FolderInfoResponseMessageType)gfrt.ResponseMessages.Items[0]);
if (firmt.ResponseClass == ResponseClassType.Success)
return ((FolderInfoResponseMessageType)gfrt.ResponseMessages.Items[0]).Folders[0].FolderId.Id;
else
return null;
}
static public ItemType[] FindAllItems(ExchangeServiceBinding esb, string strFolderID, DateTime StartDate, DateTime EndDate)
{
if (null == esb || null == strFolderID || "" == strFolderID)
return null;
//get the inbox folder ID
FolderIdType[] fita = new FolderIdType[1];
fita[0] = new FolderIdType();
fita[0].Id = strFolderID;
//request AllProperties
PathToUnindexedFieldType[] ptufta = new PathToUnindexedFieldType[1];
ptufta[0] = new PathToUnindexedFieldType();
ptufta[0].FieldURI = UnindexedFieldURIType.itemItemClass;
ItemResponseShapeType irst = new ItemResponseShapeType();
irst.BaseShape = DefaultShapeNamesType.AllProperties;
irst.IncludeMimeContent = false;
irst.AdditionalProperties = ptufta;
//restrict the returned items to just items of a specific message class
PathToUnindexedFieldType MsgClassField = new
PathToUnindexedFieldType();
MsgClassField.FieldURI = UnindexedFieldURIType.itemItemClass;
ConstantValueType MsgClassToGet = new ConstantValueType();
MsgClassToGet.Value = "IPM.NOTE";
FieldURIOrConstantType MsgClassConstant = new
FieldURIOrConstantType();
MsgClassConstant.Item = MsgClassToGet;
IsEqualToType iett = new IsEqualToType();
iett.FieldURIOrConstant = MsgClassConstant;
iett.Item = MsgClassField;
//restrict the returned items greater than a specified date
PathToUnindexedFieldType StartDateReceivedField = new PathToUnindexedFieldType();
StartDateReceivedField.FieldURI = UnindexedFieldURIType.itemDateTimeReceived;
ConstantValueType StartDateReceivedToGet = new ConstantValueType();
StartDateReceivedToGet.Value = StartDate.ToUniversalTime().ToString();
FieldURIOrConstantType StartDateReceivedConstant = new FieldURIOrConstantType();
StartDateReceivedConstant.Item = StartDateReceivedToGet;
IsGreaterThanOrEqualToType igtett = new IsGreaterThanOrEqualToType();
igtett.FieldURIOrConstant = StartDateReceivedConstant;
igtett.Item = StartDateReceivedField;
//restrict the returned items less than a specified date
PathToUnindexedFieldType EndDateReceivedField = new PathToUnindexedFieldType();
EndDateReceivedField.FieldURI = UnindexedFieldURIType.itemDateTimeReceived;
ConstantValueType EndDateReceivedToGet = new ConstantValueType();
EndDateReceivedToGet.Value = EndDate.ToUniversalTime().ToString();
FieldURIOrConstantType EndDateReceivedConstant = new FieldURIOrConstantType();
EndDateReceivedConstant.Item = EndDateReceivedToGet;
IsLessThanOrEqualToType iltett = new IsLessThanOrEqualToType();
iltett.FieldURIOrConstant = EndDateReceivedConstant;
iltett.Item = EndDateReceivedField;
AndType at = new AndType();
at.Items = new SearchExpressionType[3];
at.Items[0] = igtett;
at.Items[1] = iltett;
//TODO: Uncomment the following line if you want to display only email items from folder
// If the following line is commented then all items would be returned from folder,
// for e.g. Reports, Appointment, Meeting Requests, Contacts etc.
//at.Items[2] = iett;
RestrictionType rt = new RestrictionType();
rt.Item = at;
//find the items
FindItemType fit = new FindItemType();
fit.ItemShape = irst;
fit.ParentFolderIds = fita;
fit.Restriction = rt;
FindItemResponseType firt = esb.FindItem(fit);
return
((ArrayOfRealItemsType)((FindItemResponseMessageType)firt.ResponseMessages
.Items[0]).RootFolder.Item).Items;
}
}


This code has been taken from here.

Tips Centre 2008-2012. All rights reserved. Developed by vrPakistanis Technologies.