Skip to content

Commit

Permalink
Add ability to update site thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
reshmee011 committed Oct 5, 2023
1 parent ee72caa commit dc3884c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
87 changes: 87 additions & 0 deletions src/m365/spo/commands/site/site-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ describe(commands.SITE_SET, () => {
assert.notStrictEqual(actual, true);
});

it('fails validation if siteThumbnailUrl is not a string', async () => {
const actual = await command.validate({ options: { url: 'https://contoso.sharepoint.com/sites/logo', siteThumbnailUrl: true } }, commandInfo);
assert.notStrictEqual(actual, true);
});

it('fails validation if non-GUID value specified for siteDesignId', async () => {
const actual = await command.validate({ options: { url: 'https://contoso.sharepoint.com', siteDesignId: 'Invalid' } }, commandInfo);
assert.notStrictEqual(actual, true);
Expand Down Expand Up @@ -2337,6 +2342,88 @@ describe(commands.SITE_SET, () => {
assert.strictEqual(data.relativeLogoUrl, "");
});

it('applies site relative thumbnail url to the specified site', async () => {
let data: any = {};

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/logo/_api/site?$select=GroupId,Id') {
return {
Id: '255a50b2-527f-4413-8485-57f4c17a24d1',
GroupId: 'e10a459e-60c8-4000-8240-a68d6a12d39e'
};
}

throw 'Invalid request';
});

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/logo/_api/siteiconmanager/setsitelogo') {
data = opts.data;
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { url: 'https://contoso.sharepoint.com/sites/logo', siteThumbnailUrl: "/sites/logo/SiteAssets/parker-ms-1200.png" } });
assert.strictEqual(data.relativeLogoUrl, "/sites/logo/SiteAssets/parker-ms-1200.png");
});

it('applies site absolute thumbnail url to the specified site', async () => {
let data: any = {};

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/logo/_api/site?$select=GroupId,Id') {
return {
Id: '255a50b2-527f-4413-8485-57f4c17a24d1',
GroupId: 'e10a459e-60c8-4000-8240-a68d6a12d39e'
};
}

throw 'Invalid request';
});

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/logo/_api/siteiconmanager/setsitelogo') {
data = opts.data;
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { url: 'https://contoso.sharepoint.com/sites/logo', siteThumbnailUrl: "https://contoso.sharepoint.com/sites/logo/SiteAssets/parker-ms-1200.png" } });
assert.strictEqual(data.relativeLogoUrl, "/sites/logo/SiteAssets/parker-ms-1200.png");
});

it('correctly handles unsetting the thumbnail from the specified site', async () => {
let data: any = {};

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/logo/_api/site?$select=GroupId,Id') {
return {
Id: '255a50b2-527f-4413-8485-57f4c17a24d1',
GroupId: 'e10a459e-60c8-4000-8240-a68d6a12d39e'
};
}

throw 'Invalid request';
});

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/logo/_api/siteiconmanager/setsitelogo') {
data = opts.data;
return;
}

throw 'Invalid request';
});

await command.action(logger, { options: { debug: true, url: 'https://contoso.sharepoint.com/sites/logo', siteThumbnailUrl: "" } });
assert.strictEqual(data.relativeLogoUrl, "");
});


