Hello,
Update: Forget one Detail:
Initially the error was:
System.IO.FileNotFoundException: Could not load file or assembly 'Azure.Core, Version=1.38.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8'
I tried downgrading the package, but at some point after 7 downgrades of different packages, the error is that Microsoft.Kiota.Abstractions, Version=1.1.2.0 is missing and I can not downgrade to it, because of dependencies.
So I added the Azure.Core dll (1.38 & 1.37) with gacutil and then the error from above appears that the method is not found:
I have a problem with Graph.API / SharePoint On Prem and I am stuck.
I am trying to access Graph API in a SharePoint 2019 Farm Solution.
My code and setup is working fine in a console application on the same client / server, but inside SharePoint Farm Solution I get this error:
System.MissingMethodException: Method not found: 'Void Microsoft.Graph.GraphServiceClient..ctor(Azure.Core.TokenCredential, System.Collections.Generic.IEnumerable`1<System.String>, System.String)'.
System:
- SharePoint Server Subscription Edition
- Visual Studio 2022
- Both Console and Farm Solution running .NET Framework 4.8
- Azure.Identity (1.11.2.0)
- Microsoft.Graph (5.50.0.0)
I try to access the Graph Api with a tenantId, clientId and clientSecret and like I said it works find in the console.
How to reproduce the error:
- Create a SharePoint Farm Solution (.NET Framework 4.8, not Sandbox)
- With NuGet install (latest version as of 24.04.2024)
- Azure.Identity (1.11.2.0)
- Microsoft.Graph (5.50.0.0)
- Create Application Page or Webpart (I created an Application Page, but the same error comes in a webpart - Visual Web Part (Farm Solution Only)
- Add a simple Button and a click event, i.e.: <asp:Button ID="btnSendEmail" Text="Send Email" OnClick="btnSendEmail_Click" runat="server" />
- Add the class from below and add this following function. (you can also try your own Graph Api call, this is just my example): GraphEmail.SendEmail("name@yourdomain.com", "name@yourdomain.com", "Test", "Test");
- Add all necessary dll to the Solution package (I started with the only required and at the end added all which are also copied locally in the console application)
- Deploy the solution
- Test the button in the Application Page or Webpart
I am guessing there is some conflict with SharePoint DLLs or config? I can't explain it and also can not find anything on the internet.
Thanks!
GraphEmail Class for point 5 above, this is with a wait, but the same error occurs async. I also have a Event class where i create an calendar event with the same error.
internal static class GraphEmail
{
internal static void SendEmail(string from, string to, string subject, string body)
{
Message msg = new Message()
{
Subject = subject,
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = body
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = to
}
}
}
};
SendMailPostRequestBody postBody = new SendMailPostRequestBody()
{
Message = msg,
SaveToSentItems = true
};
ClientSecretCredential credential = new ClientSecretCredential("tenantid", "clientId", "clientSecret");
GraphServiceClient graphClient = new GraphServiceClient(credential);
graphClient.Users[from]
.SendMail
.PostAsync(postBody)
.Wait();
}
}