Share via


Call the Office 365 APIs from apps for SharePoint

Learn how to add calls to the Office 365 APIs to a provider-hosted aplicativo para SharePoint.

Última alteração: terça-feira, 14 de abril de 2015

Aplica-se a: SharePoint Online | SharePoint Server 2013

Neste artigo
Before you begin
Update the app in Visual Studio
Register the app with AAD and deploy it
Recursos adicionais

This topic is addressed to the scenario in which you have a provider-hosted aplicativo para SharePoint that is deployed and in use in production, and you want to add access to the Office 365 APIs to it.

Dica

You cannot access the Office 365 APIs from a SharePoint-hosted app unless you first convert it to a provider-hosted app. This is because the OAuth system by which apps get permissions to call the Office 365 APIs requires that there be a single URL, shared by all instances of the app, to which the secure token service (which is AAD in this case) sends the access token that will be passed to the Office 365 APIs with each call. So an app has to have a remote web application or service to serve in this token-handling role. A SharePoint-hosted app does not have remote components. Also, every instance of the app has its own app web domain, so this domain cannot serve as the token-handling URL.

Before you begin

The following are prerequisites to the procedures in this article:

Update the app in Visual Studio

  1. Open the app solution in Visual Studio.

  2. Right-click the web application project in Solution Explorer and select Add, and then Connected Service.

  3. Continue with the procedure described in Use the Visual Studio Service Manager to register your app and add Office 365 APIs to your project.

    With this procedure, the tools do three major things:

    • Add the NuGet packages for Microsoft.Office365.Discovery, Microsoft.Office365.OutlookServices, and Microsoft.Office365.SharePoint to the project.

    • Create a temporary registration in Azure AD, with a localhost URL. You will be replacing this with a permanent registration when you are ready to deploy the updated app to production.

    • Add some settings to the appSettings section of the web.config file.

  4. Add calls to the Office 365 APIs in the web application code. (You cannot access the Office 365 APIs from JavaScript on app web pages. For more about what a SharePoint app web is, see Host webs, webs de aplicativo e domínio de isolamento.)

    Your code will have distinct access tokens for its existing SharePoint code (which gets the token from Microsoft Azure Access Control Service (ACS)) and its new Office 365 APIs code (which gets its token from AAD). If you are using managed code, the OAuth token flow code is done for you in the DLLs that are included in the NuGet packages. This code is implemented by using the client context pattern just as the generated SharePointContext.cs and TokenHelper.cs files do for your existing code.

  5. Debug the app in Visual Studio (F5). Each time you do, the Office Developer Tools for Visual Studio create a new client ID in your web.config and a new temporary registration of the app with ACS. However, the temporary registration in AAD does not change.

  6. Update the app web components (if any) of the provider-hosted app as needed. See Aplicativo para o processo de atualização do SharePoint and its child topics.

  7. When the app is ready for production, continue with Register the app with AAD and deploy it.

Register the app with AAD and deploy it

  1. If the AAD directory in which Office Developer Tools for Visual Studio created the temporary registration is the same one in which the production app will be registered, delete the temporary registration with the following procedure; otherwise, skip to step 2.

    1. Log in to the Microsoft Azure Portal.

    2. Open the ACTIVE DIRECTORY tab and then open the directory in which you registered the app.

    3. Click APPLICATIONS, and then select Applications my company owns on the Show drop down list, and then click the circled check button. Your newly registered app is listed.

    4. Select your app and click DELETE at the bottom of the page.

  2. Do not manually create a permanent registration in AAD for the app. Instead, register your app with AAD and finalize the client ID and client secret values as explained in Register and manage apps in Azure Active Directory.

  3. Redeploy your updated remote components to the same URL as they had before the update.

  4. If none of the following conditions apply, you are finished. Your new functionality is immediately available in existing instances of the app and users do not have to update them. If any of these conditions do apply, continue with the next step.

    • You made changes to the SharePoint components of the provider-hosted app, such as a change to a file on the app web or a custom action on the host web.

    • You added a handler for the AppUpdated event.

    • Your app is marketed through the Office Store.

  5. Raise the version number of the app in the app manifest.

  6. Repackage the app and upload it to the Office Store or to the various app catalogs to which it was previously uploaded. Within 24 hours, users will get a notification on the app tile that they can update the app.

Recursos adicionais