0

I've been trying to get this function to post an item to a Sharepoint list, however it's been coming back as a 403 each time. The formDigest value populates each time I console log it and I can see it in the network request headers, however not sure what else would be causing the issue. I'm hoping it's something really simple I'm just not seeing. Any help would be appreciated.

Thanks!

public submitReport() {

let formDigest = "";
let reportedby: string = this.state.userEmail;
let page: string = this.state.location;
let note: string = this.state.feedback;
axios.post('https://<SITE URL>/_api/contextinfo')
.then((res) => {
  formDigest = res.data.FormDigestValue;
  console.log('formdigest: ' + formDigest);
})
.then(() => {
  axios({
    method: 'POST',
    url: "https://<SITE URL>/sites/intranet/help/_api/web/lists/GetByTitle('<LIST TITLE>')/items",
    headers: {
      "X-RequestDigest": formDigest,
      "Accept": "application/json;odata=verbose",
      "content-type": "application/json;odata=verbose",
    },
    data: {
      '__metadata': {
        'type': 'SP.Data.User_x0020_FeedbackListItem'
      },
      'Title': new Date(),
      'ReportedBy': reportedby,
      'Page': page,
      'Note': note
    }
  })
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  })
})

}

  • Do you have add item permission on List? – Bharat Khunti Apr 4 '18 at 5:57
  • how about using "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value, ? – Gautam Sheth Apr 4 '18 at 6:02
  • Why are you not using the the spfx-way with: import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http'; – Oskar Apr 4 '18 at 8:19
  • Are you fetching the formdigest from the same site you are trying to post to: site/intranet/help/_api/contextinfo ? – Anders Aune Apr 4 '18 at 9:32
  • @AndersAune Yes that was definitely the problem. I've never worked with the x-formdigest header before and I thought the one from the root of the domain was all I needed. I knew it was something obvious like this thank you! Totally working now. – Heythisispaul Apr 4 '18 at 15:52
0

I would try to use the spfx-functionality with

import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@microsoft/sp-http';

You don't need any digit - the framework does this for you (you also don't have the problem of an expired x-requestdigit). You could do it in the following way:

var itemType = "SP.Data.User_x0020_FeedbackListItem";
var absoluteUri = this.props.context.pageContext.web.absoluteUrl;

const itemData: any = {
    "__metadata": { "type": itemType },
    'Title': new Date(),
    'ReportedBy': reportedby,
    'Page': page,
    'Note': note
};
const itemHeader: any = {
    'Accept': 'application/json;odata=nometadata',
    'Content-type': 'application/json;odata=verbose',
    'odata-version': '',
    "X-HTTP-Method": "POST"
};
const spHttpClientOptions: ISPHttpClientOptions = {
    "headers": itemHeader,
    "body": JSON.stringify(itemData)
};

this.props.context.spHttpClient.post(
    absoluteUri + "/_api/web/lists/GetByTitle('<LIST TITLE>')/items",
    SPHttpClient.configurations.v1,
    spHttpClientOptions);

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.