简体   繁体   中英

Access Sharepoint from a Worker role service running on Azure

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!

So the crux of this problem is that you get a 403 when hitting Sharepoint O365. http://www.checkupdown.com/status/E403.html tells us that a HTTP 403 error indicates (my emphasis):

The Web server (running the Web site) thinks that the HTTP data stream sent by the client (eg your Web browser) was correct, but access to the resource identified by the URL is forbidden for some reason .

This indicates a fundamental access problem , which may be difficult to resolve because the HTTP protocol allows the Web server to give this response without providing any reason at all. So the 403 error is equivalent to a blanket 'NO' by the Web server - with no further discussion allowed.

Although your code (binaries) might be the same on your local dev machine and in the Cloud Service, your configuration might not be the same .

Can you therefore check that you have the same configuration values in your Cloud Service's ServiceConfiguration.Local.cscfg and ServiceConfiguration.Cloud.cscfg , specifically the Sharepoint.Uri and Sharepoint.ListName values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM