Share via

Need graph api call full code for pushing notification for particullar user in C# console app

Mayur 0 Reputation points
2024-11-26T14:43:50.52+00:00

Hello,

I am not able to find the full code or latest code that will send notification to the user from c# console application.Need your help please.

I have tried this link https://learn.microsoft.com/en-us/graph/teams-send-activityfeednotifications?tabs=desktop%2Chttp

The code mention there its half.

Graph api calls are also not working.

Microsoft Teams | Development
Microsoft Teams | Development

Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs

Microsoft Security | Microsoft Graph
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,456 Reputation points
    2024-11-27T02:52:31.2366667+00:00

    Hi @Mayur

    Please refer to the following code:

    // Code snippets are only available for the latest version. Current version is 5.x
    
    // Dependencies
    using Microsoft.Graph.Users.Item.Teamwork.SendActivityNotification;
    using Microsoft.Graph.Models;
    using Azure.Identity;
    using Microsoft.Graph;
    using Microsoft.Graph.Models.ODataErrors;
    
    // The client credentials flow requires that you request the
    // /.default scope, and pre-configure your permissions on the
    // app registration in Azure. An administrator must grant consent
    // to those permissions beforehand.
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    // Values from app registration
    var clientId = "YOUR_CLIENT_ID";
    var tenantId = "YOUR_TENANT_ID";
    var clientSecret = "YOUR_CLIENT_SECRET";
    
    // using Azure.Identity;
    var options = new ClientSecretCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    };
    
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
    try
    {
    
    var requestBody = new SendActivityNotificationPostRequestBody
    {
    	Topic = new TeamworkActivityTopic
    	{
    		Source = TeamworkActivityTopicSource.EntityUrl,
    		Value = "https://graph.microsoft.com/v1.0/users/{userId}/teamwork/installedApps/{installationId}",
    	},
    	ActivityType = "taskCreated",
    	PreviewText = new ItemBody
    	{
    		Content = "New Task Created",
    	},
    	TemplateParameters = new List<KeyValuePair>
    	{
    		new KeyValuePair
    		{
    			Name = "taskId",
    			Value = "Task 12322",
    		},
    	},
    };
    
    // To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
    await graphClient.Users["{user-id}"].Teamwork.SendActivityNotification.PostAsync(requestBody);
    
    }
    catch (ODataError odataError)
    {
        Console.WriteLine(odataError.Error.Code);
        Console.WriteLine(odataError.Error.Message);
       
    }
    

    API source: https://learn.microsoft.com/en-us/graph/api/userteamwork-sendactivitynotification?view=graph-rest-1.0&tabs=http.


    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.