-
Notifications
You must be signed in to change notification settings - Fork 308
/
contenttype-get.ts
122 lines (99 loc) · 3.5 KB
/
contenttype-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
import request from '../../../../request';
import commands from '../../commands';
import {
CommandOption, CommandValidate, CommandTypes, CommandError
} from '../../../../Command';
import SpoCommand from '../../../base/SpoCommand';
import GlobalOptions from '../../../../GlobalOptions';
const vorpal: Vorpal = require('../../../../vorpal-init');
interface CommandArgs {
options: Options;
}
interface Options extends GlobalOptions {
webUrl: string;
listTitle?: string;
id: string;
}
class SpoContentTypeGetCommand extends SpoCommand {
public get name(): string {
return `${commands.CONTENTTYPE_GET}`;
}
public get description(): string {
return 'Retrieves information about the specified list or site content type';
}
public types(): CommandTypes | undefined {
return {
string: ['id', 'i']
};
}
public commandAction(cmd: CommandInstance, args: CommandArgs, cb: (err?: any) => void): void {
const requestOptions: any = {
url: `${args.options.webUrl}/_api/web/${(args.options.listTitle ? `lists/getByTitle('${encodeURIComponent(args.options.listTitle)}')/` : '')}contenttypes('${encodeURIComponent(args.options.id)}')`,
headers: {
accept: 'application/json;odata=nometadata'
},
json: true
};
request
.get(requestOptions)
.then((res: any): void => {
if (res['odata.null'] === true) {
cb(new CommandError(`Content type with ID ${args.options.id} not found`));
return;
}
cmd.log(res);
if (this.verbose) {
cmd.log(vorpal.chalk.green('DONE'));
}
cb();
}, (err: any): void => this.handleRejectedODataJsonPromise(err, cmd, cb));
}
public options(): CommandOption[] {
const options: CommandOption[] = [
{
option: '-u, --webUrl <webUrl>',
description: 'Absolute URL of the site where the content type is located'
},
{
option: '-l, --listTitle [listTitle]',
description: 'Title of the list where the content type is located (if it is a list content type)'
},
{
option: '-i, --id <id>',
description: 'The ID of the content type to retrieve'
}
];
const parentOptions: CommandOption[] = super.options();
return options.concat(parentOptions);
}
public validate(): CommandValidate {
return (args: CommandArgs): boolean | string => {
if (!args.options.webUrl) {
return 'Required parameter webUrl missing';
}
const isValidSharePointUrl: boolean | string = SpoCommand.isValidSharePointUrl(args.options.webUrl);
if (isValidSharePointUrl !== true) {
return isValidSharePointUrl;
}
if (!args.options.id) {
return 'Required parameter id missing';
}
return true;
};
}
public commandHelp(args: {}, log: (help: string) => void): void {
const chalk = vorpal.chalk;
log(vorpal.find(this.name).helpInformation());
log(
` Remarks:
If no content type with the specified is found in the site or the list, you
will get the ${chalk.grey('Content type with ID 0x010012 not found')} error.
Examples:
Retrieve site content type
${this.name} --webUrl https://contoso.sharepoint.com/sites/contoso-sales --id 0x0100558D85B7216F6A489A499DB361E1AE2F
Retrieve list content type
${this.name} --webUrl https://contoso.sharepoint.com/sites/contoso-sales --listTitle Events --id 0x0100558D85B7216F6A489A499DB361E1AE2F
`);
}
}
module.exports = new SpoContentTypeGetCommand();