Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pnp#729 - Experimental support for creating and/or adding plans to a team #741

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions src/lib/PnP.Framework/Provisioning/ObjectHandlers/ObjectTeams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,8 @@ private static bool SetTeamChannels(PnPMonitoredScope scope, TokenParser parser,
if (!SetTeamTabs(scope, parser, channel.Tabs, teamId, channelId, accessToken)) return false;
}

// TODO: Handle TabResources
// We need to define a "schema" for their settings
// Experimental implementation of Planner Tab Resource
SetTabResources(scope, parser, channel.TabResources, teamId, channelId, accessToken);

if (channel.Messages != null && channel.Messages.Any())
{
Expand All @@ -937,6 +937,67 @@ private static bool SetTeamChannels(PnPMonitoredScope scope, TokenParser parser,
return true;
}

private static JToken GetExistingPlans(string teamId, string accessToken)
{
return JToken.Parse(HttpHelper.MakeGetRequestForString($"{GraphHelper.MicrosoftGraphBaseURI}beta/groups/{teamId}/planner/plans", accessToken))["value"];
}

public static JToken CreatePlan(PnPMonitoredScope scope, TokenParser parser, string teamId, string accessToken, TeamTabResource resource)
{
var config = JToken.Parse(resource.TabResourceSettings);
var title = config["displayName"].ToString();
var owner = teamId;
var payload = new
{
title,
owner
};
return JToken.Parse(HttpHelper.MakePostRequestForString($"{GraphHelper.MicrosoftGraphBaseURI}v1.0/planner/plans", payload, HttpHelper.JsonContentType, accessToken));
}

private static bool SetTabResources(PnPMonitoredScope scope, TokenParser parser, TeamTabResourceCollection tabResources, string teamId, string channelId, string accessToken)
{
foreach (var resource in tabResources)
{
switch (resource.Type)
{
case TabResourceType.Planner:
var config = JToken.Parse(resource.TabResourceSettings);
var title = config["displayName"].ToString();
var existingPlans = GetExistingPlans(teamId, accessToken);
var plan = existingPlans.SingleOrDefault(r => r["displayName"].ToString() == title);
// If exists, get it, if not create it and return it
if (plan == null)
{
plan = CreatePlan(scope, parser, teamId, accessToken, resource);
}
var planId = plan["id"].ToString();
var tenantId = parser.ParseString("{$authority}");
var tabId = parser.ParseString(resource.TargetTabId);

string template = "https://tasks.teams.microsoft.com/teamsui/" + tenantId + "/Home/PlannerFrame?page={pageId}&planId=" + planId + "&auth_pvr=Orgid&auth_upn={userPrincipalName}&groupId={groupId}&entityId={entityId}&tid={tid}&userObjectId={userObjectId}&channelId={channelId}&sessionId={sessionId}&theme={theme}&mkt={locale}&ringId={ringId}&PlannerRouteHint=" + tenantId;

UpdateTeamTab(new TeamTab()
{
Configuration = new TeamTabConfiguration()
{
EntityId = planId,
ContentUrl = template.Replace("{pageId}", "7"),
RemoveUrl = template.Replace("{pageId}", "13"),
WebsiteUrl = "https://tasks.office.com/" + tenantId + "/Home/PlanViews/" + planId
},
DisplayName = parser.ParseString(title)
}, parser, teamId, channelId, tabId, accessToken); ;

break;
default:
break;
}
}

return true;
}

public static JToken GetExistingTeamChannels(string teamId, string accessToken)
{
var channels = string.Empty;
Expand Down