Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added internalName option for spo field commands #6379

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/docs/cmd/spo/field/field-get.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ m365 spo field get [options]
: Server- or web-relative URL of the list where the field is located. Specify either `listTitle`, `listId` or `listUrl`.

`-i, --id [id]`
: The ID of the field to retrieve. Specify `id` or `title` but not both.
: The ID of the field to retrieve. Specify either `id`, `title` or `internalName`.

`-t, --title [title]`
: The display name (case-sensitive) of the field to retrieve. Specify `id` or `title` but not both.
: The display name (case-sensitive) of the field to retrieve. Specify either `id`, `title` or `internalName`.
```

`--internalName [internalName]`
: The internal name (case-sensitive) of the field to retrieve. Specify either `id`, `title` or `internalName`.
```

<Global />
Expand All @@ -56,6 +60,12 @@ Retrieves list column by display name located in the specified site. Retrieves t
m365 spo field get --webUrl https://contoso.sharepoint.com/sites/contoso-sales --listUrl "Lists/Events" --title "Title"
```

Retrieves list column by internal name located in the specified site. Retrieves the list by its url.

```sh
m365 spo field get --webUrl https://contoso.sharepoint.com/sites/contoso-sales --listUrl "Lists/Events" --internalName "Title"
```

## Response

<Tabs>
Expand Down
15 changes: 12 additions & 3 deletions docs/docs/cmd/spo/field/field-remove.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ m365 spo field remove [options]
: Server- or web-relative URL of the list where the field is located. Specify either `listTitle`, `listId` or `listUrl`.

`-i, --id [id]`
: The ID of the field to remove. Specify either `id`, `title`, or `group`.
: The ID of the field to remove. Specify either `id`, `title`, `internalName`, or `group`.

`-t, --title [title]`
: The display name (case-sensitive) of the field to remove. Specify either `id`, `title`, or `group`.
: The display name (case-sensitive) of the field to remove. Specify either `id`, `title`, `internalName`, or `group`.

`--internalName [internalName]`
: The internal name (case-sensitive) of the field to remove. Specify either `id`, `title`, `internalName`, or `group`.

`-g, --group [group]`
: Delete all fields from this group (case-sensitive). Specify either `id`, `title`, or `group`.
: Delete all fields from this group (case-sensitive). Specify either `id`, `title`, `internalName`, or `group`.

`-f, --force`
: Don't prompt for confirming removing the field.
Expand Down Expand Up @@ -60,6 +63,12 @@ Remove the list column with the specified display name, located in the specified
m365 spo field remove --webUrl https://contoso.sharepoint.com/sites/contoso-sales --listUrl "Lists/Events" --title "Title"
```

Remove the list column with the specified display name, located in the specified site. Retrieves the list by its url.

```sh
m365 spo field remove --webUrl https://contoso.sharepoint.com/sites/contoso-sales --listUrl "Lists/Events" --internalName "Title"
```

Remove all site columns from the specified group.

```sh
Expand Down
20 changes: 17 additions & 3 deletions docs/docs/cmd/spo/field/field-set.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ m365 spo field set [options]
: Server- or site-relative URL of the list where the field is located (if list column). Specify either `listTitle`, `listId` or `listUrl`.

`-i, --id [id]`
: ID of the field to update. Specify either `id` or `title` but not both.
: ID of the field to update. Specify either `id`, `title`, or 'internalName' but not all.

`-t, --title [title]`
: Title or internal name of the field to update. Specify either `id` or `title` but not both.
: Title of the field to update. Specify either `id`, `title`, or 'internalName' but not all.

`--internalName [internalName]`
: Internal name of the field to update. Specify either `id`, `title`, or 'internalName' but not all.

`--updateExistingLists`
: Set, to push the update to existing lists. Otherwise, the changes will apply to new lists only.
Expand All @@ -51,6 +54,12 @@ When updating column formatting for a field with the `--CustomFormatter` option,

Update the title of the site column specified by its internal name and push changes to existing lists.

```sh
m365 spo field set --webUrl https://contoso.sharepoint.com/sites/project-x --internalName 'MyColumn' --updateExistingLists --Title 'My column'
```

Update the title of the site column specified by its title and push changes to existing lists.

```sh
m365 spo field set --webUrl https://contoso.sharepoint.com/sites/project-x --title 'MyColumn' --updateExistingLists --Title 'My column'
```
Expand All @@ -67,11 +76,16 @@ Update the description of a column specified by the ID on a list retrieved by th
m365 spo field set --webUrl https://contoso.sharepoint.com/sites/project-x --listUrl '/sites/project-x/Lists/My List' --id 330f29c5-5c4c-465f-9f4b-7903020ae1ce --Description 'My column Description'
```

Update column formatting of the specified list column.
Update column formatting of the specified list column based on title.

