/
app-install.ts
158 lines (129 loc) · 4.94 KB
/
app-install.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { ContextInfo } from './../../spo';
import auth from '../../SpoAuth';
import Auth from '../../../../Auth';
import config from '../../../../config';
import commands from '../../commands';
import GlobalOptions from '../../../../GlobalOptions';
import * as request from 'request-promise-native';
import {
CommandHelp,
CommandOption,
CommandValidate
} from '../../../../Command';
import SpoCommand from '../../SpoCommand';
import Utils from '../../../../Utils';
const vorpal: Vorpal = require('../../../../vorpal-init');
interface CommandArgs {
options: Options;
}
interface Options extends GlobalOptions {
id: string;
siteUrl: string;
}
class AppInstallCommand extends SpoCommand {
public get name(): string {
return commands.APP_INSTALL;
}
public get description(): string {
return 'Installs an app from the tenant app catalog in the site';
}
public commandAction(cmd: CommandInstance, args: CommandArgs, cb: () => void): void {
const resource: string = Auth.getResourceFromUrl(args.options.siteUrl);
let siteAccessToken: string = '';
auth
.getAccessToken(resource, auth.service.refreshToken as string, cmd, this.debug)
.then((accessToken: string): Promise<ContextInfo> => {
siteAccessToken = accessToken;
if (this.debug) {
cmd.log(`Retrieved access token ${accessToken}. Retrieving request digest...`);
}
return this.getRequestDigestForSite(args.options.siteUrl, siteAccessToken, cmd, this.debug);
})
.then((res: ContextInfo): Promise<string> => {
if (this.debug) {
cmd.log('Response:');
cmd.log(res);
cmd.log('');
}
if (this.verbose) {
cmd.log(`Installing app '${args.options.id}' in site '${args.options.siteUrl}'...`);
}
const requestOptions: any = {
url: `${args.options.siteUrl}/_api/web/tenantappcatalog/AvailableApps/GetById('${encodeURIComponent(args.options.id)}')/install`,
headers: Utils.getRequestHeaders({
authorization: `Bearer ${auth.site.accessToken}`,
accept: 'application/json;odata=nometadata',
'X-RequestDigest': res.FormDigestValue
})
};
if (this.debug) {
cmd.log('Executing web request...');
cmd.log(requestOptions);
cmd.log('');
}
return request.post(requestOptions);
})
.then((res: string): void => {
if (this.debug) {
cmd.log('Response:');
cmd.log(res);
cmd.log('');
}
cb();
}, (rawRes: any): void => this.handleRejectedODataPromise(rawRes, cmd, cb));
}
public options(): CommandOption[] {
const options: CommandOption[] = [
{
option: '-i, --id <id>',
description: 'ID of the app to retrieve information for'
},
{
option: '-s, --siteUrl <siteUrl>',
description: 'Absolute URL of the site to install the app in'
}
];
const parentOptions: CommandOption[] = super.options();
return options.concat(parentOptions);
}
public validate(): CommandValidate {
return (args: CommandArgs): boolean | string => {
if (!args.options.id) {
return 'Required parameter id missing';
}
if (!args.options.siteUrl) {
return 'Required parameter siteUrl missing';
}
const isValidSharePointUrl: boolean | string = SpoCommand.isValidSharePointUrl(args.options.siteUrl);
if (isValidSharePointUrl !== true) {
return isValidSharePointUrl;
}
return true;
};
}
public help(): CommandHelp {
return function (args: {}, log: (help: string) => void): void {
const chalk = vorpal.chalk;
log(vorpal.find(commands.APP_INSTALL).helpInformation());
log(
` ${chalk.yellow('Important:')} before using this command, connect to a SharePoint Online site,
using the ${chalk.blue(commands.CONNECT)} command.
Remarks:
To install an app from the tenant app catalog in a site, you have to first connect
to a SharePoint site using the ${chalk.blue(commands.CONNECT)} command,
eg. ${chalk.grey(`${config.delimiter} ${commands.CONNECT} https://contoso.sharepoint.com`)}.
If the app with the specified ID doesn't exist in the tenant app catalog, the command will fail
with an error. Before you can install app in a site, you have to add it to the tenant app catalog
first using the ${chalk.blue(commands.APP_ADD)} command.
Examples:
Install the app with ID ${chalk.grey('b2307a39-e878-458b-bc90-03bc578531d6')}
in the ${chalk.grey('https://contoso.sharepoint.com')} site.
${chalk.grey(config.delimiter)} ${commands.APP_INSTALL} -i b2307a39-e878-458b-bc90-03bc578531d6 -s https://contoso.sharepoint.com
More information:
Application Lifecycle Management (ALM) APIs
https://docs.microsoft.com/en-us/sharepoint/dev/apis/alm-api-for-spfx-add-ins
`);
};
}
}
module.exports = new AppInstallCommand();