- Day 21 - Getting your Team organized with Planner through the Microsoft Graph
To complete this sample you need the following:
- Complete the Base Console Application Setup
- Complete the Day 20 - Device Code Auth Setup
- Visual Studio Code installed on your development machine. If you do not have Visual Studio Code, visit the previous link for download options. (Note: This tutorial was written with Visual Studio Code version 1.28.2. The steps in this guide may work with other versions, but that has not been tested.)
- .Net Core SDK. (Note This tutorial was written with .Net Core SDK 2.1.403. The steps in this guide may work with other versions, but that has not been tested.)
- C# extension for Visual Studio Code
- Either a personal Microsoft account with a mailbox on Outlook.com, or a Microsoft work or school account.
If you don't have a Microsoft account, there are a couple of options to get a free account:
- You can sign up for a new personal Microsoft account.
- You can sign up for the Office 365 Developer Program to get a free Office 365 subscription.
As this exercise requires new permissions the App Registration needs to be updated to include the Group.ReadWrite.All and User.ReadBasic.All permissions using the new Azure AD Portal App Registrations UI (in preview as of the time of publish Nov 2018).
-
Open a browser and navigate to the Azure AD Portal. Login using a personal account (aka: Microsoft Account) or Work or School Account with permissions to create app registrations.
Note: If you do not have permissions to create app registrations contact your Azure AD domain administrators.
-
Click Azure Active Directory from the left-hand navigation menu.
-
Click on the .NET Core Graph Tutorial item in the list
Note: If you used a different name while completing the Base Console Application Setup select that instead.
-
Click API permissions from the current blade content.
-
Click Add a permission from the current blade content.
-
On the Request API permissions flyout select Microsoft Graph.
-
Select Delegated permissions.
-
In the "Select permissions" search box type "<Start of permission string>".
-
Select Group.ReadWrite.All from the filtered list.
-
Click Add permissions at the bottom of flyout.
-
-
Back on the API permissions content blade, click Grant admin consent for <name of tenant>.
- Click Yes.
Note: Make sure you do not have any application permission already selected, it will make the request fail. If you do have some, remove them before granting the new permissions.
Planner relies on the Office 365 group infrastructure to function properly. Please make sure you create a group.
- On the application registration view from the last step, click on Manifest.
- Set the
allowPublicClient
property totrue
. - Click on
Save
In this step you will create a Helper method that encapsulates the logic for listing existing plans and then add calls to the console application created in the Device Code Flow.
Important: Ensure that you follow the steps from Day 20 Device Code Flow exercise or today's application updates will not leverage the proper authentication flow necessary to be successful.
-
Inside the
Program
class update the methodMain
with the following definition. This method will leverage a Helper we will create nextstatic void Main(string[] args) { // Load appsettings.json var config = LoadAppSettings(); if (null == config) { Console.WriteLine("Missing or invalid appsettings.json file. Please see README.md for configuration instructions."); return; } var GraphServiceClient = GetAuthenticatedGraphClient(config); var plannerHelper = new PlannerHelper(GraphServiceClient); plannerHelper.PlannerHelperCall().GetAwaiter().GetResult(); }
-
Inside the
Helpers
folder add a classPlannerHelper.cs
with the following definition.We will build on this class during the exercise. As it is defined, the helper will list current plans for the first group it can find, and create one if none exist.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Graph; namespace ConsoleGraphTest { public class PlannerHelper { private readonly GraphServiceClient _graphClient; public PlannerHelper(GraphServiceClient graphClient) { _graphClient = graphClient ?? throw new ArgumentNullException(nameof(graphClient)); } public async Task PlannerHelperCall() { //Getting the first group we can find to create a plan var groupId = (await _graphClient.Me.GetMemberGroups(false).Request().PostAsync()).FirstOrDefault(); if (groupId != null) { var users = await _graphClient.Users.Request(new List<QueryOption> { new QueryOption("$top", "3") }).GetAsync(); var planId = await GetAndListCurrentPlans(groupId) ?? await CreatePlannerPlan(users, groupId); } } private async Task<string> GetAndListCurrentPlans(string groupId) { //Querying plans in current group var plans = await _graphClient.Groups[groupId].Planner.Plans.Request(new List<QueryOption> { new QueryOption("$orderby", "Title asc") }).GetAsync(); if (plans.Any()) { Console.WriteLine($"Number of plans in current tenant: {plans.Count}"); Console.WriteLine(plans.Select(x => $"-- {x.Title}").Aggregate((x, y) => $"{x}\n{y}")); return plans.First().Id; } else { Console.WriteLine("No existing plan"); return null; } } private async Task<string> CreatePlannerPlan(IEnumerable<User> users, string groupId) { // Getting users to share the plan with var sharedWith = new PlannerUserIds(); users.ToList().ForEach(x => sharedWith.Add(x.Id)); // Creating a new planner plan var createdPlan = await _graphClient.Planner.Plans.Request().AddAsync( new PlannerPlan { Title = $"My new Plan {Guid.NewGuid().ToString()}", Owner = groupId, Details = new PlannerPlanDetails { SharedWith = sharedWith, CategoryDescriptions = new PlannerCategoryDescriptions { Category1 = "my first category", Category2 = "my second category" }, } } ); Console.WriteLine($"Added a new plan {createdPlan.Id}"); return createdPlan.Id; } } }
-
Save all files.
The console application is now able to list the plans in the group and create one if none exist. In order to test the console application run the following commands from the command line:
dotnet build
dotnet run
-
Inside the
PlannerHelper
class add a new methodCreatePlannerBucket
with the following definition. This method adds a new bucket to a plan.private async Task<string> CreatePlannerBucket(string groupId, string planId) { // Creating a new bucket within the plan var createdBucket = await _graphClient.Planner.Buckets.Request().AddAsync( new PlannerBucket { Name = "my first bucket", OrderHint = " !", PlanId = planId } ); Console.WriteLine($"Added new bucket {createdBucket.Name} to plan"); return createdBucket.Id; }
-
Inside the
PlannerHelper
add the following line at the end of thePlannerHelperCall
method.var bucketId = await CreatePlannerBucket(groupId, planId);
-
Save all files.
The console application is now able add new buckets to a plan. In order to test the console application run the following commands from the command line:
dotnet build
dotnet run
-
Inside the
PlannerHelper
class add a new methodCreatePlannerTask
with the following definition. This method adds a new task to a bucket.private async Task CreatePlannerTask(IEnumerable<User> users, string groupId, string planId, string bucketId) { // Preparing the assignment for the task var assignments = new PlannerAssignments(); users.ToList().ForEach(x => assignments.AddAssignee(x.Id)); // Creating a task within the bucket var createdTask = await _graphClient.Planner.Tasks.Request().AddAsync( new PlannerTask { DueDateTime = DateTimeOffset.UtcNow.AddDays(7), Title = "Do the dishes", Details = new PlannerTaskDetails { Description = "Do the dishes that are remaining in the sink" }, Assignments = assignments, PlanId = planId, BucketId = bucketId } ); Console.WriteLine($"Added new task {createdTask.Title} to bucket"); }
-
Inside the
PlannerHelper
class update thePlannerHelperCall
method to add the following line at the end.await CreatePlannerTask(users, groupId, planId, bucketId);
-
Save all files.
The console application is now able add new tasks to a bucket. In order to test the console application run the following commands from the command line:
dotnet build
dotnet run