Graph API in SharePoint Farm Solution - System.MissingMethodException: Method not found: Void Microsoft.Graph.GraphServiceClient

Pronizius 46 Reputation points
2024-04-25T12:11:54.6766667+00:00

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:

  1. Create a SharePoint Farm Solution (.NET Framework 4.8, not Sandbox)
  2. With NuGet install (latest version as of 24.04.2024)
    1. Azure.Identity (1.11.2.0)
    2. Microsoft.Graph (5.50.0.0)
  3. Create Application Page or Webpart (I created an Application Page, but the same error comes in a webpart - Visual Web Part (Farm Solution Only)
  4. Add a simple Button and a click event, i.e.: <asp:Button ID="btnSendEmail" Text="Send Email" OnClick="btnSendEmail_Click" runat="server" />
  5. 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");
  6. 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)
  7. Deploy the solution
  8. 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();

    }

}

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,285 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,252 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,150 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,783 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pronizius 46 Reputation points
    2024-06-06T12:29:53.22+00:00

    If anyone has the same issue, we opened a ticket with Microsoft, after a few weeks of testing, we were told, it is not supported.

    So it is not possible to use the Graph API SDK in a SharePoint Farm Solution.

    We decided to do a workaround and program an own custom web api with the Graph API SDK and call the custom web api with simple Http posts.

    For me it was too risky to call the Graph API directly with Http Posts, because we would need to rebuild all request and response classes or risk to use the SDK Classes, which maybe again cause issues. Also such way it is easier to update and reuse.

    Of course performance wise, debugging etc. its not the best solution.

    0 comments No comments