0

i've currently got a requirement to create 3 columns in a list. Which i'm doing using the below function. This gets called 3 times and a different field name is passed each time.The guid that gets passed to the function is the id of the list I want to create the columns in.

 private async addCol(fieldName, guid): Promise<any> {
    const digestCache: IDigestCache = this.context.serviceScope.consume(DigestCache.serviceKey);
    digestCache.fetchDigest(this.context.pageContext.web.serverRelativeUrl).then((digest: string): void => {
      let obj = {};

      if (fieldName == "Full Address") {
        obj = "{ '__metadata': { 'type': 'SP.FieldText' }, 'FieldTypeKind': 2,'Title': 'Full Address'}";
      }
      if (fieldName == "Description") {
        obj = "{ '__metadata': { 'type': 'SP.FieldText' }, 'FieldTypeKind': 2,'Title': 'Description' }";
      }
      if (fieldName == "Location") {
        obj = "{ '__metadata': { 'type': 'SP.Field' }, 'FieldTypeKind': 31, 'Title': 'Location' }"
      }
      console.log("Post data: ");
      console.log(obj);

      $.post({
        url: this.context.pageContext.site.serverRelativeUrl + "/_api/web/lists(guid'" + guid + "')/fields",
        type: "POST",
        data: obj,
        headers: {
          "accept": "application/json;odata=verbose",
          "content-type": "application/json;odata=verbose",
          "X-RequestDigest": digest,
        },
        success: function (data) {
          //console.log("done");
          //console.log(data)
          console.log("added: " + fieldName);
        },
        error: function (response) {
          console.log(response.responseText);
        }
      });
    });
  }

When the code runs, i get this error from the console:

{"error":{"code":"-2147023746, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The specified program requires a newer version of Windows. (Exception from HRESULT: 0x8007047E)"}}}

And only one column gets created. Is this something to do with Sharepoint taking its time to update the list?

  • can you check with additional header 'odata-version':'3.0' ? Modify it as headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": digest, "odata-version":"3.0" }, and check ? – Gautam Sheth Mar 21 '18 at 13:25
  • Looks like SPFx is using odata 4.0 which is causing some issue, we can try with odata 3.0 and check – Gautam Sheth Mar 21 '18 at 13:28
0

Fixed it:

 const digestCache: IDigestCache = this.context.serviceScope.consume(DigestCache.serviceKey);
digestCache.fetchDigest(this.context.pageContext.web.serverRelativeUrl).then((digest: string): void => {

  const h2 = new Headers();
  h2.append("Accept", "application/json;odata.metadata=full");
  h2.append("Content-type", "application/json;odata.metadata=full");

  const optUpdate1: ISPHttpClientOptions = {
    headers: h2,
    body: `{ "@odata.type": "#SP.Field", "Title": "Location", "FieldTypeKind": 31, "Required": false, "EnforceUniqueValues": false, "StaticName": "Location"}`
  };

  const optUpdate2: ISPHttpClientOptions = {
    headers: h2,
    body: `{ "@odata.type": "#SP.Field", "Title": "Full Address", "FieldTypeKind": 2, "Required": false, "EnforceUniqueValues": false, "StaticName": "Full Address"}`
  };

  // Options for Details column addition
  const optUpdate3: ISPHttpClientOptions = {
    headers: h2,
    body: `{ "@odata.type": "#SP.Field", "Title": "Description", "FieldTypeKind": 2, "Required": false, "EnforceUniqueValues": false, "StaticName": "Description"}`
  };

  const myWebUrl = this.context.pageContext.web.absoluteUrl;
  const spBatchCreationOptions: ISPHttpClientBatchCreationOptions = { webUrl: myWebUrl };
  const spBatch: SPHttpClientBatch = this.context.spHttpClient.beginBatch(spBatchCreationOptions);
  const addLocation: Promise<SPHttpClientResponse> = spBatch.post(`${myWebUrl}/_api/web/lists(guid'${guid}')/Fields`, SPHttpClientBatch.configurations.v1, optUpdate1);
  const addPageUrlField: Promise<SPHttpClientResponse> = spBatch.post(`${myWebUrl}/_api/web/lists(guid'${guid}')/Fields`, SPHttpClientBatch.configurations.v1, optUpdate2);
  const addDetailsField: Promise<SPHttpClientResponse> = spBatch.post(`${myWebUrl}/_api/web/lists(guid'${guid}')/Fields`, SPHttpClientBatch.configurations.v1, optUpdate3);
  let p: Promise<SPHttpClientResponse>[] = [addPageUrlField, addDetailsField, addLocation];

  console.log("Executing the batch, batches ;) ");
  spBatch.execute();

});

Changing __metadata to a @odata.type seemed to work along with batching. Enjoy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.