I am trying to use the C# Example on the "Send a sharing invitation" page in the Graph API documentation. However, it fails with a 400 Error. According to the "Microsoft Graph error responses and resource types" page, this signifies "Can't process the request because it's malformed or incorrect." Unfortunately this is too vague for me to work out what I should be changing.
In case it helps with diagnosing this issue, I see the below warning in Visual Studio when I add this snippet:
[] (local variable) GraphServiceClient graphServiceClient CS0618: 'InviteRequestBuilder.PostAsync(Invite PostRequestBody, Action<InviteRequestBuilder.InviteRequestBuilder PostRequestConfiguration>?, Cancellation Token)' is obsolete: 'This method is obsolete. Use PostAsInvitePostResponse instead.'
Again, this is too vague for me to figure what I am supposed to change.
The code I have used is below. Additionally, I have raised this as an issue with Microsoft, by creating an issue on the Graph API Github repository. However, I have had bad experiences with Microsoft never responding to issues, so I am also raising this here. If anyone has any advice I would greatly appreciate it.
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Drives.Item.Items.Item.Invite.InvitePostRequestBody
{
Recipients = new List<DriveRecipient>
{
new DriveRecipient
{
Email = "ryan@contoso.com",
},
},
Message = "Here's the file that we're collaborating on.",
RequireSignIn = true,
SendInvitation = true,
Roles = new List<string>
{
"write",
},
Password = "password123",
ExpirationDateTime = "2018-07-15T14:00:00.000Z",
};
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Invite.PostAsync(requestBody);
I just realised why this didn't work. In the sample, they do not have a $ symbol before each the opening quotation mark in the final line of code. Consequently, they are not using the value of the drive-id
and driveItem-id
variables, but instead their literal names.
Therefore, I have now edited the last line as shown below, by adding the $ symbols. I realise I am answering my own question, but I hope this will help anyone else who encounters this issue.
var result = await graphClient.Drives[$"{drive-id}"].Items[$"{driveItem-id}"].Invite.PostAsInvitePostResponseAsync(requestBody);