Skip to content

Commit 0237aa9

Browse files
arjunumenonwaldekmastykarz
authored andcommitted
Extends 'spo web set' with welcomePage. Closes pnp#1730
1 parent 1a86c73 commit 0237aa9

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

docs/docs/about/comparison-powershell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ Set-PnPGroup|SharePointPnPPowerShellOnline|
632632
Set-PnPGroupPermissions|SharePointPnPPowerShellOnline|
633633
Set-PnPHomeSite|SharePointPnPPowerShellOnline|[spo homesite set](../cmd/spo/homesite/homesite-set.md)
634634
Set-PnPHideDefaultThemes|SharePointPnPPowerShellOnline|[spo hidedefaultthemes set](../cmd/spo/hidedefaultthemes/hidedefaultthemes-set.md)
635-
Set-PnPHomePage|SharePointPnPPowerShellOnline|
635+
Set-PnPHomePage|SharePointPnPPowerShellOnline|[spo web set](../cmd/spo/web/web-set.md)
636636
Set-PnPHubSite|SharePointPnPPowerShellOnline|[spo hubsite set](../cmd/spo/hubsite/hubsite-set.md)
637637
Set-PnPIndexedProperties|SharePointPnPPowerShellOnline|
638638
Set-PnPInPlaceRecordsManagement|SharePointPnPPowerShellOnline|[spo site inplacerecordsmanagement set](../cmd/spo/site/site-inplacerecordsmanagement-set.md)

