sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

POST request in an ODataBatch request is not wrapped in a changeset

Open vman opened this issue 10 years ago • 5 comments

All GET requests specified in the batch request work fine, but as soon as I add a POST request to my batch, it stops working as the POST request is not wrapped in a changeset.

http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part1-protocol/odata-v4.0-errata03-os-part1-protocol-complete.html#_Toc453752313

Here is my code for the OData batch request:

private _makeODataBatchRequest(): void {
    const serviceScope: ServiceScope = ServiceScope.startNewRoot();
    serviceScope.finish();

    const webAbsoluteUrl: string = this.context.pageContext.web.absoluteUrl;
    const batchOpts: IODataBatchOptions = { webUrl: webAbsoluteUrl };

    const odataBatch: ODataBatch = new ODataBatch(serviceScope, batchOpts);

    const getMyProps: Promise<Response> = odataBatch.get(`${webAbsoluteUrl}/_api/SP.UserProfiles.PeopleManager/GetMyProperties`);
    const getWebTitle: Promise<Response> = odataBatch.get(`${webAbsoluteUrl}/_api/web/title`);

    const postBody: Object = { __metadata: { type: 'SP.List' }, Title: "Developer workbench", BaseTemplate: 100 };
    const reqHeaders: Headers = new Headers();
    reqHeaders.append('odata-version', '');

    const createList: Promise<Response> = odataBatch.post(`${webAbsoluteUrl}/_api/web/lists`,
    {
      body: JSON.stringify(postBody),
      headers: reqHeaders
    });

    odataBatch.execute().then(() => {

      getMyProps.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });

      getWebTitle.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });

      createList.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });
    });
  }

The request body generated by this is as follows:

--batch_19ed0228-2598-45ce-8eb3-483972e81f0c
Content-type: application/http
Content-Transfer-Encoding: binary

GET https://vrdmn.sharepoint.com/sites/dev/_api/SP.UserProfiles.PeopleManager/GetMyProperties HTTP/1.1
accept: application/json;odata.metadata=minimal



--batch_19ed0228-2598-45ce-8eb3-483972e81f0c
Content-type: application/http
Content-Transfer-Encoding: binary

GET https://vrdmn.sharepoint.com/sites/dev/_api/web/title HTTP/1.1
accept: application/json;odata.metadata=minimal



--batch_19ed0228-2598-45ce-8eb3-483972e81f0c
Content-type: application/http
Content-Transfer-Encoding: binary

POST https://vrdmn.sharepoint.com/sites/dev/_api/web/lists HTTP/1.1
odata-version: 
content-type: application/json;charset=utf-8
accept: application/json

{"__metadata":{"type":"SP.List"},"Title":"Developer workbench","BaseTemplate":100}

--batch_19ed0228-2598-45ce-8eb3-483972e81f0c--

As you can see the POST request is not wrapped in a changeset and as a result, the request fails.

vman avatar Aug 29 '16 15:08 vman

Tracked as VSO #242876

manishgarg1 avatar Aug 30 '16 17:08 manishgarg1

Interesting. We originally were introducing changesets, but when we upgraded to OData Version 4.0 they seemed to no longer be necessary. I will investigate this bug.

octogonz avatar Sep 20 '16 22:09 octogonz

Hi @pgonzal I had another look at this and tweaked my code a little bit and it seems its working now! I am also using Drop 3 (not sure if that makes any difference):

private _makeODataBatchRequest(): void {
    const serviceScope: ServiceScope = this.context.serviceScope;

    const webAbsoluteUrl: string = this.context.pageContext.web.absoluteUrl;
    const batchOpts: IODataBatchOptions = { webUrl: webAbsoluteUrl };

    const odataBatch: ODataBatch = this.context.httpClient.beginBatch(batchOpts);

    const getMyProps: Promise<Response> = odataBatch.get(`${webAbsoluteUrl}/_api/SP.UserProfiles.PeopleManager/GetMyProperties`);
    const getWebTitle: Promise<Response> = odataBatch.get(`${webAbsoluteUrl}/_api/web/title`);

    const postBody: Object = { Title: "Developer workbench", BaseTemplate: 100 };

    const createList: Promise<Response> = odataBatch.post(`${webAbsoluteUrl}/_api/web/lists`,
    {
      body: JSON.stringify(postBody)
    });

    odataBatch.execute().then(() => {

      getMyProps.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });

      getWebTitle.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });

      createList.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });
    });
  }

vman avatar Sep 21 '16 11:09 vman

closing some old bugs.

patmill avatar Feb 25 '17 01:02 patmill

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 30 '20 12:01 msft-github-bot