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

Use of latest Microsoft.Graph nuget package #1406

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// Copyright (c) Microsoft. All Rights Reserved.
// </copyright>

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -118,53 +117,45 @@ public async Task<ActionResult> SendNotificationToManager(RequestInfo taskInfo)
var graphClient = SimpleGraphClient.GetGraphClient(taskInfo.access_token);

// Retrieve user information from Microsoft Graph API
var user = await graphClient.Users[taskInfo.personaName]
.Request()
.GetAsync();
var user = await graphClient.Users[taskInfo.personaName].GetAsync();

// Retrieve installed apps for the user from Microsoft Graph API
var installedApps = await graphClient.Users[user.Id].Teamwork.InstalledApps
.Request()
.Expand("teamsApp")
.GetAsync();
var installedApps = await graphClient.Users[user.UserPrincipalName].Teamwork.InstalledApps
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Expand = new string[] { "teamsAppDefinition" };
});

// Filter installed apps to find the one with DisplayName "Tab Request Approval"
var installationId = installedApps.Where(id => id.TeamsApp.DisplayName == "Tab Request Approval").Select(x => x.TeamsApp.Id);
var installationId = installedApps.Value.Where(id => id.TeamsAppDefinition.DisplayName == "Tab Request Approval").Select(x => x.TeamsAppDefinition.Id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pankajmunde12 rest changes working fine accept instead of
var installationId = installedApps.Value.Where(id => id.TeamsAppDefinition.DisplayName == "Tab Request Approval").Select(x => x.TeamsAppDefinition.Id);
It should be
var installationId = installedApps.Value.Where(id => id.TeamsAppDefinition.DisplayName == "Tab Request Approval").Select(x => x.TeamsAppDefinition.TeamsAppId);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Pawank-MSFT !
I have updated the code. Please check.


// Check if there is at least one matching installationId
if (installationId.Any())
{
// Construct URL for the Teams entity
var url = "https://teams.microsoft.com/l/entity/" + installationId.ToList()[0] + "/request?context={\"subEntityId\":\"" + taskInfo.taskId + "\"}";

// Create a TeamworkActivityTopic for the notification
var topic = new TeamworkActivityTopic
{
Source = TeamworkActivityTopicSource.Text,
Value = $"{taskInfo.title}",
WebUrl = url
};

// Create preview text for the notification
var previewText = new ItemBody
{
Content = $"Request By: {taskInfo.userName}"
};

// Create template parameters for the notification
var templateParameters = new List<Microsoft.Graph.KeyValuePair>()
var requestBody = new Microsoft.Graph.Users.Item.Teamwork.SendActivityNotification.SendActivityNotificationPostRequestBody
{
new Microsoft.Graph.KeyValuePair
Topic = new TeamworkActivityTopic
{
Source = TeamworkActivityTopicSource.Text,
Value = $"{taskInfo.title}",
WebUrl = "https://teams.microsoft.com/l/entity/" + _configuration["AzureAd:MicrosoftAppId"] + "/request?context={\"subEntityId\":\"" + taskInfo.taskId + "\"}"
},
ActivityType = "approvalRequired",
PreviewText = new ItemBody
{
Name = "approvalTaskId",
Value = taskInfo.title
}
Content = $"Request By: {taskInfo.userName}"
},
TemplateParameters = new List<Microsoft.Graph.Models.KeyValuePair>
{
new Microsoft.Graph.Models.KeyValuePair
{
Name = "approvalTaskId",
Value = taskInfo.title
},
},
};
// Send the activity notification using Microsoft Graph API
await graphClient.Users[user.Id].Teamwork
.SendActivityNotification(topic, "approvalRequired", null, previewText, templateParameters)
.Request()
.PostAsync();

await graphClient.Users[user.Id].Teamwork.SendActivityNotification.PostAsync(requestBody);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using Microsoft.Kiota.Abstractions.Authentication;

namespace TabRequestApproval
{
public class SimpleGraphClient
{
public static GraphServiceClient GetGraphClient(string accessToken)
{
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
TokenProvider provider = new TokenProvider();
provider.token = accessToken;
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(provider);
var graphServiceClient = new GraphServiceClient(authenticationProvider);
return graphServiceClient;
}

requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"");
}

return Task.CompletedTask;
}));
public class TokenProvider : IAccessTokenProvider
{
public string token { get; set; }
public AllowedHostsValidator AllowedHostsValidator => throw new NotImplementedException();

return graphClient;
public Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object>? additionalAuthenticationContext = null, CancellationToken cancellationToken = default)
{
return Task.FromResult(token);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.11" />
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.18.1" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.18.1" />
<PackageReference Include="Microsoft.Graph" Version="5.58.0" />
<PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.6" />
<PackageReference Include="Microsoft.Graph.Beta" Version="0.35.0-preview" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.48.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
</ItemGroup>
Expand Down