Ouvrir le client OneNote

Vous pouvez utiliser la propriété links d’une page ou d’un bloc-notes pour ouvrir une application OneNote à une page particulière ou sur un bloc-notes particulier.

La propriété links est un objet JSON qui contient deux URL. Les URL ouvrent la page ou le bloc-notes dans l’application cliente OneNote ou dans OneNote sur le web.

{ 
    "links": {
        "oneNoteClientUrl": {
            "href": "onenote:https://..."
        },
        "oneNoteWebUrl": {
            "href": "https://..."
        }
    }
}
  • oneNoteClientUrl

    • Ouvre le client OneNote s’il est déjà installé sur l’appareil. Cette URL comprend le préfixe onenote.
    • Ouvre la version spécifique de la langue si elle est installée sur l’appareil. Dans le cas contraire, elle utilise le paramètre de langue de la plateforme.
  • oneNoteWebUrl

    • Ouvre OneNote sur le web si le navigateur par défaut sur l’appareil le prend en charge.
    • Utilise le paramètre de langue du navigateur.

L’API OneNote renvoie la propriété links dans la réponse HTTP pour les opérations suivantes :

Les exemples suivants indiquent comment vérifier le code d’état de la réponse, analyser le fichier JSON pour extraire les URL, puis ouvrir le client OneNote.

Exemple iOS

L’exemple suivant obtient les URL du client OneNote à partir de la réponse JSON. Il utilise la bibliothèque AFNetworking (https://afnetworking.com/) pour extraire les deux URL. Dans l’exemple, created est un pointeur vers l’objet ONSCPSStandardResponse utilisé pour stocker les valeurs de réponse et responseObject contient le fichier JSON analysé.

    /* Import the JSON library */
    #import "AFURLRequestSerialization.h"

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            if(delegate) {
                  int status = [returnResponse statusCode];
                  ONSCPSStandardResponse *standardResponse = nil;
                  if (status == 201) {
                        ONSCPSCreateSuccessResponse *created = 
                              [[ONSCPSCreateSuccessResponse alloc] init];
                        created.httpStatusCode = status;
                        NSError *jsonError;
                        NSDictionary *responseObject = 
                              [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&jsonError];
                        if(responseObject && !jsonError) {
                              created.oneNoteClientUrl = ((NSDictionary *)
                                    ((NSDictionary *)responseObject[@"links"])[@"oneNoteClientUrl"])[@"href"];
                              created.oneNoteWebUrl = ((NSDictionary *)
                                    ((NSDictionary *)responseObject[@"links"])[@"oneNoteWebUrl"])[@"href"];
                        }
                  standardResponse = created;
                  }
                  else {
                        ONSCPSStandardErrorResponse *error = [[ONSCPSStandardErrorResponse alloc] init];
                        error.httpStatusCode = status;
                        error.message = [[NSString alloc] initWithData:returnData 
                              encoding:NSUTF8StringEncoding];
                        standardResponse = error;
                  }
                  // Send the response back to the client.
                  if (standardResponse) {
                        [delegate exampleServiceActionDidCompleteWithResponse: standardResponse];
                  }
            }
      }

Une fois que vous avez analysé les URL de la réponse, vous pouvez ouvrir OneNote en utilisant le code suivant. Utilisez oneNoteClientUrl pour ouvrir le client OneNote installé ou oneNoteWebURL pour ouvrir OneNote sur le web.

NSURL *url = [NSURL URLWithString:standardResponse.oneNoteWebUrl];
[[UIApplication sharedApplication] openURL:url];

Exemple Android

Tout d’abord, vérifiez le code d’état de succès, puis analysez le fichier JSON. L’exemple suppose qu’une demande POST a été envoyée et recherche donc un code d’état 201 Created. Si vous avez exécuté une demande GET, recherchez plutôt un code d’état 200.

public ApiResponse getResponse() throws Exception {
    /* Get the HTTP response code and message from the connection object */
    int responseCode = mUrlConnection.getResponseCode();
    String responseMessage = mUrlConnection.getResponseMessage();
    String responseBody = null;

    /* Get the response if the new page was created successfully. */
    if ( responseCode == 201) {
        InputStream is = mUrlConnection.getInputStream();

        /* Verify that this byte array is big enough. */
        byte[] b1 = new byte[1024];
        StringBuffer buffer = new StringBuffer();

        /* Copy the body of the response into the new string. */
        /* Make sure the buffer is big enough. */
        while ( is.read(b1) != -1)
            buffer.append(new String(b1));

      /* When the returned data is complete, close the connection 
         and convert the byte array into a string. */
        mUrlConnection.disconnect();
        responseBody =  buffer.toString();
    }

    /* Create a new JSON object, and an object to hold the response URLs. */
    JSONObject responseObject = null;
    ApiResponse response = new ApiResponse();
    try {

        /* Store and verify the HTTP response code. */
        response.setResponseCode(responseCode);
        response.setResponseMessage(responseMessage);
        if ( responseCode == 201) {

            /* Retrieve the two URLs from the links property. */
            responseObject = new JSONObject(responseBody);
            String clientUrl = responseObject.getJSONObject(
                "links").getJSONObject("oneNoteClientUrl").getString("href");
            String webUrl = responseObject.getJSONObject(
                "links").getJSONObject("oneNoteWebUrl").getString("href");
            response.setOneNoteClientUrl(clientUrl);
            response.setOneNoteWebUrl(webUrl);
        }
    } catch (JSONException ex) {

        /* If the JSON was malformed or incomplete... */
        String msg = ex.getMessage();
        msg = msg;
    }
    return response;
}

À l’aide des propriétés de réponse, votre application peut ouvrir OneNote sur le web, comme illustré dans l’exemple suivant.

if (response.getResponseCode() == 201) {
    Uri uriUrl = Uri.parse(response.getOneNoteWebUrl);  
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); 
    startActivity(launchBrowser);
}

Votre application peut également ouvrir le client OneNote sur un appareil Android. Lorsque vous utilisez la propriété oneNoteClientUrl, vous devez placer les chaînes GUID entre accolades { } avant de lancer la tentative. L’exemple suivant montre comment procéder.

if (response.getResponseCode() == 201) {

    // Get the URL from the OneNote API JSON response.
    String onenoteClientUrl = obtainClientLinkFromJSONResponse();
    String androidClientUrl = 
        onenoteClientUrl.replaceAll(
            "=([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})&",
            "={$1}&");

    // Open the URL: Open the newly created OneNote page.
    Uri uriUrl = Uri.parse(androidClientUrl);  
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); 
    startActivity(launchBrowser);
}