Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All hosts] (Device Permission) Map code samples #1935

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions docs/code-snippets/office-snippets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,107 @@ Office.CustomXmlParts#getByNamespaceAsync:member(2):
function write(message){
document.getElementById('message').innerText += message;
}
Office.DevicePermission#requestPermissions:member(1):
- |-
// Request permission from a user to access their camera and microphone.
if (Office.context.platform === Office.PlatformType.OfficeOnline) {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.microphone
];
Office.devicePermission
.requestPermissions(deviceCapabilities)
.then((isGranted) => {
if (isGranted) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}
Office.DevicePermission#requestPermissionsAsync:member(2):
- |-
// Request permission from a user to access their camera, geolocation, and microphone.
if (Office.context.mailbox.diagnostics.hostName === "OutlookWebApp") {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.geolocation,
Office.DevicePermissionType.microphone
];

Office.devicePermission.requestPermissionsAsync(deviceCapabilities, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log (asyncResult.error.message);
return;
}

if (asyncResult.value) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}
Office.DevicePermissionType:enum:
- |-
// Request permission from a user to access their device capabilities.
const host = Office.context.host;
if (host === Office.HostType.Excel || host === Office.HostType.PowerPoint || host === Office.HostType.Word) {
if (Office.context.platform === Office.PlatformType.OfficeOnline) {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.microphone
];
Office.devicePermission
.requestPermissions(deviceCapabilities)
.then((isGranted) => {
if (isGranted) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}
} else if (host === Office.HostType.Outlook) {
if (Office.context.mailbox.diagnostics.hostName === "OutlookWebApp") {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.geolocation,
Office.DevicePermissionType.microphone
];

Office.devicePermission.requestPermissionsAsync(deviceCapabilities, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log (asyncResult.error.message);
return;
}

if (asyncResult.value) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}
} else {
console.log("The add-in isn't running in Excel, Outlook, PowerPoint, or Word.");
}
Office.Dialog#addEventHandler:member(1):
- |-
// The following example shows how to open a dialog with a specified size. It also shows
Expand Down
65 changes: 65 additions & 0 deletions docs/docs-ref-autogen/office/office/office.devicepermission.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ methods:

- If a user selects **Deny** from the dialog, the user will be requested for permissions again the next time
the add-in requires access to the user's device capabilities.


#### Examples


```TypeScript

// Request permission from a user to access their camera and microphone.

if (Office.context.platform === Office.PlatformType.OfficeOnline) {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.microphone
];
Office.devicePermission
.requestPermissions(deviceCapabilities)
.then((isGranted) => {
if (isGranted) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}

```
isPreview: false
isDeprecated: false
syntax:
Expand Down Expand Up @@ -211,6 +241,41 @@ methods:
- If your add-in implements [event-based
activation](https://learn.microsoft.com/office/dev/add-ins/outlook/autolaunch)<!-- -->, browser permissions to
device capabilities aren't inherited and the `requestPermissionsAsync` method isn't supported.


#### Examples


```TypeScript

// Request permission from a user to access their camera, geolocation, and microphone.

if (Office.context.mailbox.diagnostics.hostName === "OutlookWebApp") {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.geolocation,
Office.DevicePermissionType.microphone
];

Office.devicePermission.requestPermissionsAsync(deviceCapabilities, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log (asyncResult.error.message);
return;
}

if (asyncResult.value) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}

```
isPreview: false
isDeprecated: false
syntax:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,67 @@ remarks: >-

**Requirement set**: [DevicePermission
1.1](https://learn.microsoft.com/javascript/api/requirement-sets/common/device-permission-service-requirement-sets)


#### Examples


```TypeScript

// Request permission from a user to access their device capabilities.

const host = Office.context.host;

if (host === Office.HostType.Excel || host === Office.HostType.PowerPoint || host === Office.HostType.Word) {
if (Office.context.platform === Office.PlatformType.OfficeOnline) {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.microphone
];
Office.devicePermission
.requestPermissions(deviceCapabilities)
.then((isGranted) => {
if (isGranted) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}
} else if (host === Office.HostType.Outlook) {
if (Office.context.mailbox.diagnostics.hostName === "OutlookWebApp") {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.geolocation,
Office.DevicePermissionType.microphone
];

Office.devicePermission.requestPermissionsAsync(deviceCapabilities, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log (asyncResult.error.message);
return;
}

if (asyncResult.value) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}
} else {
console.log("The add-in isn't running in Excel, Outlook, PowerPoint, or Word.");
}

```
isPreview: false
isDeprecated: false
fields:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ methods:

- If a user selects **Deny** from the dialog, the user will be requested for permissions again the next time
the add-in requires access to the user's device capabilities.


#### Examples


```TypeScript

// Request permission from a user to access their camera and microphone.

if (Office.context.platform === Office.PlatformType.OfficeOnline) {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.microphone
];
Office.devicePermission
.requestPermissions(deviceCapabilities)
.then((isGranted) => {
if (isGranted) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}

```
isPreview: false
isDeprecated: false
syntax:
Expand Down Expand Up @@ -211,6 +241,41 @@ methods:
- If your add-in implements [event-based
activation](https://learn.microsoft.com/office/dev/add-ins/outlook/autolaunch)<!-- -->, browser permissions to
device capabilities aren't inherited and the `requestPermissionsAsync` method isn't supported.


#### Examples


```TypeScript

// Request permission from a user to access their camera, geolocation, and microphone.

if (Office.context.mailbox.diagnostics.hostName === "OutlookWebApp") {
const deviceCapabilities = [
Office.DevicePermissionType.camera,
Office.DevicePermissionType.geolocation,
Office.DevicePermissionType.microphone
];

Office.devicePermission.requestPermissionsAsync(deviceCapabilities, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log (asyncResult.error.message);
return;
}

if (asyncResult.value) {
console.log("Permission granted.");

// Do something when permission is granted.
} else {
console.log("Permission denied.");

// Do something when permission is denied.
}
});
}

```
isPreview: false
isDeprecated: false
syntax:
Expand Down
Loading