In order to Synchronize events in an Outlook calendar Microsoft has provided the following documentation: https://msdn.microsoft.com/en-us/office/office365/howto/sync-calendar-view
To do that with C# .NET, they have provided the following Nuget package https://www.nuget.org/packages/Microsoft.Office365.OutlookServices-V2.0/
To play with that dll they have provided this basic sample: https://dev.outlook.com/restapi/tutorial/dotnet
Following that sample I have written the following code to get events:
OutlookServicesClient client =
new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"),
() => GetAccessToken(account));
ExceptionDispatchInfo unauthorizedException = null;
try
{
client.Context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(
(sender, e) => InsertXCustomHeaders(sender, e, account));
client.Context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(
(sender, e) => InsertSyncCustomHeaders(sender, e));
DateTime startDate = //Today
DateTime endDate = // 2 years from now;
IPagedCollection<IEvent> events = await client.Me.Calendars[calendarID].GetCalendarView(startDate, endDate).ExecuteAsync();
return events;
}
catch (WebException exception)
{
}
Source code of InsertXCustomHeaders and InsertSyncCustomHeaders used above:
private static void InsertXCustomHeaders(object sender, SendingRequest2EventArgs e, UserConnectedAccount connectedAccount)
{
e.RequestMessage.SetHeader("Authorization", string.Format("Bearer {0}", connectedAccount.AccessToken));
e.RequestMessage.SetHeader("UserAgent", GetUserAgent());
e.RequestMessage.SetHeader("client-request-id", "Bearer " + GetClientRequestID());
e.RequestMessage.SetHeader("return-client-request-id", "true");
}
private static void InsertSyncCustomHeaders(object sender, SendingRequest2EventArgs e)
{
int initialSyncMaxPageSize = DynamicPropertyRepository.GetSystemPropertyInt("InitialSyncMaxPageSize", 100);
e.RequestMessage.SetHeader("Prefer", "odata.track-changes");
e.RequestMessage.SetHeader("Prefer", string.Format("odata.maxpagesize = {0}", initialSyncMaxPageSize));
}
I have no issue getting events with that code, i.e. I am getting events in
IPagedCollection events variable above.
Issue I am facing:
I am unable to find how to get deltaToken, slipToken nextLink values as mentioned in the response at this link https://msdn.microsoft.com/en-us/office/office365/howto/sync-calendar-view with the above c# code.
Is Syncing of events even supported with C# .NET Nuget package: https://www.nuget.org/packages/Microsoft.Office365.OutlookServices-V2.0/?
Is there any sample to perform https://msdn.microsoft.com/en-us/office/office365/howto/sync-calendar-view with https://www.nuget.org/packages/Microsoft.Office365.OutlookServices-V2.0/ library?
First thing about that library: it isn't going to be maintained going forward, so keep in mind that new features and such won't get added to it. That's what you're seeing here, the library wasn't updated after sync support was added to the REST API.
Because of this, while you can force the headers into the request to enable sync behavior, the IPagedCollection doesn't expose anything that will help you do subsequent sync events, like deltaToken. For sync, you are going to want to move to something else. I've got a sample that doesn't use any library. Instead it's all custom code, so you have complete control over requests and responses.
Related
I've been writing some code that pulls data from an external source. Essentially first it gets a list of the events that relate to today, processes it, then gets a list of events that relate to tomorrow. Depending upon how many events there are in a given day the processing can take a number of hours. Here is the problem:
If I run ScrapeToday() and ScrapeTomorrow() immediately after one another without the processing everything is gravy. However, as in the normal program flow, if there is a large gap between the operations I catch the following error;
The request was aborted: Could not create SSL/TLS secure channel.
My first instinct is that this must be due to a bug in HttpClient, likely something expiring due to the long duration between requests. However as a new client is created for each request I wouldn't've thought it possible. I've done some digging around on SO and some other sites but have not been able to reach the root of the problem - any help would be appreciated!
Code below.
public static async Task<Dictionary<string, Calendar.Event>> ScrapeToday()
{
try
{
var client = new HttpClient();
WriteLine("Requesting today's calendar...");
var json = await client.GetStringAsync(calendarTodayUrl);
var results = JsonConvert.DeserializeObject<Dictionary<string, Calendar.Event>>(json);
return results;
}
catch (Exception e)
{
Scraper.Instance.WriteLine("Error retrieving today's calendar: " + e);
return new Dictionary<string, Calendar.Event>();
}
}
public static async Task<Dictionary<string, Calendar.Event>> ScrapeTomorrow()
{
try {
var client = new HttpClient();
WriteLine("Requesting tomorrow's calendar...");
var json = await client.GetStringAsync(calendarTomorrowUrl);
var results = JsonConvert.DeserializeObject<Dictionary<string, Calendar.Event>>(json);
return results;
}
catch (Exception e)
{
Scraper.Instance.WriteLine("Error retrieving tomorrow's calendar: " + e);
return new Dictionary<string, Calendar.Event>();
}
}
Edit: Since posting I have tried making a global httpclient and using that for both requests. That also did not work.
Edit2: The bug is reproduceable with an elapsed time of 30 minutes between the calls.
Edit3: If the call is retried after the failure it always works.
I'm trying to create a web app which does many things but the one that I'm currently focused in is the inbox count. I want to use EWS StreamSubscription so that I can get notification for each event and returns the total count of items in the inbox. How can I use this in terms of MVC? I did find some code from Microsoft tutorial that I was gonna test, but I just couldn't figure how I could use it in MVC world i.e. What's the model going to be, if model is the count then how does it get notified every time an event occurs in Exchange Server, etc.
Here's the code I downloaded from Microsoft, but just couldn't understand how I can convert the count to json and push it to client as soon as a new change event occurs. NOTE: This code is unchanged, so it doesn't return count, yet.
using System;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.Exchange.WebServices.Data;
namespace StreamingNotificationsSample
{
internal class Program
{
private static AutoResetEvent _Signal;
private static ExchangeService _ExchangeService;
private static string _SynchronizationState;
private static Thread _BackroundSyncThread;
private static StreamingSubscriptionConnection CreateStreamingSubscription(ExchangeService service,
StreamingSubscription subscription)
{
var connection = new StreamingSubscriptionConnection(service, 30);
connection.AddSubscription(subscription);
connection.OnNotificationEvent += OnNotificationEvent;
connection.OnSubscriptionError += OnSubscriptionError;
connection.OnDisconnect += OnDisconnect;
connection.Open();
return connection;
}
private static void SynchronizeChangesPeriodically()
{
while (true)
{
try
{
// Get all changes from the server and process them according to the business
// rules.
SynchronizeChanges(new FolderId(WellKnownFolderName.Inbox));
}
catch (Exception ex)
{
Console.WriteLine("Failed to synchronize items. Error: {0}", ex);
}
// Since the SyncFolderItems operation is a
// rather expensive operation, only do this every 10 minutes
Thread.Sleep(TimeSpan.FromMinutes(10));
}
}
public static void SynchronizeChanges(FolderId folderId)
{
bool moreChangesAvailable;
do
{
Console.WriteLine("Synchronizing changes...");
// Get all changes since the last call. The synchronization cookie is stored in the _SynchronizationState field.
// Only the the ids are requested. Additional properties should be fetched via GetItem calls.
var changes = _ExchangeService.SyncFolderItems(folderId, PropertySet.IdOnly, null, 512,
SyncFolderItemsScope.NormalItems, _SynchronizationState);
// Update the synchronization cookie
_SynchronizationState = changes.SyncState;
// Process all changes
foreach (var itemChange in changes)
{
// This example just prints the ChangeType and ItemId to the console
// LOB application would apply business rules to each item.
Console.Out.WriteLine("ChangeType = {0}", itemChange.ChangeType);
Console.Out.WriteLine("ChangeType = {0}", itemChange.ItemId);
}
// If more changes are available, issue additional SyncFolderItems requests.
moreChangesAvailable = changes.MoreChangesAvailable;
} while (moreChangesAvailable);
}
public static void Main(string[] args)
{
// Create new exchange service binding
// Important point: Specify Exchange 2010 with SP1 as the requested version.
_ExchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
{
Credentials = new NetworkCredential("user", "password"),
Url = new Uri("URL to the Exchange Web Services")
};
// Process all items in the folder on a background-thread.
// A real-world LOB application would retrieve the last synchronization state first
// and write it to the _SynchronizationState field.
_BackroundSyncThread = new Thread(SynchronizeChangesPeriodically);
_BackroundSyncThread.Start();
// Create a new subscription
var subscription = _ExchangeService.SubscribeToStreamingNotifications(new FolderId[] {WellKnownFolderName.Inbox},
EventType.NewMail);
// Create new streaming notification conection
var connection = CreateStreamingSubscription(_ExchangeService, subscription);
Console.Out.WriteLine("Subscription created.");
_Signal = new AutoResetEvent(false);
// Wait for the application to exit
_Signal.WaitOne();
// Finally, unsubscribe from the Exchange server
subscription.Unsubscribe();
// Close the connection
connection.Close();
}
private static void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
// Cast the sender as a StreamingSubscriptionConnection object.
var connection = (StreamingSubscriptionConnection) sender;
// Ask the user if they want to reconnect or close the subscription.
Console.WriteLine("The connection has been aborted; probably because it timed out.");
Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
while (true)
{
var keyInfo = Console.ReadKey(true);
{
switch (keyInfo.Key)
{
case ConsoleKey.Y:
// Reconnect the connection
connection.Open();
Console.WriteLine("Connection has been reopened.");
break;
case ConsoleKey.N:
// Signal the main thread to exit.
Console.WriteLine("Terminating.");
_Signal.Set();
break;
}
}
}
}
private static void OnNotificationEvent(object sender, NotificationEventArgs args)
{
// Extract the item ids for all NewMail Events in the list.
var newMails = from e in args.Events.OfType<ItemEvent>()
where e.EventType == EventType.NewMail
select e.ItemId;
// Note: For the sake of simplicity, error handling is ommited here.
// Just assume everything went fine
var response = _ExchangeService.BindToItems(newMails,
new PropertySet(BasePropertySet.IdOnly, ItemSchema.DateTimeReceived,
ItemSchema.Subject));
var items = response.Select(itemResponse => itemResponse.Item);
foreach (var item in items)
{
Console.Out.WriteLine("A new mail has been created. Received on {0}", item.DateTimeReceived);
Console.Out.WriteLine("Subject: {0}", item.Subject);
}
}
private static void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
{
// Handle error conditions.
var e = args.Exception;
Console.Out.WriteLine("The following error occured:");
Console.Out.WriteLine(e.ToString());
Console.Out.WriteLine();
}
}
}
I just want to understand the basic concept as in what can be model, and where can I use other functions.
Your problem is that you are confusing a service (EWS) with your applications model. They are two different things. Your model is entirely in your control, and you can do whatever you want with it. EWS is outside of your control, and is merely a service you call to get data.
In your controller, you call the EWS service and get the count. Then you populate your model with that count, then in your view, you render that model property. It's really that simple.
A web page has no state. It doesn't get notified when things change. You just reload the page and get whatever the current state is (ie, whatever the current count is).
In more advanced applications, like Single Page Apps, with Ajax, you might periodically query the service in the background. Or, you might have a special notification service that uses something like SignalR to notify your SPA of a change, but these concepts are far more advanced than you currently are. You should probably develop your app as a simple stateless app first, then improve it to add ajax functionality or what not once you have a better grasp of things.
That's a very broad question without a clear-cut answer. Your model could certainly have a "Count" property that you could update. The sample code you found would likely be used by your controller.
What im trying to do is a functionality that will advice users that make
audio calls in office communicator over a wireless connection to use a
wired connection instead.
i have been looking around but have not been able to find the info im searching for
Im looking for a way to detect if Office Communicator is in an Audio call.
is there an easy way to do this?
I don't think you'll be able to get exactly what you need with Communicator, but you can get close. (you could probably get even closer, or all the way there, if you were to upgrade to Lync).
You'll need to use the Automation API - documentation here, download here.
First thing to try is catching the users status changes:
MessengerClass _communicator;
public Form1()
{
InitializeComponent();
_communicator = new MessengerClass();
_communicator.OnMyStatusChange += new DMessengerEvents_OnMyStatusChangeEventHandler(_communicator_OnMyStatusChange);
}
void _communicator_OnMyStatusChange(int hr, MISTATUS mMyStatus)
{
AddText(string.Format("My Status changed to '{0}'", mMyStatus));
}
You're looking for a status of MISTATUS_ON_THE_PHONE
The downside of this is that certain statuses will override the MISTATUS_ON_THE_PHONE status. e.g. if the user is set to "Online", and then makes or receives a call, the status will change to MISTATUS_ON_THE_PHONE. But if their status is set to "Do not Disturb" and they make or receive a call, the status will NOT change to MISTATUS_ON_THE_PHONE.
You can maybe work around this a bit by examining the call as it is created. Catching a new conversation window being created is fairly straightforward:
_communicator = new MessengerClass();
_communicator.OnIMWindowCreated += new DMessengerEvents_OnIMWindowCreatedEventHandler(_communicator_OnIMWindowCreated);
Problem is, this will fire for IM and AV conversations, and also for incoming conversations as well as outgoing. There is no way to directly detect whether the call is an outgoing audio call.
You can also catch the "Contact Added" event, this will give you some info about which recipients get added to the conversation, and when. It's possible that the order in which this happens will give you some info as to whether its outgoing or incoming, and you could look for "tel:" uri's being added to tell you if the call is to a phone (although this won't help for communicator to communicator calls)
_communicator.OnIMWindowContactAdded += new DMessengerEvents_OnIMWindowContactAddedEventHandler(_communicator_OnIMWindowContactAdded);
The best thing to do is to have a play around with the events, and see what happens under which circumstances. This code should get you up and running with that.
MessengerClass _communicator;
public Form1()
{
InitializeComponent();
_communicator = new MessengerClass();
_communicator.OnIMWindowCreated += new DMessengerEvents_OnIMWindowCreatedEventHandler(_communicator_OnIMWindowCreated);
_communicator.OnIMWindowDestroyed += new DMessengerEvents_OnIMWindowDestroyedEventHandler(_communicator_OnIMWindowDestroyed);
_communicator.OnIMWindowContactAdded += new DMessengerEvents_OnIMWindowContactAddedEventHandler(_communicator_OnIMWindowContactAdded);
_communicator.OnIMWindowContactRemoved += new DMessengerEvents_OnIMWindowContactRemovedEventHandler(_communicator_OnIMWindowContactRemoved);
_communicator.OnMyStatusChange += new DMessengerEvents_OnMyStatusChangeEventHandler(_communicator_OnMyStatusChange);
}
void _communicator_OnMyStatusChange(int hr, MISTATUS mMyStatus)
{
AddText(string.Format("My Status changed to '{0}'", mMyStatus));
}
void _communicator_OnIMWindowContactRemoved(object pContact, object pIMWindow)
{
AddText(string.Format("{0} - Participant removed - '{1}'", ((IMessengerConversationWndAdvanced)pIMWindow).HWND, ((IMessengerContactAdvanced)pContact).SigninName));
}
void _communicator_OnIMWindowContactAdded(object pContact, object pIMWindow)
{
AddText(string.Format("{0} - Participant added - '{1}'", ((IMessengerConversationWndAdvanced)pIMWindow).HWND, ((IMessengerContactAdvanced)pContact).SigninName));
}
void _communicator_OnIMWindowDestroyed(object pIMWindow)
{
AddText(string.Format("{0} Conversation Closed, duration = {1}", ((IMessengerConversationWndAdvanced)pIMWindow).HWND, (DateTime.Now - _start).ToString()));
}
void _communicator_OnIMWindowCreated(object pIMWindow)
{
try
{
AddText(string.Format("{0} Conversation Created", ((IMessengerConversationWndAdvanced)pIMWindow).HWND));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private delegate void AddTextDelegate(string text);
private void AddText(string text)
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new AddTextDelegate(AddText), text);
return;
}
textBox1.Text += text + "\r\n";
}
By the way, don't forget to accept this as the answer using the "tick", if you feel that it helped :)
I'm building an application that uses a WCF client to retrieve data from my server.
I want my call to the service to be asynchronous because many of them need to change the UI and I don't want to lose responsiveness from my app.
I tried using *Completed and *Async:
ServiceUserClient client = new ServiceUserClient();
client.FindUserCompleted += delegate(object sender, FindUserCompletedEventArgs e)
{
// here e.Result always fails
};
client.FindUserAsync(text);
Inside the *Completed delegate I always get an error (Connection closed by remote host: I enabled every logging I could find but I still don't understand why I get these errors)
Synchronous calls always work.
I have a class that handles all the calls to the service.
Is there a way to have syncronous calls inside something like a threaded class?
Are you setting the client side bindings to match what the server accepts?
You should also try testing it with the WCF test client (normally under %Program Files%\Microsoft Visual Studio 10.0\Common7\IDE\WcfTestClient.exe). If the test client works then check the bindings.
Is your call even getting to the server? I've had similar errors happen when serializing the response from the server to the client, so you might want to check for that. If you get to your server then the bindings are not the problem but rather there is a serialization problem. Do you have "sets" on the data model properties that are trying to get deserialized on the server?
I know this is no answer but I haven't been here enough to be allowed comments...and I've been where you are, totally frustrating.
I ended up creating my own async methods using BackgroundWorker this way (probably not the best way but it works):
// this is the click event on my search button
private void FindUser_Click(object sender, EventArgs e)
{
this.UserListSearch.Enabled = false;
this.UserListSearch.Items.Clear();
Model.FindUser(FindText.Text.ToUpper(), userlist =>
{
foreach (User u in userlist)
{
ListViewItem item = new ListViewItem(u.UserName);
item.Name = u.UserName;
item.SubItems.Add(u.Description);
this.UserListSearch.Items.Add(item);
}
this.UserListSearch.Enabled = true;
});
}
// this is the function I call when I need async call
public void FindUser(string text, Action<User[]> callback)
{
CreateBackgroundWorker<User[]>(() =>
{
ServiceUsersClient client = new ServiceUsersClient();
var results = client.FindUser(text);
client.Close();
return results;
}, callback);
}
// this is my utility function to create a bgworker "on demand"
private void CreateBackgroundWorker<T>(Func<T> dowork, Action<T> callback)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (sender, args) =>
{
T result = dowork.Invoke();
(callback.Target as Form).Invoke(callback, result);
};
worker.RunWorkerAsync();
}
I want to use the status method but i dont understand how it works. Could someone show me an example of use please?
EventHandler < SvnStatusEventArgs > statusHandler = new EventHandler<SvnStatusEventArgs>(void(object, SvnStatusEventArgs) target);
client.Status(path, statusHandler);
Well, it'll work exactly like the svn status command : http://svnbook.red-bean.com/en/1.0/re26.html
You'll get the list of files pumped to the EventHandler:
using(SvnClient client = /* set up a client */ ){
EventHandler<SvnStatusEventArgs> statusHandler = new EventHandler<SvnStatusEventArgs>(HandleStatusEvent);
client.Status(#"c:\foo\some-working-copy", statusHandler);
}
...
void HandleStatusEvent (object sender, SvnStatusEventArgs args)
{
switch(args.LocalContentStatus){
case SvnStatus.Added: // Handle appropriately
break;
}
// review other properties of 'args'
}
Or if you don't mind inline delegates:
using(SvnClient client = new SvnClient())
{
client.Status(path,
delegate(object sender, SvnStatusEventArgs e)
{
if (e.LocalContentStatus == SvnStatus.Added)
Console.WriteLine("Added {0}", e.FullPath);
});
}
Note that the delegate versions of the SharpSvn functions are always a (tiny) bit faster than the revisions returns a collection as this method allows marshalling the least amount of information to the Managed world. You can use Svn*EventArgs.Detach() to marshall everything anyway. (This is what the .GetXXX() functions do internally)
The inline delegate version worked for me but the EventHandler<T> version didn't work until I set the type to EventHandler<SvnStatusEventArgs>.