-
Notifications
You must be signed in to change notification settings - Fork 308
/
customaction-get.ts
206 lines (177 loc) · 7.06 KB
/
customaction-get.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import request from '../../../../request';
import commands from '../../commands';
import GlobalOptions from '../../../../GlobalOptions';
import {
CommandOption,
CommandValidate
} from '../../../../Command';
import SpoCommand from '../../../base/SpoCommand';
import Utils from '../../../../Utils';
import { CustomAction } from './customaction';
const vorpal: Vorpal = require('../../../../vorpal-init');
interface CommandArgs {
options: Options;
}
interface Options extends GlobalOptions {
id: string;
url: string;
scope?: string;
}
class SpoCustomActionGetCommand extends SpoCommand {
public get name(): string {
return `${commands.CUSTOMACTION_GET}`;
}
public get description(): string {
return 'Gets details for the specified custom action';
}
public getTelemetryProperties(args: CommandArgs): any {
const telemetryProps: any = super.getTelemetryProperties(args);
telemetryProps.scope = args.options.scope || 'All';
return telemetryProps;
}
public commandAction(cmd: CommandInstance, args: CommandArgs, cb: () => void): void {
((): Promise<CustomAction> => {
if (args.options.scope && args.options.scope.toLowerCase() !== "all") {
return this.getCustomAction(args.options);
}
return this.searchAllScopes(args.options);
})()
.then((customAction: CustomAction): void => {
if (customAction["odata.null"] === true) {
if (this.verbose) {
cmd.log(`Custom action with id ${args.options.id} not found`);
}
}
else {
cmd.log({
ClientSideComponentId: customAction.ClientSideComponentId,
ClientSideComponentProperties: customAction.ClientSideComponentProperties,
CommandUIExtension: customAction.CommandUIExtension,
Description: customAction.Description,
Group: customAction.Group,
Id: customAction.Id,
ImageUrl: customAction.ImageUrl,
Location: customAction.Location,
Name: customAction.Name,
RegistrationId: customAction.RegistrationId,
RegistrationType: customAction.RegistrationType,
Rights: JSON.stringify(customAction.Rights),
Scope: this.humanizeScope(customAction.Scope),
ScriptBlock: customAction.ScriptBlock,
ScriptSrc: customAction.ScriptSrc,
Sequence: customAction.Sequence,
Title: customAction.Title,
Url: customAction.Url,
VersionOfUserCustomAction: customAction.VersionOfUserCustomAction
});
}
cb();
}, (err: any): void => this.handleRejectedPromise(err, cmd, cb));
}
private getCustomAction(options: Options): Promise<CustomAction> {
const requestOptions: any = {
url: `${options.url}/_api/${options.scope}/UserCustomActions('${encodeURIComponent(options.id)}')`,
headers: {
accept: 'application/json;odata=nometadata'
},
json: true
};
return request.get(requestOptions);
}
/**
* Get request with `web` scope is send first.
* If custom action not found then
* another get request is send with `site` scope.
*/
private searchAllScopes(options: Options): Promise<CustomAction> {
return new Promise<CustomAction>((resolve: (customAction: CustomAction) => void, reject: (error: any) => void): void => {
options.scope = "Web";
this
.getCustomAction(options)
.then((webResult: CustomAction): void => {
if (webResult["odata.null"] !== true) {
return resolve(webResult);
}
options.scope = "Site";
this
.getCustomAction(options)
.then((siteResult: CustomAction): void => {
return resolve(siteResult);
}, (err: any): void => {
reject(err);
});
}, (err: any): void => {
reject(err);
});
});
}
private humanizeScope(scope: number): string {
switch (scope) {
case 2:
return "Site";
case 3:
return "Web";
}
return `${scope}`;
}
public options(): CommandOption[] {
const options: CommandOption[] = [
{
option: '-i, --id <id>',
description: 'Id (Guid) of the custom action to retrieve'
},
{
option: '-u, --url <url>',
description: 'Url of the site (collection) to retrieve the custom action from'
},
{
option: '-s, --scope [scope]',
description: 'Scope of the custom action. Allowed values Site|Web|All. Default All',
autocomplete: ['Site', 'Web', 'All']
}
];
const parentOptions: CommandOption[] = super.options();
return options.concat(parentOptions);
}
public validate(): CommandValidate {
return (args: CommandArgs): boolean | string => {
if (Utils.isValidGuid(args.options.id) === false) {
return `${args.options.id} is not valid. Custom action id (Guid) expected.`;
}
if (SpoCommand.isValidSharePointUrl(args.options.url) !== true) {
return 'Missing required option url';
}
if (args.options.scope) {
if (args.options.scope !== 'Site' &&
args.options.scope !== 'Web' &&
args.options.scope !== 'All') {
return `${args.options.scope} is not a valid custom action scope. Allowed values are Site|Web|All`;
}
}
return true;
};
}
public commandHelp(args: CommandArgs, log: (help: string) => void): void {
const chalk = vorpal.chalk;
log(vorpal.find(commands.CUSTOMACTION_GET).helpInformation());
log(
` Examples:
Return details about the user custom action with ID ${chalk.grey('058140e3-0e37-44fc-a1d3-79c487d371a3')}
located in site or site collection ${chalk.grey('https://contoso.sharepoint.com/sites/test')}
${commands.CUSTOMACTION_GET} -i 058140e3-0e37-44fc-a1d3-79c487d371a3 -u https://contoso.sharepoint.com/sites/test
Return details about the user custom action with ID ${chalk.grey('058140e3-0e37-44fc-a1d3-79c487d371a3')}
located in site or site collection ${chalk.grey('https://contoso.sharepoint.com/sites/test')}
${commands.CUSTOMACTION_GET} --id 058140e3-0e37-44fc-a1d3-79c487d371a3 --url https://contoso.sharepoint.com/sites/test
Return details about the user custom action with ID ${chalk.grey('058140e3-0e37-44fc-a1d3-79c487d371a3')}
located in site collection ${chalk.grey('https://contoso.sharepoint.com/sites/test')}
${commands.CUSTOMACTION_GET} -i 058140e3-0e37-44fc-a1d3-79c487d371a3 -u https://contoso.sharepoint.com/sites/test -s Site
Return details about the user custom action with ID ${chalk.grey('058140e3-0e37-44fc-a1d3-79c487d371a3')}
located in site ${chalk.grey('https://contoso.sharepoint.com/sites/test')}
${commands.CUSTOMACTION_GET} --id 058140e3-0e37-44fc-a1d3-79c487d371a3 --url https://contoso.sharepoint.com/sites/test --scope Web
More information:
UserCustomAction REST API resources:
https://msdn.microsoft.com/en-us/library/office/dn531432.aspx#bk_UserCustomAction
`);
}
}
module.exports = new SpoCustomActionGetCommand();