Last active
August 29, 2015 14:24
-
-
Save vbfox/1cec0518f92aed77e1f4 to your computer and use it in GitHub Desktop.
Utilities for Exchange Webservice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class EwsExtensions | |
{ | |
public static string GetFullPath(this Folder folder, string separator = "/") | |
{ | |
var folderPathProperty = new ExtendedPropertyDefinition(26293, MapiPropertyType.String); | |
var folderPathPropertySet = new PropertySet(BasePropertySet.FirstClassProperties) { folderPathProperty }; | |
var folderWithFullnameProperty = Folder.Bind(folder.Service, folder.Id, folderPathPropertySet); | |
object pathObj = null; | |
if (!folderWithFullnameProperty.TryGetProperty(folderPathProperty, out pathObj)) | |
{ | |
return folder.DisplayName; | |
} | |
var path = (string)pathObj; | |
return path.Replace("\uFFFE", separator); | |
} | |
public static IEnumerable<TItem> ViewToEnumerable<TItem, TView>(Func<int, TView> createView, Func<TView, Tuple<Collection<TItem>, int?, int>> getViewResults, | |
int viewSize = 1000, IProgress<Tuple<int,int>> progress = null) | |
where TView : PagedView | |
{ | |
var view = createView(viewSize); | |
Tuple<Collection<TItem>, int?, int> results; | |
int received = 0; | |
do | |
{ | |
results = getViewResults(view); | |
received += results.Item1.Count; | |
if (progress != null) | |
{ | |
progress.Report(Tuple.Create(received, results.Item3)); | |
} | |
foreach(var item in results.Item1) | |
{ | |
yield return item; | |
} | |
if (results.Item2 != null) | |
{ | |
view.Offset = results.Item2.Value; | |
} | |
} while(results.Item2.HasValue && results.Item2 > 0); | |
} | |
public static IEnumerable<Folder> FindFolders(this Folder parent, IProgress<Tuple<int,int>> progress = null) | |
{ | |
return ViewToEnumerable( | |
pageSize => new FolderView(pageSize), | |
view => | |
{ | |
var result = parent.FindFolders(view); | |
return Tuple.Create(result.Folders, result.NextPageOffset, result.TotalCount); | |
}, progress: progress); | |
} | |
public static IEnumerable<Folder> GetAllSubFoldersAndSelf(this Folder parent) | |
{ | |
var queue = new Queue<Folder>(); | |
queue.Enqueue(parent); | |
while(queue.Count > 0) | |
{ | |
var item = queue.Dequeue(); | |
yield return item; | |
foreach(var child in item.FindFolders()) | |
{ | |
queue.Enqueue(child); | |
} | |
} | |
} | |
public static Folder GetSubFolder(this Folder parent, string displayName) | |
{ | |
return parent.FindFolders().First(f => f.DisplayName == displayName); | |
} | |
public static IEnumerable<Item> FindItems(this Folder folder, SearchFilter filter, IProgress<Tuple<int,int>> progress = null) | |
{ | |
return ViewToEnumerable( | |
pageSize => new ItemView(pageSize), | |
view => | |
{ | |
var result = folder.Service.FindItems(folder.Id, filter, view); | |
return Tuple.Create(result.Items, result.NextPageOffset, result.TotalCount); | |
}, progress: progress); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment