Thursday, March 14, 2019

Office 365 – Planner - Useful functions

Office 365 – Planner

·       Get all assigned planner

·       Get member collection [IUserMemberOfCollectionWithReferencesPage]

·       Get buckets [IPlannerPlanBucketsCollectionPage]

·       Get Site Url For Plans

 

using Microsoft.Graph;

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using System;

using System.Linq;

using System.Collections.Generic;

using System.Net.Http.Headers;

using System.Threading.Tasks;

 

namespace Office365.Planner.Extension

{

    public class MyPlans

    {

 

        /// <summary>

        /// Get all Plan information based on current user

        /// </summary>

        /// <returns></returns>

        public async static Task<List<PlansData>> GetPlans()

        {

            List<PlansData> plansData = new List<PlansData>();

            try

            {

                string accessToken = GetAccessToken();

                GraphServiceClient graphClient = GetGraphClient(accessToken);

                if (graphClient != null)

                {

                    plansData = await GetMemberCollection(graphClient, plansData);

 

                }

                else

                    throw new Exception("GraphServiceClient is null");

            }

            catch (Exception)

            {

            }

            finally

            {

            }

            return plansData;

 

        }

 

        /// <summary>

        /// Get Member Collection [Me]

        /// </summary>

        /// <param name="graphClient"></param>

        /// <param name="plansData"></param>

        /// <returns></returns>

        public async static Task<List<PlansData>> GetMemberCollection(GraphServiceClient graphClient, List<PlansData> plansData)

        {

            try

            {

                IUserMemberOfCollectionWithReferencesPage memberCollection = await graphClient.Me.MemberOf.Request().GetAsync();

 

                if (memberCollection?.Count > 0)

                {

                    foreach (var member in memberCollection)

                    {

                        if (member is Microsoft.Graph.Group)

                        {

                            Group group = (Group)member;

                            if (group != null && group.GroupTypes != null && group.GroupTypes.Count() > 0)

                            {

                                plansData = await GetPlanData(graphClient, plansData, group.Id, group.MailNickname);

                            }

                        }

                    }

                }

            }

            catch (Exception)

            {

            }

            return plansData;

        }

 

        /// <summary>

        /// Bind all plan data

        /// </summary>

        /// <param name="graphClient"></param>

        /// <param name="plansData"></param>

        /// <param name="groupId"></param>

        /// <param name="mailNickname"></param>

        /// <returns></returns>

        public async static Task<List<PlansData>> GetPlanData(GraphServiceClient graphClient, List<PlansData> plansData, string groupId, string mailNickname)

        {

            try

            {

                //Get Plans Information based on the group id

                IPlannerGroupPlansCollectionPage plansCollection = await graphClient.Groups[groupId].Planner.Plans.Request().GetAsync();

                if (plansCollection?.Count > 0)

                {

                    foreach (var record in plansCollection)

                    {

                        List<Bucket> bucketsList = await GetBuckets(graphClient, record.Id);

                        string siteUrl = GetSiteUrlForPlans(graphClient, groupId);

                        plansData.Add(new PlansData(record.Id, record.Title, siteUrl, bucketsList));

 

                    }

                }

            }

            catch (Exception)

            {

            }

            return plansData;

        }

 

        /// <summary>

        /// Get all buckets based on plan id

        /// </summary>

        /// <param name="graphClient"></param>

        /// <param name="planId"></param>

        /// <returns></returns>

        public async static Task<List<Bucket>> GetBuckets(GraphServiceClient graphClient, string planId)

        {

            List<Bucket> bucketsList = new List<Bucket>();

 

            try

            {

                //Get Plans Information based on the group id

                IPlannerPlanBucketsCollectionPage bucketCollection = await graphClient.Planner.Plans[planId].Buckets.Request().GetAsync();

                if (bucketCollection?.Count > 0)

                {

                    foreach (var bucket in bucketCollection)

                    {

                        Bucket b = new Bucket(bucket.Id, bucket.Name);

                        bucketsList.Add(b);

                    }

                }

            }

            catch (Exception)

            {

            }

            return bucketsList;

        }

 

        /// <summary>

        /// Get site url based on group id

        /// </summary>

        /// <param name="graphClient"></param>

        /// <param name="groupId"></param>

        /// <returns></returns>

        public static string GetSiteUrlForPlans(GraphServiceClient graphClient, string groupId)

        {

            try

            {

                Task<Group> groupCollection = graphClient.Groups[groupId].Request().GetAsync();

                groupCollection.Wait();

                Group group = groupCollection.Result;

                string companyGroupName = "Your Company Group Name";

                return string.Format("https://{0}.sharepoint.com/teams/{0}/", companyGroupName, group.MailNickname);

            }

            catch (Exception)

            {

            }

            return null;

        }

 

        /// <summary>

        /// Get AAD Access Token

        /// </summary>

        /// <returns></returns>

        public static string GetAccessToken()

        {

            string AppId = "";

            string TenantId = "";

            string GraphResourceUrl = "https://graph.microsoft.com";

            string AuthorityUrl = "https://login.microsoftonline.com/" + TenantId;

            string RedirectUri = "http://localhost:12345/";

 

            try

            {

                AuthenticationContext authContext = new AuthenticationContext(AuthorityUrl, true);

                AuthenticationResult authResult = authContext.AcquireTokenAsync(GraphResourceUrl, AppId, new Uri(RedirectUri), new PlatformParameters(PromptBehavior.Auto)).Result;

                return authResult.AccessToken;

 

            }

            catch (Exception ex)

            {

            }

            return null;

        }

 

        /// <summary>

        /// Get GraphServiceClient

        /// </summary>

        /// <param name="graphToken"></param>

        /// <returns></returns>

        public static GraphServiceClient GetGraphClient(string graphToken)

        {

            try

            {

                DelegateAuthenticationProvider authenticationProvider = new DelegateAuthenticationProvider(

                (requestMessage) =>

                {

                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", graphToken);

                    return Task.FromResult(0);

                });

                return new GraphServiceClient(authenticationProvider);

            }

            catch (Exception)

            {

            }

            return null;

        }

    }

 

    public class PlansData

    {

        public string ID { get; set; }

 

        public string Title { get; set; }

 

        public string SiteUrl { get; set; }

 

        public List<Bucket> Buckets { get; set; }

 

        public PlansData()

        { }

 

        public PlansData(string id, string title)

        {

            ID = id;

            Title = title;

        }

 

        public PlansData(string id, string title, List<Bucket> buckets)

        {

            ID = id;

            Title = title;

            Buckets = buckets;

        }

 

        public PlansData(string id, string title, string siteUrl, List<Bucket> buckets)

        {

            ID = id;

            Title = title;

            SiteUrl = siteUrl;

            Buckets = buckets;

        }

    }

 

    public class Bucket

    {

        public string ID { get; set; }

        public string Name { get; set; }

        public Bucket(string id, string name)

        {

            ID = id;

            Name = name;

        }

    }

}

 

No comments:

Post a Comment