docs/docs/cmd/spo/web/web-set.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Option|Description
2424
`--footerEnabled [footerEnabled]`|Set to `true` to enable footer and to `false` to disable it
2525
`--searchScope [searchScope]`|Search scope to set in the site. Allowed values `DefaultScope,Tenant,Hub,Site`
2626
`--query [query]`|JMESPath query string. See [http://jmespath.org/](http://jmespath.org/) for more information and examples
27+
`--welcomePage [welcomePage]`|Site-relative URL of the welcome page for the site
2728
`-o, --output [output]`|Output type. `json,text`. Default `text`
2829
`--verbose`|Runs command with verbose logging
2930
`--debug`|Runs command with debug logging
@@ -76,6 +77,12 @@ Set search scope to tenant scope
7677
spo web set --webUrl https://contoso.sharepoint.com/sites/team-a --searchScope tenant
7778
```
7879

80+
Set welcome page for the web
81+
82+
```sh
83+
spo web set --webUrl https://contoso.sharepoint.com/sites/team-a --welcomePage "SitePages/new-home.aspx"
84+
```
85+
7986
## More information
8087

8188
- Web properties: [https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ee545886(v=office.15)](https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ee545886(v=office.15))

src/m365/spo/commands/web/web-set.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,44 @@ describe(commands.WEB_SET, () => {
373373
});
374374
});
375375

376+
it('Update Welcome page', (done) => {
377+
sinon.stub(request, 'patch').callsFake((opts) => {
378+
if (opts.url === 'https://contoso.sharepoint.com/sites/team-a/_api/web') {
379+
return Promise.resolve();
380+
}
381+
return Promise.reject('Invalid request');
382+
});
383+
384+
cmdInstance.action({ options: { debug: false, welcomePage: 'SitePages/Home.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a' } }, () => {
385+
try {
386+
assert(cmdInstanceLogSpy.notCalled);
387+
done();
388+
}
389+
catch (e) {
390+
done(e);
391+
}
392+
});
393+
});
394+
395+
it('Update Welcome page (debug)', (done) => {
396+
sinon.stub(request, 'patch').callsFake((opts) => {
397+
if (opts.url === 'https://contoso.sharepoint.com/sites/team-a/_api/web') {
398+
return Promise.resolve();
399+
}
400+
return Promise.reject('Invalid request');
401+
});
402+
403+
cmdInstance.action({ options: { debug: true, welcomePage: 'SitePages/Home.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a' } }, () => {
404+
try {
405+
assert(cmdInstanceLogSpy.called);
406+
done();
407+
}
408+
catch (e) {
409+
done(e);
410+
}
411+
});
412+
});
413+
376414
it('correctly handles error when hub site not found', (done) => {
377415
sinon.stub(request, 'patch').callsFake((opts) => {
378416
return Promise.reject({
@@ -399,6 +437,32 @@ describe(commands.WEB_SET, () => {
399437
});
400438
});
401439

440+
it('correctly handles error while updating Welcome page', (done) => {
441+
sinon.stub(request, 'patch').callsFake((opts) => {
442+
return Promise.reject({
443+
error: {
444+
"odata.error": {
445+
"code": "-1, Microsoft.SharePoint.Client.ResourceNotFoundException",
446+
"message": {
447+
"lang": "en-US",
448+
"value": "The WelcomePage property must be a path that is relative to the folder, and the path cannot contain two consecutive periods (..)."
449+
}
450+
}
451+
}
452+
});
453+
});
454+
455+
cmdInstance.action({ options: { debug: false, welcomePage: 'https://contoso.sharepoint.com/sites/team-a/SitePages/Home.aspx', webUrl: 'https://contoso.sharepoint.com/sites/team-a'} }, (err?: any) => {
456+
try {
457+
assert.equal(JSON.stringify(err), JSON.stringify(new CommandError("The WelcomePage property must be a path that is relative to the folder, and the path cannot contain two consecutive periods (..).")));
458+
done();
459+
}
460+
catch (e) {
461+
done(e);
462+
}
463+
});
464+
});
465+
402466
it('supports debug mode', () => {
403467
const options = (command.options() as CommandOption[]);
404468
let containsOption = false;

src/m365/spo/commands/web/web-set.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface Options extends GlobalOptions {
2323
webUrl: string;
2424
footerEnabled?: string;
2525
searchScope?: string;
26+
welcomePage?: string;
2627
}
2728

2829
class SpoWebSetCommand extends SpoCommand {
@@ -48,6 +49,7 @@ class SpoWebSetCommand extends SpoCommand {
4849
telemetryProps.quickLaunchEnabled = typeof args.options.quickLaunchEnabled !== 'undefined';
4950
telemetryProps.footerEnabled = typeof args.options.footerEnabled !== 'undefined';
5051
telemetryProps.searchScope = args.options.searchScope !== 'undefined';
52+
telemetryProps.welcomePage = args.options.welcomePage !== 'undefined';
5153
this.trackUnknownOptions(telemetryProps, args.options);
5254
return telemetryProps;
5355
}
@@ -102,6 +104,29 @@ class SpoWebSetCommand extends SpoCommand {
102104

103105
request
104106
.patch(requestOptions)
107+
.then((): Promise<void> => {
108+
if (typeof args.options.welcomePage === 'undefined') {
109+
return Promise.resolve();
110+
}
111+
112+
if (this.verbose) {
113+
cmd.log(`Updating Welcome page for the site ${args.options.webUrl}`);
114+
}
115+
116+
const requestOptions: any = {
117+
url: `${args.options.webUrl}/_api/web/rootfolder`,
118+
headers: {
119+
'Content-Type': 'application/json;odata=nometadata',
120+
accept: 'application/json;odata=nometadata',
121+
'IF-MATCH': '*',
122+
'X-HTTP-Method': 'PATCH'
123+
},
124+
body: { WelcomePage: args.options.welcomePage },
125+
json: true
126+
};
127+
128+
return request.patch(requestOptions)
129+
})
105130
.then((): void => {
106131
if (this.debug) {
107132
cmd.log(vorpal.chalk.green('DONE'));
@@ -161,6 +186,10 @@ class SpoWebSetCommand extends SpoCommand {
161186
option: '--searchScope [searchScope]',
162187
description: 'Search scope to set in the site. Allowed values DefaultScope|Tenant|Hub|Site',
163188
autocomplete: SpoWebSetCommand.searchScopeOptions
189+
},
190+
{
191+
option: '--welcomePage [welcomePage]',
192+
description: 'Site-relative URL of the welcome page for the site'
164193
}
165194
];
166195

@@ -237,9 +266,9 @@ class SpoWebSetCommand extends SpoCommand {
237266
command, you can update the value of any other web property using its
238267
CSOM name, eg. ${chalk.grey('--AllowAutomaticASPXPageIndexing')}. At this
239268
moment, the CLI supports properties of types Boolean, String and Int32.
240-
269+
241270
Examples:
242-
271+
243272
Update subsite title
244273
${commands.WEB_SET} --webUrl https://contoso.sharepoint.com/sites/team-a --title Team-a
245274
@@ -261,6 +290,9 @@ class SpoWebSetCommand extends SpoCommand {
261290
Set search scope to tenant scope
262291
${commands.WEB_SET} --webUrl https://contoso.sharepoint.com/sites/team-a --searchScope tenant
263292
293+
Set welcome page for the web
294+
${commands.WEB_SET} --webUrl https://contoso.sharepoint.com/sites/team-a --welcomePage "SitePages/new-home.aspx"
295+
264296
More information:
265297
266298
Web properties

0 commit comments

Comments
 (0)