```sh
m365 spo field set --webUrl https://contoso.sharepoint.com/sites/project-x --listTitle 'My List' --title 'MyColumn' --CustomFormatter '{"schema":"https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "elmType": "div", "txtContent": "@currentField"}'
```
Update column formatting of the specified list column based on internalName.

```sh
m365 spo field set --webUrl https://contoso.sharepoint.com/sites/project-x --listTitle 'My List' --internalName 'MyColumn' --CustomFormatter '{"schema":"https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "elmType": "div", "txtContent": "@currentField"}'
```

## Response

Expand Down
73 changes: 60 additions & 13 deletions src/m365/spo/commands/field/field-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe(commands.FIELD_GET, () => {

it('gets information about a site column', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/fields/getbyid('5ee2dd25-d941-455a-9bdb-7f2c54aed11b')`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/fields/getbyid('5ee2dd25-d941-455a-9bdb-7f2c54aed11b')`) {
return {
"AutoIndexed": false,
"CanBeDeleted": true,
Expand Down Expand Up @@ -177,7 +177,7 @@ describe(commands.FIELD_GET, () => {

it('gets information about a list column', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists/getByTitle('Documents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle('Documents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) {
return {
"AutoIndexed": false,
"CanBeDeleted": false,
Expand Down Expand Up @@ -262,22 +262,21 @@ describe(commands.FIELD_GET, () => {

it('should call the correct GET url when id and list url specified', async () => {
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/GetList('%2Fsites%2Fportal%2FLists%2FEvents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) {
return {
"Id": "03e45e84-1992-4d42-9116-26f756012634"
};
}

throw 'Invalid request';
});

await assert.rejects(command.action(logger, { options: { verbose: true, webUrl: 'https://contoso.sharepoint.com/sites/portal', id: '03e45e84-1992-4d42-9116-26f756012634', listUrl: 'Lists/Events' } }));
await command.action(logger, { options: { debug: true, verbose: true, webUrl: 'https://contoso.sharepoint.com/sites/portal', id: '03e45e84-1992-4d42-9116-26f756012634', listUrl: 'Lists/Events' } });
assert.strictEqual(getStub.lastCall.args[0].url, 'https://contoso.sharepoint.com/sites/portal/_api/web/GetList(\'%2Fsites%2Fportal%2FLists%2FEvents\')/fields/getbyid(\'03e45e84-1992-4d42-9116-26f756012634\')');
});

it('should call the correct GET url when field title and list title specified (verbose)', async () => {
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle('Documents')/fields/getbyinternalnameortitle('Title')`) {
return {
"Id": "03e45e84-1992-4d42-9116-26f756012634"
};
Expand All @@ -292,7 +291,7 @@ describe(commands.FIELD_GET, () => {

it('should call the correct GET url when field title and list title specified', async () => {
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle('Documents')/fields/getbyinternalnameortitle('Title')`) {
return {
"Id": "03e45e84-1992-4d42-9116-26f756012634"
};
Expand All @@ -306,8 +305,9 @@ describe(commands.FIELD_GET, () => {
});

it('should call the correct GET url when field title and list url specified', async () => {
const webUrl = 'https://contoso.sharepoint.com/sites/portal';
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists`) > -1) {
if (opts.url === `${webUrl}/_api/web/lists(guid'03e45e84-1992-4d42-9116-26f756012634')/fields/getbyinternalnameortitle('Title')`) {
return {
"Id": "03e45e84-1992-4d42-9116-26f756012634"
};
Expand All @@ -320,16 +320,63 @@ describe(commands.FIELD_GET, () => {
assert.strictEqual(getStub.lastCall.args[0].url, 'https://contoso.sharepoint.com/sites/portal/_api/web/lists(guid\'03e45e84-1992-4d42-9116-26f756012634\')/fields/getbyinternalnameortitle(\'Title\')');
});

it('should call the correct GET url when field internalName and list title specified (verbose)', async () => {
const webUrl = 'https://contoso.sharepoint.com/sites/portal';
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `${webUrl}/_api/web/lists/getByTitle('Documents')/fields/getbyinternalnameortitle('Title')`) {
return {
"Id": "03e45e84-1992-4d42-9116-26f756012634"
};
}

throw 'Invalid request';
});

await command.action(logger, { options: { debug: true, verbose: true, webUrl: 'https://contoso.sharepoint.com/sites/portal', internalName: 'Title', listTitle: 'Documents' } });
assert.strictEqual(getStub.lastCall.args[0].url, 'https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle(\'Documents\')/fields/getbyinternalnameortitle(\'Title\')');
});

it('should call the correct GET url when field internalName and list title specified', async () => {
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle('Documents')/fields/getbyinternalnameortitle('Title')`) {
return {
"Id": "03e45e84-1992-4d42-9116-26f756012634"
};
}

throw 'Invalid request';
});

await command.action(logger, { options: { webUrl: 'https://contoso.sharepoint.com/sites/portal', internalName: 'Title', listTitle: 'Documents' } });
assert.strictEqual(getStub.lastCall.args[0].url, 'https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle(\'Documents\')/fields/getbyinternalnameortitle(\'Title\')');
});

it('should call the correct GET url when field internalName and list url specified', async () => {
const webUrl = 'https://contoso.sharepoint.com/sites/portal';
const getStub = sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `${webUrl}/_api/web/lists(guid'03e45e84-1992-4d42-9116-26f756012634')/fields/getbyinternalnameortitle('Title')`) {
return {
'Id': '03e45e84-1992-4d42-9116-26f756012634'
};
}

throw 'Invalid request';
});

await command.action(logger, { options: { debug: true, webUrl: 'https://contoso.sharepoint.com/sites/portal', internalName: 'Title', listId: '03e45e84-1992-4d42-9116-26f756012634' } });
assert.strictEqual(getStub.lastCall.args[0].url, 'https://contoso.sharepoint.com/sites/portal/_api/web/lists(guid\'03e45e84-1992-4d42-9116-26f756012634\')/fields/getbyinternalnameortitle(\'Title\')');
});

it('correctly handles site column not found', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) {
throw {
error: {
"odata.error": {
"code": "-2147024809, System.ArgumentException",
"message": {
"lang": "en-US",
"value": "Invalid field name. {03e45e84-1992-4d42-9116-26f756012634} https://m365x526922.sharepoint.com/sites/portal "
"value": "Invalid field name. {03e45e84-1992-4d42-9116-26f756012634} https://contoso.sharepoint.com/sites/portal "
}
}
}
Expand All @@ -340,12 +387,12 @@ describe(commands.FIELD_GET, () => {
});

await assert.rejects(command.action(logger, { options: { debug: true, webUrl: 'https://contoso.sharepoint.com/sites/portal', id: '03e45e84-1992-4d42-9116-26f756012634' } } as any),
new CommandError('Invalid field name. {03e45e84-1992-4d42-9116-26f756012634} https://m365x526922.sharepoint.com/sites/portal '));
new CommandError('Invalid field name. {03e45e84-1992-4d42-9116-26f756012634} https://contoso.sharepoint.com/sites/portal '));
});

it('correctly handles list column not found', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists/getByTitle('Documents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle('Documents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) {
throw {
error: {
"odata.error": {
Expand All @@ -368,7 +415,7 @@ describe(commands.FIELD_GET, () => {

it('correctly handles list not found', async () => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf(`/_api/web/lists/getByTitle('Documents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) > -1) {
if (opts.url === `https://contoso.sharepoint.com/sites/portal/_api/web/lists/getByTitle('Documents')/fields/getbyid('03e45e84-1992-4d42-9116-26f756012634')`) {
throw {
error: {
"odata.error": {
Expand Down
11 changes: 8 additions & 3 deletions src/m365/spo/commands/field/field-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Options extends GlobalOptions {
listUrl?: string;
id?: string;
title?: string;
internalName?: string;
}

class SpoFieldGetCommand extends SpoCommand {
Expand Down Expand Up @@ -45,7 +46,8 @@ class SpoFieldGetCommand extends SpoCommand {
listTitle: typeof args.options.listTitle !== 'undefined',
listUrl: typeof args.options.listUrl !== 'undefined',
id: typeof args.options.id !== 'undefined',
title: typeof args.options.title !== 'undefined'
title: typeof args.options.title !== 'undefined',
internalName: typeof args.options.internalName !== 'undefined'
});
});
}
Expand All @@ -69,6 +71,9 @@ class SpoFieldGetCommand extends SpoCommand {
},
{
option: '-t, --title [title]'
},
{
option: '--internalName [internalName]'
}
);
}
Expand All @@ -95,7 +100,7 @@ class SpoFieldGetCommand extends SpoCommand {
}

#initOptionSets(): void {
this.optionSets.push({ options: ['id', 'title'] });
this.optionSets.push({ options: ['id', 'title', 'internalName'] });
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
Expand All @@ -118,7 +123,7 @@ class SpoFieldGetCommand extends SpoCommand {
fieldRestUrl = `/getbyid('${formatting.encodeQueryParameter(args.options.id)}')`;
}
else {
fieldRestUrl = `/getbyinternalnameortitle('${formatting.encodeQueryParameter(args.options.title as string)}')`;
fieldRestUrl = `/getbyinternalnameortitle('${formatting.encodeQueryParameter((args.options.title || args.options.internalName) as string)}')`;
}

const requestOptions: CliRequestOptions = {
Expand Down
Loading
Loading