I need to update some information on Sharepoint when some information changes in another system. The way I'm doing this (I didn't get to choose, but rather the company) is:
1. When the event occurs in the other system, I send a message to an Azure ServiceBus queue:
QueueClient queueClient = QueueClient.CreateFromConnectionString(connectionString, "authors");
BrokeredMessage message = new BrokeredMessage();
message.ContentType = "Authors";
message.Properties["FirstName"] = FirstName;
// set other properties
try {
queueClient.Send(message);
}
catch (Exception e) {
Logger.Error("Authors Windows Azure notification service fail.", e);
}
finally {
queueClient.Close();
}
This part works fine.
2. I created a WorkerRole to read the ServiceBus, process the message, and post the information in Sharepoint (I simplified the actual code a bit):
BrokeredMessage receivedMessage = Client.Receive(TimeSpan.FromSeconds(10));
if (receivedMessage != null && receivedMessage.ContentType == "Authors")
{
Uri uri = new Uri(CloudConfigurationManager.GetSetting("Sharepoint.Uri"));
Office365ClaimsHelper claimsHelper = new Office365ClaimsHelper(uri, CloudConfigurationManager.GetSetting("Sharepoint.User"), CloudConfigurationManager.GetSetting("Sharepoint.Password"));
using (ClientContext context = new ClientContext(uri))
{
context.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;
AuthorWrapper author = GetAuthorFromMessage(receivedMessage);
string title = CloudConfigurationManager.GetSetting("Sharepoint.ListName");
List authorsList = context.Web.Lists.GetByTitle(title);
context.Load(authorsList);
context.ExecuteQuery(); // THIS LINE
// do some other stuff, including adding the author to the list
}
receivedMessage.Complete();
}
When I ran the WorkerRole locally (using Microsoft Azure Emulator), everything worked perfectly and the information got updated in Sharepoint.
However, when I Published the WorkerRole in Azure, it didn't work (it read the message from the ServiceBus, but didn't update Sharepoint). I used this to debug the the Worker role that's running in Azure, and I found that a 403 Forbidden exception rose when I tried to retrieve the list from Sharepoint (see commented line "THIS LINE").
It's the EXACT SAME CODE, running locally works fine, running in Azure raises this exception!
Notes: the Office365ClaimsHelper class is from github
I already tried this, but it's not my case (I use Cloud Service not Virtual Machine).
The WorkerRole is targeting .Net 4.0 (again, not my choice)
I tried to put the most significant code, but if you need something more let me know, thanks in advance!