I want to copy a mail from one folder to another.
Referring to the documentation, it should work like this:
var graphClient = new GraphServiceClient(authProvider);
var destinationId = "destinationId-value";
await graphClient.Me.Messages["{message-id}"]
.Copy(destinationId)
.Request()
.PostAsync();
However, when I try to use .Copy() like described I get the error, that I can't use it like a method. If I try to add the information as an [], like it's done for the users or messages property, I receive a different error.
I have tried it like this:
var graphClient = GetGraphClientInstance();
var destinationFolderId = "destinationFolderId-value";
await graphClient.Users["myUserName"].Messages[specificMail.Id]
.Copy(destinationFolderId )
.Request()
.PostAsync();
I'm using Microsoft.Graph version 5.0.0-preview-12.
Does anyone have an idea how to use the Copy property correctly?
Using Microsoft.Graph v5 your original code:
var graphClient = new GraphServiceClient(authProvider);
var destinationId = "destinationId-value";
await graphClient.Me.Messages["{message-id}"]
.Copy(destinationId)
.Request()
.PostAsync();
should be changed to:
var graphClient = new GraphServiceClient(authProvider);
var destinationId = "destinationId-value";
await graphClient.Users["myUserName"].Messages[specificMail.Id]
.MicrosoftGraphCopy
.PostAsync(new CopyPostRequestBody { DestinationId = destinationId });
Related
While using newest version of Microsoft.Graph and Microsoft.Graph.Core libraries I am trying to execute following snippet to find user and then update him. User is found and retrieved but after executing Update method, program crashes. The application is .Net Framework 4.6.2. Not possible to upgrade due to other dependencies.
var builder = ConfidentialClientApplicationBuilder.CreateWithApplicationOptions(new ConfidentialClientApplicationOptions()
{
ClientId = "appId",
ClientSecret = "clientSecret",
TenantId = "tenantId"
});
var client = new GraphServiceClient(
new MicrosoftGraphAuthProvider(
builder.Build(),
new string[] {"https://graph.microsoft.com/.default" }));
var user = new Microsoft.Graph.User()
{
GivenName = "test",
Surname = "test",
Mail = "test#testmail.com"
};
var originalUser = await client.Users[upn]
.Request()
.Select("displayName")
.GetAsync();
await client.Users[originalUser.Id]
.Request()
.UpdateAsync(user);
Exception message
{"error":{"code":"Request_BadRequest","message":"Specified HTTP method is not allowed for the request target.","innerError":{"date":"2022-11-04T18:13:06","request-id":"","client-request-id":""}}}
First thoughts are to look at HTTP method used. But this is official distribution from Microsoft running against very slightly customized AzureAd tenant, so I would expect to work just like that. I am starting to be clueless. Gonna be helpful for ideas.
If you want to update user , you have to use
var result = await graphClient.Users[userId].Request().UpdateAsync(userUpdate);
doc - https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=CS#updating-an-existing-entity-with-patch
Hope this helps ,
Thanks
How do I run the query
https://graph.microsoft.com/v1.0/users?$count=true&$search="displayName:room"&$filter=endsWith(mail,'xxxx.com')&$select=id,displayName,mail
in C#?
this is what I have now:
return await _graphServiceClient
.Users.Request()
.Header("ConsistencyLevel", "eventual")
. .Filter($"(endsWith(mail, 'xxxx.com'))&$count=true")
.Select("id,displayName,mail")
.Top(999)
.GetAsync();
Try this code pls:
var queryOptions = new List<QueryOption>()
{
new QueryOption("$count", "true"),
new QueryOption("$search", "\"displayName:tiny\"")
};
var res = await graphClient
.Users.Request(queryOptions)
.Header("ConsistencyLevel", "eventual")
.Filter("endswith(mail,'contoso.com')")
.OrderBy("userPrincipalName")
.Select("id,displayName,mail")
.Top(999)
.GetAsync();
When we follow the official code snippet, we should use .Search() but it will meet exception:
Then let's see github issue here, and we can set search parameter into query option.
I want list name of rooms created. I got an error at FindingRooms method, IUserRequestBuilder does not contain a definition for FindRooms. How to solve it?
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("****-8d12-45ce-99dc-ee97478abc48")
.WithTenantId("****-0c15-41f2-9858-b64924a83a6c").WithRedirectUri("http://localhost")
.Build();
var password = new SecureString();
password.AppendChar('<');
password.AppendChar('T');
password.AppendChar('N');
password.AppendChar('>');
password.AppendChar('7');
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
Microsoft.Graph.User me = graphClient.Me.Request()
.WithUsernamePassword("Tej#indica.onmicrosoft.com", password)
.GetAsync().Result;
var findRooms = await graphClient.Me
.FindRooms("Test#indica.onmicrosoft.com")
.Request()
.GetAsync();
Currently, the findRooms API is in Graph beta version. So, you need to add Microsoft.Graph.Beta package.
I'm trying to get the user's calendar events for today. So I added some query parameters but they're getting ignored and the graph client returns the user's events as if I didn't supply any parameters (startatetime):
var options = new QueryOption[]
{
new QueryOption("startdatetime", DateTime.UtcNow.ToString("o")),
new QueryOption("enddatetime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
.Me
.Calendar
.Events
.Request(options)
.GetAsync();
I tested it in the graph explorer and it works fine. But in the sdk, it returns calendar events that started before today.
Your code is the equivalent of calling:
`/events?startdatetime={dateTime}&enddatetime={dateTime}`.
That is a valid endpoint, but you're passing invalid query params. What you're looking for is calendarView:
`/calendarView?startdatetime={dateTime}&enddatetime={dateTime}`
Using the SDK, this would look like this:
var options = new QueryOption[]
{
new QueryOption("startDateTime", DateTime.UtcNow.ToString("o")),
new QueryOption("endDateTime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
.Me
.CalendarView
.Request(options)
.GetAsync();
I'm getting an invalid request error for the following (Message: One of the provided arguments is not acceptable):
DriveRecipient[] invitees = new DriveRecipient[1];
invitees[0] = new DriveRecipient()
{
Email = "testEmail#testdomain.com"
};
var test = await graphClient
.Me
.Drive
.Root
.ItemWithPath("/TestFolder")
.Invite(invitees, true, sendInvitation : true, message: "Test Message")
.Request()
.PostAsync();
I'm trying to share a folder (root/TestFolder) in OneDrive but am getting an invalid request error. Is it possible to share a folder this way? Or alternatively, how would I just create a shared folder, if this doesn't work?
You need to include the roles you want to apply ("read" and/or "write"):
var invitees = new List<DriveRecipient>();
invitees.Add(new DriveRecipient()
{
Email = "testEmail#testdomain.com"
});
var test = await client
.Me
.Drive
.Root
.ItemWithPath("/TestFolder")
.Invite(recipients: invitees,
requireSignIn: true,
sendInvitation: true,
message: "Test Invite",
roles: new List<string>() { "Read", "Write" })
.Request()
.PostAsync();