![]() |
VOOZH | about |
dotnet add package TimHanewich.MicrosoftGraphHelper --version 1.1.0
NuGet\Install-Package TimHanewich.MicrosoftGraphHelper -Version 1.1.0
<PackageReference Include="TimHanewich.MicrosoftGraphHelper" Version="1.1.0" />
<PackageVersion Include="TimHanewich.MicrosoftGraphHelper" Version="1.1.0" />Directory.Packages.props
<PackageReference Include="TimHanewich.MicrosoftGraphHelper" />Project file
paket add TimHanewich.MicrosoftGraphHelper --version 1.1.0
#r "nuget: TimHanewich.MicrosoftGraphHelper, 1.1.0"
#:package TimHanewich.MicrosoftGraphHelper@1.1.0
#addin nuget:?package=TimHanewich.MicrosoftGraphHelper&version=1.1.0Install as a Cake Addin
#tool nuget:?package=TimHanewich.MicrosoftGraphHelper&version=1.1.0Install as a Cake Tool
This is a .NET class library designed to assist with the Microsoft Graph Authentication process as well as with transacting with several graph modules. This library is available on NuGet as TimHanewich.MicrosoftGraphHelper.
This library was built around the Microsoft documentation specified here.
To install the package from NuGet in your .NET project, run the following:
dotnet add package TimHanewich.MicrosoftGraphHelper
The following example demonstrates using this library to authenticate with the Microsoft Graph API.
Note: The Tenant property, per Microsoft documentation, can be "common" for both Microsoft accounts and work/school accounts, "organizations" for work/school accounts only, "consumers" for Microsoft accounts only, of a tenant identifier (GUID).
MicrosoftGraphHelper mgh = new MicrosoftGraphHelper();
mgh.Tenant = "consumers";
mgh.ClientId = Guid.Parse("d9571adf-0c99-4285-bd6c-85d1ad9df015"); //ID of the app registration in Azure Entra ID
mgh.RedirectUrl = "https://www.google.com/"; //registered redirect URL of the app registration in Azure Entra ID
mgh.Scope.Add("User.Read"); //add any scopes you want
mgh.Scope.Add("Calendars.ReadWrite");
mgh.Scope.Add("Mail.Read");
//authorization happens via the web browser. Redirect the user to visit the url and provide consent.
//they will redirected to the redirect URL (must be a registered redirect URL in the application in Azure AD) with a "code" parameter.
string url = mgh.AssembleAuthorizationUrl();
Console.WriteLine("Please go to the following URL and sign in. After you sign in, give me the "code" parameter out of the URL it redirects you to");
Console.WriteLine(url);
Console.Write("Give me the code: ");
string code = Console.ReadLine();
mgh.GetAccessTokenAsync(code).Wait();
Normally the bearer token you will be given will expire within 60 minutes. This means your access will also stop. However, if the offline_access scope was added to the original authorization flow (it is by default in the MicrosoftGraphHelper class), you can refresh your token by using the refresh token that was originally provided in the authorization flow.
For example:
// The token payload was saved to JSON previously. Here, we are retrieving it and adding it back
MicrosoftGraphTokenPayload tokens = JsonConvert.DeserializeObject<MicrosoftGraphTokenPayload>(System.IO.File.ReadAllText(@"C:\Users\timh\Downloads\tah\TimHanewich.MicrosoftGraphHelper\payload.json"));
MicrosoftGraphHelper mgh = new MicrosoftGraphHelper();
mgh.LastReceivedTokenPayload = tokens;
//Refresh if the retrieved token is expired
if (mgh.AccessTokenHasExpired())
{
Console.Write("Tokens are expired! Refreshing... ");
await mgh.RefreshAccessTokenAsync();
Console.WriteLine("Refreshed!");
}
else
{
Console.WriteLine("Tokens are still active! No need to refresh.");
}
The following demonstrates how you can schedule a new event in your user's default outlook calendar. It requires the Calendars.ReadWrite scope.
//Create Outlook Event
OutlookEvent ev = new OutlookEvent();
ev.Subject = "Let's do something";
ev.Body = "Go shopping maybe?";
ev.StartUTC = new DateTime(2025, 03, 02, 12, 0, 0);
ev.EndUTC = ev.StartUTC.AddMinutes(15);
//Schedule
Console.Write("Scheduling... ");
await mgh.CreateOutlookEventAsync(ev);
Console.WriteLine("done!");
The following requires the Mail.Send scope.
//Construct email
OutlookEmailMessage email = new OutlookEmailMessage();
email.ToRecipients.Add("timhanewich@gmail.com");
email.Subject = "My favorite songs";
email.Content = "1. Chris Brown - Yeah 3X\n2. Chris Brown - Forever\n3. Chris Brown - Turn Up the Music";
email.ContentType = OutlookEmailMessageContentType.Text;
//Send email
Console.Write("Sending email... ");
await mgh.SendOutlookEmailMessageAsync(email);
Console.WriteLine("Sent!");
//Get the sites that are available
SharepointSite[] sites = mgh.SearchSharepointSitesAsync("").Result;
Console.WriteLine(JArray.Parse(JsonConvert.SerializeObject(sites)).ToString());
//Get the lists in that site
SharepointList[] lists = mgh.ListSharepointListsAsync(Guid.Parse("2e069086-c6f2-4735-a728-eb33b8347842")).Result;
Console.WriteLine(JArray.Parse(JsonConvert.SerializeObject(lists)).ToString());
//Get the content of a list
SharepointListItem[] items = mgh.GetAllItemsFromSharepointListAsync(Guid.Parse("2e069086-c6f2-4735-a728-eb33b8347842"), Guid.Parse("771b32f1-859c-4570-8bf2-7c86d140dc5c")).Result;
Console.WriteLine(JArray.Parse(JsonConvert.SerializeObject(items)).ToString());
//Creating a new item (record) in a list
JObject jo = new JObject();
jo.Add("Title", "Harry the Hippo");
mgh.CreateItemAsync(Guid.Parse("2e069086-c6f2-4735-a728-eb33b8347842"), Guid.Parse("771b32f1-859c-4570-8bf2-7c86d140dc5c"), jo).Wait();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 1 NuGet packages that depend on TimHanewich.MicrosoftGraphHelper:
| Package | Downloads |
|---|---|
|
Aletheia
Package Description |
This package is not used by any popular GitHub repositories.