Skip to content

Commit 9990ac8

Browse files
authored
[All Hosts] (snippets) Adding some high-value snippets (#2073)
* [All Hosts] (snippets) Adding some high-value snippets * Update docs/code-snippets/officeruntime-snippets.yaml
1 parent a3f9fde commit 9990ac8

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

docs/code-snippets/office-snippets.yaml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,14 @@ Office.Auth:interface:
501501
console.log("Error obtaining token", result.error);
502502
}
503503
});
504+
Office.Auth#getAuthContext:member(1):
505+
- |-
506+
try{
507+
const authContext = await Office.auth.getAuthContext();
508+
console.log(authContext.userPrincipalName);
509+
} catch (error) {
510+
console.log("Error obtaining token", error);
511+
}
504512
Office.Auth#getAccessToken:member(1):
505513
- |-
506514
try{
@@ -4035,6 +4043,102 @@ Office.Settings#set:member(1):
40354043
function setMySetting() {
40364044
Office.context.document.settings.set('mySetting', 'mySetting value');
40374045
}
4046+
Office.SharedProperties:interface:
4047+
- |-
4048+
function performOperation() {
4049+
Office.context.mailbox.getCallbackTokenAsync({
4050+
isRest: true
4051+
},
4052+
function (asyncResult) {
4053+
if (asyncResult.status === Office.AsyncResultStatus.Succeeded && asyncResult.value !== "") {
4054+
Office.context.mailbox.item.getSharedPropertiesAsync({
4055+
// Pass auth token along.
4056+
asyncContext: asyncResult.value
4057+
},
4058+
function (asyncResult1) {
4059+
let sharedProperties = asyncResult1.value;
4060+
let delegatePermissions = sharedProperties.delegatePermissions;
4061+
4062+
// Determine if user can do the expected operation.
4063+
// E.g., do they have Write permission?
4064+
if ((delegatePermissions & Office.MailboxEnums.DelegatePermissions.Write) != 0) {
4065+
// Construct REST URL for your operation.
4066+
// Update <version> placeholder with actual Outlook REST API version e.g. "v2.0".
4067+
// Update <operation> placeholder with actual operation.
4068+
let rest_url = sharedProperties.targetRestUrl + "/<version>/users/" + sharedProperties.targetMailbox + "/<operation>";
4069+
4070+
$.ajax({
4071+
url: rest_url,
4072+
dataType: 'json',
4073+
headers:
4074+
{
4075+
"Authorization": "Bearer " + asyncResult1.asyncContext
4076+
}
4077+
}
4078+
).done(
4079+
function (response) {
4080+
console.log("success");
4081+
}
4082+
).fail(
4083+
function (error) {
4084+
console.log("error message");
4085+
}
4086+
);
4087+
}
4088+
}
4089+
);
4090+
}
4091+
}
4092+
);
4093+
}
4094+
Office.SharedProperties#delegatePermissions:member:
4095+
- |-
4096+
function performOperation() {
4097+
Office.context.mailbox.getCallbackTokenAsync({
4098+
isRest: true
4099+
},
4100+
function (asyncResult) {
4101+
if (asyncResult.status === Office.AsyncResultStatus.Succeeded && asyncResult.value !== "") {
4102+
Office.context.mailbox.item.getSharedPropertiesAsync({
4103+
// Pass auth token along.
4104+
asyncContext: asyncResult.value
4105+
},
4106+
function (asyncResult1) {
4107+
let sharedProperties = asyncResult1.value;
4108+
let delegatePermissions = sharedProperties.delegatePermissions;
4109+
4110+
// Determine if user can do the expected operation.
4111+
// E.g., do they have Write permission?
4112+
if ((delegatePermissions & Office.MailboxEnums.DelegatePermissions.Write) != 0) {
4113+
// Construct REST URL for your operation.
4114+
// Update <version> placeholder with actual Outlook REST API version e.g. "v2.0".
4115+
// Update <operation> placeholder with actual operation.
4116+
let rest_url = sharedProperties.targetRestUrl + "/<version>/users/" + sharedProperties.targetMailbox + "/<operation>";
4117+
4118+
$.ajax({
4119+
url: rest_url,
4120+
dataType: 'json',
4121+
headers:
4122+
{
4123+
"Authorization": "Bearer " + asyncResult1.asyncContext
4124+
}
4125+
}
4126+
).done(
4127+
function (response) {
4128+
console.log("success");
4129+
}
4130+
).fail(
4131+
function (error) {
4132+
console.log("error message");
4133+
}
4134+
);
4135+
}
4136+
}
4137+
);
4138+
}
4139+
}
4140+
);
4141+
}
40384142
Office.TableBinding#addColumnsAsync:member(1):
40394143
- |-
40404144
// The following example adds a single column with three rows to a bound table with the id "myTable"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
OfficeRuntime.Auth:interface:
2+
- |-
3+
// Get the auth context object and use it to get an
4+
// access token.
5+
const authContext = OfficeRuntime.context.auth;
6+
const accessToken = authContext.getAccessTokenAsync();
7+
Office.Auth#getAccessToken:member(1):
8+
- |-
9+
async function getUserData() {
10+
try {
11+
let userTokenEncoded = await OfficeRuntime.auth.getAccessToken();
12+
let userToken = jwt_decode(userTokenEncoded); // Using the https://www.npmjs.com/package/jwt-decode library.
13+
console.log(userToken.name); // user name
14+
console.log(userToken.preferred_username); // email
15+
console.log(userToken.oid); // user id
16+
}
17+
catch (exception) {
18+
if (exception.code === 13003) {
19+
// SSO is not supported for domain user accounts, only
20+
// Microsoft 365 Education or work account, or a Microsoft account.
21+
} else {
22+
// Handle error
23+
}
24+
}
25+
}

docs/code-snippets/outlook-snippets.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,34 @@ Office.LocationIdentifier:interface:
593593
"type": Office.MailboxEnums.LocationType.Custom
594594
}
595595
];
596+
Office.Mailbox:interface:
597+
- |-
598+
Office.initialize = function (reason) {
599+
$(document).ready(function () {
600+
// Get a reference to the mailbox and use it to add an
601+
// event handler.
602+
const mailBox = Office.context.mailbox;
603+
mailbox.addHandlerAsync(
604+
Office.EventType.ItemChanged,
605+
loadNewItem,
606+
function (result) {
607+
if (result.status === Office.AsyncResultStatus.Failed) {
608+
// Handle error.
609+
}
610+
});
611+
});
612+
};
613+
614+
function loadNewItem(eventArgs) {
615+
const item = Office.context.mailbox.item;
616+
617+
// Check that item is not null.
618+
if (item !== null) {
619+
// Work with item, e.g., define and call function that
620+
// loads the properties of the newly selected item.
621+
loadProps(item);
622+
}
623+
}
596624
Office.Mailbox#addHandlerAsync:member(1):
597625
- |-
598626
Office.initialize = function (reason) {
@@ -976,6 +1004,24 @@ Office.MessageCompose#to:member:
9761004
function callback(asyncResult) {
9771005
const arrayOfToRecipients = asyncResult.value;
9781006
}
1007+
Office.MessageRead:interface:
1008+
- |-
1009+
// The following code builds an HTML string with details of all attachments on the current item.
1010+
const item = Office.context.mailbox.item;
1011+
let outputString = "";
1012+
if (item.attachments.length > 0) {
1013+
for (let i = 0 ; i < item.attachments.length ; i++) {
1014+
const attachment = item.attachments[i];
1015+
outputString += "<BR>" + i + ". Name: ";
1016+
outputString += attachment.name;
1017+
outputString += "<BR>ID: " + attachment.id;
1018+
outputString += "<BR>contentType: " + attachment.contentType;
1019+
outputString += "<BR>size: " + attachment.size;
1020+
outputString += "<BR>attachmentType: " + attachment.attachmentType;
1021+
outputString += "<BR>isInline: " + attachment.isInline;
1022+
}
1023+
}
1024+
console.log(outputString);
9791025
Office.MessageRead#addHandlerAsync:member(1):
9801026
- |-
9811027
function myHandlerFunction(eventarg) {

0 commit comments

Comments
 (0)