2

I'm working on a web part for SharePoint 2016 on-prem and I want to send an email from the current user. I cannot use @pnp/sp library as it's not supported with TypeScript 2.2.2 and I'm unable to upgrade it. I'm using SPHttpClient post method, but it gives me error 400:

The property '__metadata' does not exist on type 'SP.Utilities.EmailProperties'. Make sure to only use property names that are defined by the type

const siteUrl = this.context.pageContext.web.absoluteUrl;
const emailUrl = siteUrl + '/_api/SP.Utilities.Utility.SendEmail';

const reqOptions: ISPHttpClientOptions  = {
  headers: {
    "Accept": "application/json;odata=verbose",
    "Content-Type": "application/json;odata=verbose"
  },
  body: JSON.stringify({
    'properties': {
      '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
      'To': { 'results': ['email@email.com'] },
      'Body': 'Ignore this test email.',
      'Subject': 'Invoice Uploader Test Email'
    }
  })
};

this.context.spHttpClient.post(emailUrl, SPHttpClient.configurations.v1, reqOptions)
  .then((response: SPHttpClientResponse) => {
    console.log(`Status code: ${response.status}`);
    console.log(`Status text: ${response.statusText}`);

    response.json().then((responseJSON: JSON) => {
      console.log(responseJSON);
    });
  })
  .catch((err) => {
    console.log(err);
  });

What is the corect syntax of such request?

4

You need to add an additional header, odata-version and set it to 3.

This error happens because SPFx's built-in SPHttpClient uses the odata-version header whose value is set to 4.

So, simply change it as below and check:

const reqOptions: ISPHttpClientOptions  = {
      headers: {
        "Accept": "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "odata-version":"3.0",
      }, 
      // rest of your code
}

Referenced from:

  1. Suppressing SharePoint REST metadata using odata=nometadata fails

  2. Consider making HttpClient features opt-in

  • 1
    Brilliant, thank you. It's incredible how specific knowledge you need to complete a simple action like that. – Jarek Danielak Feb 8 at 12:29
  • 1
    Do checkout this blog where it is explained in more detail :) Quite helpful, because a lot of requests might get seemingly stuck because of this minor thing ! – Gautam Sheth Feb 8 at 12:32

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.