Search code examples
outlookmicrosoft-graph-apioutlook-web-addins

Get current mailbox from Outlook web addin


I just found this: https://learn.microsoft.com/en-us/office/dev/add-ins/reference/manifest/supportssharedfolders . Which tells me there is a way to load an addin into a postbox from another user. I have activated the feature via manifest, which is working fine.

To let the server know where to find the mail, I am currently working with, I need the postbox name, that I am currently in. So I went through the properties I get within Office.context. There seems to be no reference to the current mailbox. Just Office.context.mailbox.userProfile.emailAddress which is referring to my signed in user.

Since I need the current postbox to access the mail via Graph / EWS, there has to be a way to read it, else the SupportsSharedFolders would be senseless. How would I get the current postbox name/ID?


Solution

  • You can get an item's shared properties in Compose or Read mode by calling the item.getSharedPropertiesAsync method. This returns a SharedProperties object that currently provides the user's permissions, the owner's email address, the REST API's base URL, and the target mailbox.

    The following example shows how to get the shared properties of a message or appointment, check if the delegate or shared mailbox user has Write permission, and make a REST call.

    function performOperation() {
      Office.context.mailbox.getCallbackTokenAsync({
          isRest: true
        },
        function (asyncResult) {
          if (asyncResult.status === Office.AsyncResultStatus.Succeeded && asyncResult.value !== "") {
            Office.context.mailbox.item.getSharedPropertiesAsync({
                // Pass auth token along.
                asyncContext: asyncResult.value
              },
              function (asyncResult1) {
                let sharedProperties = asyncResult1.value;
                let delegatePermissions = sharedProperties.delegatePermissions;
    
                // Determine if user can do the expected operation.
                // E.g., do they have Write permission?
                if ((delegatePermissions & Office.MailboxEnums.DelegatePermissions.Write) != 0) {
                  // Construct REST URL for your operation.
                  // Update <version> placeholder with actual Outlook REST API version e.g. "v2.0".
                  // Update <operation> placeholder with actual operation.
                  let rest_url = sharedProperties.targetRestUrl + "/<version>/users/" + sharedProperties.targetMailbox + "/<operation>";
      
                  $.ajax({
                      url: rest_url,
                      dataType: 'json',
                      headers:
                      {
                        "Authorization": "Bearer " + asyncResult1.asyncContext
                      }
                    }
                  ).done(
                    function (response) {
                      console.log("success");
                    }
                  ).fail(
                    function (error) {
                      console.log("error message");
                    }
                  );
                }
              }
            );
          }
        }
      );
    }