Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodecleyre committed Jan 22, 2024
1 parent 75e0c4f commit 3adebfd
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 37 deletions.
2 changes: 0 additions & 2 deletions src/m365/spo/commands/file/file-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,6 @@ describe(commands.FILE_GET, () => {
assert.strictEqual(getStub.lastCall.args[0].url, `https://contoso.sharepoint.com/sites/project-x/_api/web/GetFileByServerRelativePath(DecodedUrl=@f)?@f='%2Fsites%2Fproject-x%2FDocuments%2FTest1.docx'`);
});



it('uses correct API url when tenant root URL option is passed', async () => {
const getStub: any = sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url as string).indexOf('/_api/web/GetFileByServerRelativePath(') > -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/m365/spo/commands/page/page-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class SpoPageAddCommand extends SpoCommand {
const listUrl: string = urlUtil.getServerRelativePath(args.options.webUrl, listServerRelativeUrl);
const requestUrl = `${args.options.webUrl}/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')`;

await spo.systemUpdateListItem(requestUrl, listItemId, logger, this.verbose, listItemSetOptions, undefined);
await spo.systemUpdateListItem(requestUrl, listItemId, logger, this.verbose, listItemSetOptions);
break;
case 'Template':
requestOptions.url = `${args.options.webUrl}/_api/SitePages/Pages(${listItemId})/SavePageAsTemplate`;
Expand Down
6 changes: 3 additions & 3 deletions src/m365/spo/commands/page/page-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class SpoPageSetCommand extends SpoCommand {
listItemSetOptions.PromotedState = 0;
listItemSetOptions.BannerImageUrl = `${resource}/_layouts/15/images/sitepagethumbnail.png, /_layouts/15/images/sitepagethumbnail.png`;
}
await spo.systemUpdateListItem(requestUrl, itemId, logger, this.verbose, listItemSetOptions, undefined);
await spo.systemUpdateListItem(requestUrl, itemId, logger, this.verbose, listItemSetOptions);
}
if (args.options.promoteAs) {
const requestOptions: CliRequestOptions = {
Expand Down Expand Up @@ -234,7 +234,7 @@ class SpoPageSetCommand extends SpoCommand {
PromotedState: 2,
FirstPublishedDate: new Date().toISOString()
};
await spo.systemUpdateListItem(requestUrl, newsPageItemId, logger, this.verbose, listItemSetOptions, undefined);
await spo.systemUpdateListItem(requestUrl, newsPageItemId, logger, this.verbose, listItemSetOptions);
break;
case 'Template':
const templateItem = await spo.getFileAsListItemByUrl(args.options.webUrl, serverRelativeFileUrl, logger, this.verbose);
Expand Down Expand Up @@ -346,7 +346,7 @@ class SpoPageSetCommand extends SpoCommand {
const listItemSetOptions: any = {
PromotedState: 0
};
await spo.systemUpdateListItem(requestUrl, fileId, logger, this.verbose, listItemSetOptions, undefined);
await spo.systemUpdateListItem(requestUrl, fileId, logger, this.verbose, listItemSetOptions);
}

let requestOptions: CliRequestOptions;
Expand Down
99 changes: 96 additions & 3 deletions src/utils/spo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ describe('utils/spo', () => {
]);
}

if (opts.data === `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">\n <Actions>\n \n <Method Name="ParseAndSetFieldValue" Id="1" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">Title</Parameter>\n <Parameter Type="String">NewTitle</Parameter>\n </Parameters>\n </Method>\n <Method Name="ParseAndSetFieldValue" Id="1" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">customColumn</Parameter>\n <Parameter Type="String">My custom column</Parameter>\n </Parameters>\n </Method>\n <Method Name="ParseAndSetFieldValue" Id="1" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">ContentType</Parameter>\n <Parameter Type="String">Item</Parameter>\n </Parameters>\n </Method>\n <Method Name="SystemUpdate" Id="2" ObjectPathId="147" />\n </Actions>\n <ObjectPaths>\n <Identity Id="147" Name="d704ae73-d5ed-459e-80b0-b8103c5fb6e0|8f2be65d-f195-4699-b0de-24aca3384ba9:site:0ead8b78-89e5-427f-b1bc-6e5a77ac191c:web:4c076c07-e3f1-49a8-ad01-dbb70b263cd7:list:f64041f2-9818-4b67-92ff-3bc5dbbef27e:item:1,1" />\n </ObjectPaths>\n </Request>`) {
if (opts.data === `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">\n <Actions>\n \n <Method Name="ParseAndSetFieldValue" Id="1" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">Title</Parameter>\n <Parameter Type="String">NewTitle</Parameter>\n </Parameters>\n </Method>\n <Method Name="ParseAndSetFieldValue" Id="2" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">customColumn</Parameter>\n <Parameter Type="String">My custom column</Parameter>\n </Parameters>\n </Method>\n <Method Name="ParseAndSetFieldValue" Id="3" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">ContentType</Parameter>\n <Parameter Type="String">Item</Parameter>\n </Parameters>\n </Method>\n <Method Name="SystemUpdate" Id="4" ObjectPathId="147" />\n </Actions>\n <ObjectPaths>\n <Identity Id="147" Name="d704ae73-d5ed-459e-80b0-b8103c5fb6e0|8f2be65d-f195-4699-b0de-24aca3384ba9:site:0ead8b78-89e5-427f-b1bc-6e5a77ac191c:web:4c076c07-e3f1-49a8-ad01-dbb70b263cd7:list:f64041f2-9818-4b67-92ff-3bc5dbbef27e:item:1,1" />\n </ObjectPaths>\n </Request>`) {
return ']SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.7618.1204","ErrorInfo":null,"TraceCorrelationId":"3e3e629e-f0e9-5000-9f31-c6758b453a4a"';
}
}
Expand All @@ -2190,6 +2190,71 @@ describe('utils/spo', () => {
assert.strictEqual(actual, listItemResponse);
});

it(`sets the content type of the list item with system update`, async () => {
const listItemResponse = {
Attachments: false,
AuthorId: 3,
ContentTypeId: '0x0100B21BD271A810EE488B570BE49963EA34',
Created: '2018-03-15T10:43:10Z',
EditorId: 3,
GUID: 'ea093c7b-8ae6-4400-8b75-e2d01154dffc',
ID: 1,
Modified: '2018-03-15T10:52:10Z',
Title: 'NewTitle'
};
const listUrl = '/lists/TestList';
const requestUrl = `https://contoso.sharepoint.com/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')`;

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')?$select=Id`) {
return { Id: 'f64041f2-9818-4b67-92ff-3bc5dbbef27e' };
}

if (opts.url === `https://contoso.sharepoint.com/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')/items(1)`) {
return listItemResponse;
}

throw 'Invalid request';
});


sinon.stub(spo, 'getRequestDigest').resolves({
FormDigestValue: 'ABC',
FormDigestTimeoutSeconds: 1800,
FormDigestExpiresAt: new Date(),
WebFullUrl: 'https://contoso.sharepoint.com'
});

sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_vti_bin/client.svc/ProcessQuery`) {
if (opts.data === `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><Query Id="1" ObjectPathId="5"><Query SelectAllProperties="false"><Properties><Property Name="ServerRelativeUrl" ScalarProperty="true" /></Properties></Query></Query></Actions><ObjectPaths><Property Id="5" ParentId="3" Name="Web" /><StaticProperty Id="3" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /></ObjectPaths></Request>`) {
return JSON.stringify([
{
"SchemaVersion": "15.0.0.0",
"LibraryVersion": "16.0.7618.1204",
"ErrorInfo": null,
"TraceCorrelationId": "3e3e629e-30cc-5000-9f31-cf83b8e70021"
},
{
"_ObjectType_": "SP.Web",
"_ObjectIdentity_": "d704ae73-d5ed-459e-80b0-b8103c5fb6e0|8f2be65d-f195-4699-b0de-24aca3384ba9:site:0ead8b78-89e5-427f-b1bc-6e5a77ac191c:web:4c076c07-e3f1-49a8-ad01-dbb70b263cd7",
"ServerRelativeUrl": "\\u002fsites\\u002fprojectx"
}
]);
}

if (opts.data === `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">\n <Actions>\n \n <Method Name="ParseAndSetFieldValue" Id="1" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">ContentType</Parameter>\n <Parameter Type="String">Item</Parameter>\n </Parameters>\n </Method>\n <Method Name="SystemUpdate" Id="2" ObjectPathId="147" />\n </Actions>\n <ObjectPaths>\n <Identity Id="147" Name="d704ae73-d5ed-459e-80b0-b8103c5fb6e0|8f2be65d-f195-4699-b0de-24aca3384ba9:site:0ead8b78-89e5-427f-b1bc-6e5a77ac191c:web:4c076c07-e3f1-49a8-ad01-dbb70b263cd7:list:f64041f2-9818-4b67-92ff-3bc5dbbef27e:item:1,1" />\n </ObjectPaths>\n </Request>`) {
return ']SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.7618.1204","ErrorInfo":null,"TraceCorrelationId":"3e3e629e-f0e9-5000-9f31-c6758b453a4a"';
}
}

throw 'Invalid request';
});

const actual = await spo.systemUpdateListItem(requestUrl, '1', logger, true, undefined, 'Item');
assert.strictEqual(actual, listItemResponse);
});

it(`sets the list item without system update`, async () => {
const listItemResponse = {
Attachments: false,
Expand Down Expand Up @@ -2297,7 +2362,7 @@ describe('utils/spo', () => {
]);
}

if (opts.data === `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">\n <Actions>\n \n <Method Name="SystemUpdate" Id="2" ObjectPathId="147" />\n </Actions>\n <ObjectPaths>\n <Identity Id="147" Name="d704ae73-d5ed-459e-80b0-b8103c5fb6e0|8f2be65d-f195-4699-b0de-24aca3384ba9:site:0ead8b78-89e5-427f-b1bc-6e5a77ac191c:web:4c076c07-e3f1-49a8-ad01-dbb70b263cd7:list:f64041f2-9818-4b67-92ff-3bc5dbbef27e:item:1,1" />\n </ObjectPaths>\n </Request>`) {
if (opts.data === `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">\n <Actions>\n \n <Method Name="ParseAndSetFieldValue" Id="1" ObjectPathId="147">\n <Parameters>\n <Parameter Type="String">Title</Parameter>\n <Parameter Type="String">NewTitle</Parameter>\n </Parameters>\n </Method>\n <Method Name="SystemUpdate" Id="2" ObjectPathId="147" />\n </Actions>\n <ObjectPaths>\n <Identity Id="147" Name="d704ae73-d5ed-459e-80b0-b8103c5fb6e0|8f2be65d-f195-4699-b0de-24aca3384ba9:site:0ead8b78-89e5-427f-b1bc-6e5a77ac191c:web:4c076c07-e3f1-49a8-ad01-dbb70b263cd7:list:f64041f2-9818-4b67-92ff-3bc5dbbef27e:item:1,1" />\n </ObjectPaths>\n </Request>`) {
return 'ErrorMessage": "systemUpdate error"}';
}
}
Expand All @@ -2306,14 +2371,42 @@ describe('utils/spo', () => {
});

