-
Notifications
You must be signed in to change notification settings - Fork 308
/
contenttype-remove.ts
208 lines (175 loc) · 6.44 KB
/
contenttype-remove.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
207
208
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;
id?: string;
name?: string;
confirm?: boolean;
}
class SpoContentTypeRemoveCommand extends SpoCommand {
public get name(): string {
return `${commands.CONTENTTYPE_REMOVE}`;
}
public get description(): string {
return 'Deletes site content type';
}
public types(): CommandTypes | undefined {
return {
string: ['id', 'i']
};
}
public getTelemetryProperties(args: CommandArgs): any {
const telemetryProps: any = super.getTelemetryProperties(args);
telemetryProps.id = typeof args.options.id !== 'undefined';
telemetryProps.name = typeof args.options.name !== 'undefined';
telemetryProps.confirm = (!(!args.options.confirm)).toString();
return telemetryProps;
}
public commandAction(cmd: CommandInstance, args: CommandArgs, cb: (err?: any) => void): void {
let contentTypeId: string = '';
const contentTypeIdentifierLabel: string = args.options.id ?
`with id ${args.options.id}` :
`with name ${args.options.name}`;
const removeContentType = (): void => {
((): Promise<any> => {
if (this.debug) {
cmd.log(`Retrieving information about the content type ${contentTypeIdentifierLabel}...`);
}
if (args.options.id) {
return Promise.resolve({ "value": [{ "StringId": args.options.id }] });
}
if (this.verbose) {
cmd.log(`Looking up the ID of content type ${contentTypeIdentifierLabel}...`);
}
const requestOptions: any = {
url: `${args.options.webUrl}/_api/web/availableContentTypes?$filter=(Name eq '${encodeURIComponent(args.options.name as string)}')`,
headers: {
accept: 'application/json;odata=nometadata'
},
json: true
};
return request.get(requestOptions);
})()
.then((contentTypeIdResult: { value: { StringId: string }[] }): Promise<any> => {
if (contentTypeIdResult &&
contentTypeIdResult.value &&
contentTypeIdResult.value.length > 0) {
contentTypeId = contentTypeIdResult.value[0].StringId;
//execute delete operation
const requestOptions: any = {
url: `${args.options.webUrl}/_api/web/contenttypes('${encodeURIComponent(contentTypeId)}')`,
headers: {
'X-HTTP-Method': 'DELETE',
'If-Match': '*',
'accept': 'application/json;odata=nometadata'
},
json: true
};
return request.post(requestOptions);
}
else {
return Promise.resolve({ "odata.null": true });
}
})
.then((res): void => {
if (res && res["odata.null"] === true) {
cb(new CommandError(`Content type not found`));
return;
}
else {
if (this.verbose) {
cmd.log(vorpal.chalk.green('DONE'));
}
}
cb();
}, (err: any): void => {
this.handleRejectedODataJsonPromise(err, cmd, cb);
});
}
if (args.options.confirm) {
removeContentType();
}
else {
cmd.prompt({ type: 'confirm', name: 'continue', default: false, message: `Are you sure you want to remove the content type ${args.options.id || args.options.name}?` }, (result: { continue: boolean }): void => {
if (!result.continue) {
cb();
}
else {
removeContentType();
}
});
}
}
public options(): CommandOption[] {
const options: CommandOption[] = [
{
option: '-u, --webUrl <webUrl>',
description: 'Absolute URL of the site where the content type is located'
},
{
option: '-i, --id [id]',
description: 'The ID of the content type to remove'
},
{
option: '-n, --name [name]',
description: 'The name of the content type to remove'
},
{
option: '--confirm',
description: 'Don\'t prompt for confirming removal of the content type'
}
];
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 && !args.options.name) {
return 'Specify either the id or the name';
}
if (args.options.id && args.options.name) {
return 'Specify either the id or the name but not both';
}
return true;
};
}
public commandHelp(args: {}, log: (help: string) => void): void {
const chalk = vorpal.chalk;
log(vorpal.find(this.name).helpInformation());
log(
` Remarks:
If the specified content type is in use by a list and cannot be removed,
you will be returned the error:
${chalk.grey('Another site or list is still using this content type.')}
SharePoint will not allow a content type to be removed unless any
dependent objects are also emptied from the recycle bin including
the second-stage recycle bin.
The content type you wish to remove can be selected by the ID or Name
of the content type. Either ID or Name parameter must be specified.
Examples:
Remove a site content type by ID
${this.name} --webUrl https://contoso.sharepoint.com/sites/contoso-sales --id 0x01007926A45D687BA842B947286090B8F67D
Remove a site content type by Name
${this.name} --webUrl https://contoso.sharepoint.com/sites/contoso-sales --name 'My Content Type'
Remove a site content type without prompting for confirmation
${this.name} --webUrl https://contoso.sharepoint.com/sites/contoso-sales --name 'My Content Type' --confirm
`);
}
}
module.exports = new SpoContentTypeRemoveCommand();