it('correctly handles error when applying site design to the specified site', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === 'https://contoso.sharepoint.com/sites/Sales/_api/site?$select=GroupId,Id') {
Expand Down
44 changes: 42 additions & 2 deletions src/m365/spo/commands/site/site-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Options extends GlobalOptions {
url: string;
sharingCapability?: string;
siteLogoUrl?: string;
siteThumbnailUrl?: string;
resourceQuota?: string | number;
resourceQuotaWarningLevel?: string | number;
storageQuota?: string | number;
Expand Down Expand Up @@ -82,6 +83,7 @@ class SpoSiteSetCommand extends SpoCommand {
siteDesignId: typeof args.options.siteDesignId !== undefined,
sharingCapabilities: args.options.sharingCapability,
siteLogoUrl: typeof args.options.siteLogoUrl !== 'undefined',
siteThumbnailUrl: typeof args.options.siteThumbnailUrl !== 'undefined',
resourceQuota: args.options.resourceQuota,
resourceQuotaWarningLevel: args.options.resourceQuotaWarningLevel,
storageQuota: args.options.storageQuota,
Expand Down Expand Up @@ -136,6 +138,9 @@ class SpoSiteSetCommand extends SpoCommand {
{
option: '--siteLogoUrl [siteLogoUrl]'
},
{
option: '--siteThumbnailUrl [siteThumbnailUrl]'
},
{
option: '--sharingCapability [sharingCapability]',
autocomplete: this.sharingCapabilities
Expand Down Expand Up @@ -189,6 +194,7 @@ class SpoSiteSetCommand extends SpoCommand {
typeof args.options.siteDesignId === 'undefined' &&
typeof args.options.sharingCapability === 'undefined' &&
typeof args.options.siteLogoUrl === 'undefined' &&
typeof args.options.siteThumbnailUrl === 'undefined' &&
typeof args.options.resourceQuota === 'undefined' &&
typeof args.options.resourceQuotaWarningLevel === 'undefined' &&
typeof args.options.storageQuota === 'undefined' &&
Expand All @@ -203,6 +209,10 @@ class SpoSiteSetCommand extends SpoCommand {
return `${args.options.siteLogoUrl} is not a valid value for the siteLogoUrl option. Specify the logo URL or an empty string "" to unset the logo.`;
}

if (typeof args.options.siteThumbnailUrl !== 'undefined' && typeof args.options.siteThumbnailUrl !== 'string') {
return `${args.options.siteThumbnailUrl} is not a valid value for the siteThumbnailUrl option. Specify the logo URL or an empty string "" to unset the logo.`;
}

if (args.options.siteDesignId) {
if (!validation.isValidGuid(args.options.siteDesignId)) {
return `${args.options.siteDesignId} is not a valid GUID`;
Expand Down Expand Up @@ -280,6 +290,7 @@ class SpoSiteSetCommand extends SpoCommand {
await this.waitForSiteUpdateCompletion(logger, args, siteProps);
await this.applySiteDesign(logger, args);
await this.setLogo(logger, args);
await this.setThumbnail(logger, args);
const lockState = await this.updateSiteLockState(logger, args);
await this.waitForSiteUpdateCompletion(logger, args, lockState);
}
Expand All @@ -292,7 +303,7 @@ class SpoSiteSetCommand extends SpoCommand {
}
}

private async setLogoThumbNail(logger: Logger, args: CommandArgs): Promise<void> {
private async setLogo(logger: Logger, args: CommandArgs): Promise<void> {
if (typeof args.options.siteLogoUrl === 'undefined') {
return Promise.resolve();
}
Expand All @@ -309,7 +320,36 @@ class SpoSiteSetCommand extends SpoCommand {
accept: 'application/json;odata=nometadata'
},
data: {
relativeLogoUrl: logoUrl
aspect: 1,
relativeLogoUrl: logoUrl,
type: 0
},
responseType: 'json'
};

return request.post(requestOptions);
}

private async setThumbnail(logger: Logger, args: CommandArgs): Promise<void> {
if (typeof args.options.siteThumbnailUrl === 'undefined') {
return Promise.resolve();
}

if (this.debug) {
await logger.logToStderr(`Setting the site its Thumbnail...`);
}

const thumbnailUrl = args.options.siteThumbnailUrl ? urlUtil.getServerRelativePath(args.options.url, args.options.siteThumbnailUrl) : "";

const requestOptions: any = {
url: `${args.options.url}/_api/siteiconmanager/setsitelogo`,
headers: {
accept: 'application/json;odata=nometadata'
},
data: {
aspect: 0,
relativeLogoUrl: thumbnailUrl,
type: 0
},
responseType: 'json'
};
Expand Down

0 comments on commit dc3884c

Please sign in to comment.