3

I have difficulties to find code samples of POST calls, using the MSGraphClient.

The only one I find is using the already deprecated graphHTTPClient ( https://docs.microsoft.com/en-us/sharepoint/dev/spfx/call-microsoft-graph-using-graphhttpclient ):

I wonder what would be the proper way to create an Office 365 Group using the modern MSGraphClient syntax; how do I pass the JSON Request body.

2 Answers 2

5

MSGraphClient is still in preview and will not work in production tenants as of writing this. Have tested the below the code and it works in Developer tenant.

Add the below import statement:

import { MSGraphClient } from "@microsoft/sp-client-preview";
import { Group } from "@microsoft/microsoft-graph-types";

To create a group, make a POST request to the graph groups endpoint as below:

const graphClient : MSGraphClient = this.context.serviceScope.consume(MSGraphClient.serviceKey)

const group : Group = {
        displayName: "Sample test group",
        description: "Group description",
        groupTypes: [
            "Unified"
        ],
        mailEnabled: true,
        mailNickname: "TestGroup",
        securityEnabled: true
    };


graphClient
.api("https://graph.microsoft.com/v1.0/groups/")
.post(group)
.then((groupResponse) => {
    console.log(groupResponse);        
});

Have added the below npm package:

npm install @microsoft/microsoft-graph-types --save-dev

Reference - Use MS Graph

Github sample - API Scopes

2
  • I used the same approach but it is giving error as Object literal may only specify known properties, and '"own[email protected]"' does not exist in type 'Group'. when I'm trying to add owner of group explicitly. const groups : Group = { "description": ProName + " Office 365 Group", "displayName": ProName + "_Team", "groupTypes": [ "Unified" ], "mailEnabled": false, "mailNickname": ProName + "_Team", "securityEnabled": false, "[email protected]": HBCAdminCol }; How I can add owner to it using MSGraphClient as it was working finely with graphHttpClient.
    – Ashwini
    Oct 24, 2019 at 11:44
  • Instead of [email protected], can you try with owners: "[email protected]" or owners: ["[email protected]"] and check ? Oct 24, 2019 at 13:54
2

This should help you get started. Here I'm registering a webhook

const graphClient = this.context.serviceScope.consume(MSGraphClient.serviceKey)
const future: Date = new Date(Date.now() + 4320 * 60);
const body: object = {
    changeType: "created",
    notificationUrl: "mywebhookurl",
    resource: "me/messages",
    expirationDateTime: future.toISOString(),
    clientState: "SecretClientState"
  };
await graphClient.api(`subscriptions`).post(body);
1
  • you're welcome! upvote if you like the answers :)
    – baywet
    Apr 18, 2018 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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