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 Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,443 questions
C#
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.
11,089 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,389 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,396 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 43,011 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.


Your answer

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