Jeff Beck
Jeff Beck

Reputation: 558

Sending Messages through Microsoft Graph API with SchemaExtension Data

I'm looking for some help formatting schema extension data in Microsoft's Graph API. I've been able to successfully send Office 365 messages in code and through the Graph Explorer using this body:

{
  "message": {
    "subject": "Test Subject",
    "body": {
      "contentType": "Text",
      "content": "Test Body "
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ]
  }
}

I created a schema extension and promoted it to "Available" status. I can query the extension to verify it's available and get this response body:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#schemaExtensions",
    "value": [
        {
            "id": "extc5bnq6uk_TestExtension",
            "description": "Test Extension",
            "targetTypes": [
                "Message"
            ],
            "status": "Available",
            "owner": "mysecretclienttenantgoeshere",
            "properties": [
                {
                    "name": "ValueOne",
                    "type": "String"
                },
                {
                    "name": "ValueTwo",
                    "type": "String"
                }
            ]
        }
    ]
}

So far I haven't been able to append extension data to a new message. I've tried formatting my request body like this:

{
  "message": {
    "subject": "Test Subject",
    "body": {
      "contentType": "Text",
      "content": "Test Body "
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ],
    "extc5bnq6uk_TestExtension": {
      "ValueOne": "TestValue",
      "ValueTwo": "TestValue"
    }
  }
}

and like this:

{
  "message": {
    "subject": "Test Subject",
    "body": {
      "contentType": "Text",
      "content": "Test Body "
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ],
    "extensions":[
    {
        "extc5bnq6uk_TestExtension" : {
            "ValueOne" : "TestValue"
            "ValueTwo" : "TestValue"
        }
    }
    ]
  }
}

Both formats return a 400 code with response body:

{
    "error": {
        "code": "RequestBodyRead",
        "message": "The property 'extc5bnq6uk_TestExtension' does not exist on type 'Microsoft.OutlookServices.Message'. Make sure to only use property names that are defined by the type or mark the type as open type.",
        "innerError": {
            "request-id": "21792fd0-44d1-42aa-8d51-f8abc92cbd04",
            "date": "2018-08-14T16:39:31"
        }
    }
}

I'm posting to this URL in the graph explorer:

https://graph.microsoft.com/v1.0/me/sendMail

and to the "messages" and "sendMail" endpoints in code.

Upvotes: 0

Views: 1627

Answers (2)

Keen Jin
Keen Jin

Reputation: 1138

Jeff

Based on your question you posted, you have created a schemaExtension successfully. I think you want to send an email with this schemaExtension, but when you send an email with this schemaExtension, we get the 400 code in the response.

Based on my test, I think we can use the request body as blow.

1.Create a schemaExtension like this:

{
   "@odata.context":"https://graph.microsoft.com/v1.0/$metadata#schemaExtensions/$entity",
   "id":"{extensionId}",
   "description":"sample description",
   "targetTypes":[
       "Message"
    ],
   "status":"Available",
   "owner":"{owner id}",
   "properties":[
      {
          "name":"p1",
          "type":"String"
      },
      {
          "name":"p2",
          "type":"String"
      }
    ]
  }
  1. Create a message

POST https://graph.microsoft.com/v1.0/me/messages

{
   "message":{
      "subject":"Meet for lunch?",
      "body":{
          "contentType":"Text",
          "content":"The new cafeteria is open."
       },
      "toRecipients":[
          {
              "emailAddress":{
                  "address":"{toRecipients email address}"
              }
          }
      ],
      "extensions":[
          {
              "@odata.type":"Microsoft.Graph.OpenTypeExtension",
              "extensionName":"{extensionName}",
              "p1":"Wingtip Toys",
              "p2":"10000"
          }
      ]
  },
  "saveToSentItems":"false"
}
  1. when we send this message with the request, we will get the 202 code. The {toRecipients email address} will receive the email.

Upvotes: 0

Jeff Beck
Jeff Beck

Reputation: 558

I found the answer in the Known Limitations of the documentation. Certain resource types, including messages, have to be done in two stages, an initial post and then a follow up patch.

Creating the message and then patching with this JSON returned a valid response.

{
    "extc5bnq6uk_TestExtension": {
        "ValueOne": "Test Value One",
        "ValueTwo": "Test Value Two"
    }
}

Unfortunately, another limitation for schema extensions on messages is that they can't be used to filter messages, which is what I was ultimately after.

Filtering on schema extension properties (using the $filter expresssion) is not supported for Outlook entity types - contact, event, message, or post.

Upvotes: 1

Related Questions