Skip to content

Commit

Permalink
Adds content flag to 'spo sitescript get'. Closes #5549
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlingstuyl committed Oct 12, 2023
1 parent 8536cf7 commit d6e0b2c
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
72 changes: 71 additions & 1 deletion docs/docs/cmd/spo/sitescript/sitescript-get.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ m365 spo sitescript get [options]
```md definition-list
`-i, --id <id>`
: Site script ID

`-c, --content`
: Specify to only retrieve the content of the site script.
```

<Global />
Expand All @@ -33,8 +36,16 @@ Get information about the site script with ID _2c1ba4c4-cd9b-4417-832f-92a34bc34
m365 spo sitescript get --id 2c1ba4c4-cd9b-4417-832f-92a34bc34b2a
```

Returns the site script contents:

```sh
m365 spo sitescript get --id 2c1ba4c4-cd9b-4417-832f-92a34bc34b2a --content
```

## Response

### Standard response

<Tabs>
<TabItem value="JSON">

Expand Down Expand Up @@ -67,7 +78,8 @@ m365 spo sitescript get --id 2c1ba4c4-cd9b-4417-832f-92a34bc34b2a
<TabItem value="CSV">

```csv
Content,Id,IsSiteScriptPackage,Title,Version
"{\"$schema\":\"https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json\",\"actions\":[{\"verb\":\"activateSPFeature\",\"name\":\"SiteNotebook feature\",\"title\":\"SiteNotebook feature\",\"featureId\":\"f151bb39-7c3b-414f-bb36-6bf18872052f\",\"scope\":\"web\"}],\"bindata\":{},\"version\":1}",43d4ffa0-c7ee-4a97-91d7-db27e5b62de5,,Contoso,1
```

</TabItem>
Expand All @@ -93,6 +105,64 @@ m365 spo sitescript get --id 2c1ba4c4-cd9b-4417-832f-92a34bc34b2a
</TabItem>
</Tabs>

### `content` response

When we make use of the option `content` the response will differ.

<Tabs>
<TabItem value="JSON">

```json
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json",
"actions": [
{
"verb":"activateSPFeature",
"name": "SiteNotebook feature",
"title": "SiteNotebook feature",
"featureId": "f151bb39-7c3b-414f-bb36-6bf18872052f",
"scope": "web"
}
],
"bindata":{},
"version":1
}
```

</TabItem>
<TabItem value="Text">

```text
$schema : https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json
actions : [{"verb":"createSPList","listName":"Customer Tracking","templateType":100,"subactions":[{"verb":"setDescription","description":"List of Customers and Orders"},{"verb":"addSPField","fieldType":"Text","displayName":"Customer Name","isRequired":false,"addToDefaultView":true},{"verb":"addSPField","fieldType":"Number","displayName":"Requisition Total","addToDefaultView":true,"isRequired":true},{"verb":"addSPField","fieldType":"User","displayName":"Contact","addToDefaultView":true,"isRequired":true},{"verb":"addSPField","fieldType":"Note","displayName":"Meeting Notes","isRequired":false}]}]
version : 1
```

</TabItem>
<TabItem value="CSV">

```csv
$schema,version
https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json,1
```

</TabItem>
<TabItem value="Markdown">

```md
# spo sitescript get --id "43d4ffa0-c7ee-4a97-91d7-db27e5b62de5" --content "true"

Date: 2023-06-22

Property | Value
---------|-------
$schema | https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json
version | 1
```

</TabItem>
</Tabs>

## More information

- SharePoint site design and site script overview: [https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-overview](https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-overview)
46 changes: 44 additions & 2 deletions src/m365/spo/commands/sitescript/sitescript-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe(commands.SITESCRIPT_GET, () => {
});

await command.action(logger, { options: { id: '0f27a016-d277-4bb4-b3c3-b5b040c9559b' } });
assert(loggerLogSpy.calledWith({
assert(loggerLogSpy.calledOnceWithExactly({
"Content": JSON.stringify({
"$schema": "schema.json",
"actions": [
Expand Down Expand Up @@ -149,7 +149,7 @@ describe(commands.SITESCRIPT_GET, () => {
});

await command.action(logger, { options: { debug: true, id: '0f27a016-d277-4bb4-b3c3-b5b040c9559b' } });
assert(loggerLogSpy.calledWith({
assert(loggerLogSpy.calledOnceWithExactly({
"Content": JSON.stringify({
"$schema": "schema.json",
"actions": [
Expand All @@ -168,6 +168,48 @@ describe(commands.SITESCRIPT_GET, () => {
}));
});

it('gets the specified site script contentss', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/Microsoft.Sharepoint.Utilities.WebTemplateExtensions.SiteScriptUtility.GetSiteScriptMetadata`) > -1 &&
JSON.stringify(opts.data) === JSON.stringify({
id: '0f27a016-d277-4bb4-b3c3-b5b040c9559b'
})) {
return {
"Content": JSON.stringify({
"$schema": "schema.json",
"actions": [
{
"verb": "applyTheme",
"themeName": "Contoso Theme"
}
],
"bindata": {},
"version": 1
}),
"Description": "My contoso script",
"Id": "0f27a016-d277-4bb4-b3c3-b5b040c9559b",
"Title": "Contoso",
"Version": 1
};
}

throw 'Invalid request';
});

await command.action(logger, { options: { id: '0f27a016-d277-4bb4-b3c3-b5b040c9559b', content: true } });
assert(loggerLogSpy.calledOnceWithExactly({
"$schema": "schema.json",
"actions": [
{
"verb": "applyTheme",
"themeName": "Contoso Theme"
}
],
"bindata": {},
"version": 1
}));
});

it('correctly handles error when site script not found', async () => {
sinon.stub(request, 'post').rejects({ error: { 'odata.error': { message: { value: 'File Not Found.' } } } });

Expand Down
13 changes: 11 additions & 2 deletions src/m365/spo/commands/sitescript/sitescript-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface CommandArgs {

interface Options extends GlobalOptions {
id: string;
content?: boolean;
}

class SpoSiteScriptGetCommand extends SpoCommand {
Expand All @@ -34,6 +35,9 @@ class SpoSiteScriptGetCommand extends SpoCommand {
this.options.unshift(
{
option: '-i, --id <id>'
},
{
option: '-c, --content'
}
);
}
Expand Down Expand Up @@ -65,8 +69,13 @@ class SpoSiteScriptGetCommand extends SpoCommand {
responseType: 'json'
};

const res: any = await request.post(requestOptions);
await logger.log(res);
const response: any = await request.post(requestOptions);

if (args.options.content === true) {
return await logger.log(JSON.parse(response.Content));
}

await logger.log(response);
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
Expand Down

0 comments on commit d6e0b2c

Please sign in to comment.