try {
await spo.systemUpdateListItem(requestUrl, '1', logger, true);
await spo.systemUpdateListItem(requestUrl, '1', logger, true, { Title: 'NewTitle' });
assert.fail('No error message thrown.');
}
catch (ex) {
assert.deepStrictEqual(ex, `Error occurred in systemUpdate operation - ErrorMessage": "systemUpdate error"}`);
}
});

it(`handles no contenttype or properties error when updating list item`, async () => {
const listUrl = '/sites/sales/lists/TestList';
const requestUrl = `https://contoso.sharepoint.com/sites/sales/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')`;

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/sites/sales/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')?$select=Id`) {
return { Id: 'f64041f2-9818-4b67-92ff-3bc5dbbef27e' };
}

throw 'Invalid request';
});

sinon.stub(spo, 'getRequestDigest').resolves({
FormDigestValue: 'ABC',
FormDigestTimeoutSeconds: 1800,
FormDigestExpiresAt: new Date(),
WebFullUrl: 'https://contoso.sharepoint.com'
});

try {
await spo.systemUpdateListItem(requestUrl, '1', logger, true);
assert.fail('No error message thrown.');
}
catch (ex) {
assert.deepStrictEqual(ex, `Either properties or contentTypeName must be provided for systemUpdateListItem.`);
}
});

it(`handles error when a specific field fails when updating listitem`, async () => {
const listUrl = '/sites/sales/lists/TestList';
const requestUrl = `https://contoso.sharepoint.com/sites/sales/_api/web/GetList('${formatting.encodeQueryParameter(listUrl)}')`;
Expand Down
Loading

0 comments on commit 3adebfd

Please sign in to comment.