From 9990ac8c82892fcf5c1f63bc538e962dd45c4212 Mon Sep 17 00:00:00 2001 From: Rick Kirkham Date: Wed, 25 Sep 2024 10:02:29 -0700 Subject: [PATCH] [All Hosts] (snippets) Adding some high-value snippets (#2073) * [All Hosts] (snippets) Adding some high-value snippets * Update docs/code-snippets/officeruntime-snippets.yaml --- docs/code-snippets/office-snippets.yaml | 104 ++++++++++++++++++ .../code-snippets/officeruntime-snippets.yaml | 25 +++++ docs/code-snippets/outlook-snippets.yaml | 46 ++++++++ 3 files changed, 175 insertions(+) create mode 100644 docs/code-snippets/officeruntime-snippets.yaml diff --git a/docs/code-snippets/office-snippets.yaml b/docs/code-snippets/office-snippets.yaml index eb1f4b9d66..90c0822ba9 100644 --- a/docs/code-snippets/office-snippets.yaml +++ b/docs/code-snippets/office-snippets.yaml @@ -501,6 +501,14 @@ Office.Auth:interface: console.log("Error obtaining token", result.error); } }); +Office.Auth#getAuthContext:member(1): + - |- + try{ + const authContext = await Office.auth.getAuthContext(); + console.log(authContext.userPrincipalName); + } catch (error) { + console.log("Error obtaining token", error); + } Office.Auth#getAccessToken:member(1): - |- try{ @@ -4035,6 +4043,102 @@ Office.Settings#set:member(1): function setMySetting() { Office.context.document.settings.set('mySetting', 'mySetting value'); } +Office.SharedProperties:interface: + - |- + 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 placeholder with actual Outlook REST API version e.g. "v2.0". + // Update placeholder with actual operation. + let rest_url = sharedProperties.targetRestUrl + "//users/" + sharedProperties.targetMailbox + "/"; + + $.ajax({ + url: rest_url, + dataType: 'json', + headers: + { + "Authorization": "Bearer " + asyncResult1.asyncContext + } + } + ).done( + function (response) { + console.log("success"); + } + ).fail( + function (error) { + console.log("error message"); + } + ); + } + } + ); + } + } + ); + } +Office.SharedProperties#delegatePermissions:member: + - |- + 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 placeholder with actual Outlook REST API version e.g. "v2.0". + // Update placeholder with actual operation. + let rest_url = sharedProperties.targetRestUrl + "//users/" + sharedProperties.targetMailbox + "/"; + + $.ajax({ + url: rest_url, + dataType: 'json', + headers: + { + "Authorization": "Bearer " + asyncResult1.asyncContext + } + } + ).done( + function (response) { + console.log("success"); + } + ).fail( + function (error) { + console.log("error message"); + } + ); + } + } + ); + } + } + ); + } Office.TableBinding#addColumnsAsync:member(1): - |- // The following example adds a single column with three rows to a bound table with the id "myTable" diff --git a/docs/code-snippets/officeruntime-snippets.yaml b/docs/code-snippets/officeruntime-snippets.yaml new file mode 100644 index 0000000000..039f3c886a --- /dev/null +++ b/docs/code-snippets/officeruntime-snippets.yaml @@ -0,0 +1,25 @@ +OfficeRuntime.Auth:interface: + - |- + // Get the auth context object and use it to get an + // access token. + const authContext = OfficeRuntime.context.auth; + const accessToken = authContext.getAccessTokenAsync(); +Office.Auth#getAccessToken:member(1): + - |- + async function getUserData() { + try { + let userTokenEncoded = await OfficeRuntime.auth.getAccessToken(); + let userToken = jwt_decode(userTokenEncoded); // Using the https://www.npmjs.com/package/jwt-decode library. + console.log(userToken.name); // user name + console.log(userToken.preferred_username); // email + console.log(userToken.oid); // user id + } + catch (exception) { + if (exception.code === 13003) { + // SSO is not supported for domain user accounts, only + // Microsoft 365 Education or work account, or a Microsoft account. + } else { + // Handle error + } + } + } \ No newline at end of file diff --git a/docs/code-snippets/outlook-snippets.yaml b/docs/code-snippets/outlook-snippets.yaml index 4385f7873f..a1f64b7792 100644 --- a/docs/code-snippets/outlook-snippets.yaml +++ b/docs/code-snippets/outlook-snippets.yaml @@ -593,6 +593,34 @@ Office.LocationIdentifier:interface: "type": Office.MailboxEnums.LocationType.Custom } ]; +Office.Mailbox:interface: + - |- + Office.initialize = function (reason) { + $(document).ready(function () { + // Get a reference to the mailbox and use it to add an + // event handler. + const mailBox = Office.context.mailbox; + mailbox.addHandlerAsync( + Office.EventType.ItemChanged, + loadNewItem, + function (result) { + if (result.status === Office.AsyncResultStatus.Failed) { + // Handle error. + } + }); + }); + }; + + function loadNewItem(eventArgs) { + const item = Office.context.mailbox.item; + + // Check that item is not null. + if (item !== null) { + // Work with item, e.g., define and call function that + // loads the properties of the newly selected item. + loadProps(item); + } + } Office.Mailbox#addHandlerAsync:member(1): - |- Office.initialize = function (reason) { @@ -976,6 +1004,24 @@ Office.MessageCompose#to:member: function callback(asyncResult) { const arrayOfToRecipients = asyncResult.value; } +Office.MessageRead:interface: + - |- + // The following code builds an HTML string with details of all attachments on the current item. + const item = Office.context.mailbox.item; + let outputString = ""; + if (item.attachments.length > 0) { + for (let i = 0 ; i < item.attachments.length ; i++) { + const attachment = item.attachments[i]; + outputString += "
" + i + ". Name: "; + outputString += attachment.name; + outputString += "
ID: " + attachment.id; + outputString += "
contentType: " + attachment.contentType; + outputString += "
size: " + attachment.size; + outputString += "
attachmentType: " + attachment.attachmentType; + outputString += "
isInline: " + attachment.isInline; + } + } + console.log(outputString); Office.MessageRead#addHandlerAsync:member(1): - |- function myHandlerFunction(eventarg) {