skip to Main Content

I work with Microsoft.Graph and there was a problem with adding a field of the Person or Group type. The code shown now adds a new task to the list of SharePoint tasks, which is based on the data that the server processes and issues, but the problem is that the User to whom the task should be created is not displayed, obviously, it is not created.

using Google.Protobuf;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using MySqlX.XDevAPI.Common;
using Newtonsoft.Json.Linq;
using SunApproverFlowsServer.Server.Data;
using SunApproverFlowsServer.Server.Models;
using SunApproverFlowsServer.Server.Services.Interfaces;
using System.Data;
using System.Text.Json;
using System.Xml;
using System.Xml.Linq;

namespace SunApproverFlowsServer.Server.Services.TaskMenagerService
{
    public class UserLookup
    {
        public int LookupId { get; set; }
        public string LookupValue { get; set; }
        public string Email { get; set; }
    }


    public class TaskManagerService : ITaskManager
    {
        private readonly IConfiguration Configuration;
        private readonly DataContext _context;

        public TaskManagerService(IConfiguration configuration, DataContext context)
        {
            Configuration = configuration;
            _context = context;
        }

        public async Task<string> AddTaskAsync(ParseXMLDateModel? parseXMLDate)
        {
            int workflowId = _context.SunApproversWorkflows.FirstOrDefault(el => el.ListGuid == parseXMLDate.ListId).WorkflowId;

            string siteName = Configuration["SharepointSiteInf:SiteName"];
            string clientId = Configuration["AuthenticationData:ClientId"];
            string tenantId = Configuration["AuthenticationData:TenantId"];
            string clientSecret = Configuration["AuthenticationData:ClientSecret"];
            string taskListName = Configuration["ListsTitles:TaskListName"];
            string linksListName = Configuration["ListsTitles:LinksListName"];

            try
            {
                var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
                GraphServiceClient _graphClient = new GraphServiceClient(clientSecretCredential);

                var itemsFilds = await _graphClient.Sites[siteName].Lists[linksListName].Items.Request()
                                                                                              .Expand("Fields")
                                                                                              .GetAsync();

                foreach (var item in itemsFilds)
                {
                    var workflowLookupId = item.Fields.AdditionalData["WorkflowLookupId"].ToString();

                    if (workflowLookupId != null && Convert.ToInt32(workflowLookupId) == workflowId)
                    {
                        //var usersArray = item.Fields.AdditionalData["Users"];

                        var usersArrayJson = item.Fields.AdditionalData["Users"].ToString();
                        var usersArray = JsonSerializer.Deserialize<List<UserLookup>>(usersArrayJson);

                        var lookupIds = usersArray.Select(user => user.LookupId.ToString()).ToArray();

                        var requestBody = new ListItem
                        {
                            Fields = new FieldValueSet
                            {
                                AdditionalData = new Dictionary<string, object>
                                {
                                    { 
                                        "Title", item.Fields.AdditionalData["StatusLookupId"] ?? "Not specified"
                                    },
                                    {
                                        "[email protected]", "Collection(Edm.String)"
                                    },
                                    {
                                        "AssignedTo", lookupIds
                                    },
                                    { 
                                        "Body", parseXMLDate.Description ?? "Not specified" 
                                    }
                                }
                            }
                        };

                        var result = await _graphClient.Sites[siteName].Lists[taskListName].Items.Request().AddAsync(requestBody);
                    }
                }

                return $" nIn task list "{taskListName}" added new item.n";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }


    }
}

As a result of working out, the following list is issued:
Example of adding new task

The project uses Microsoft Graph version 4.54.0. I would be glad to receive information on both this and newer versions. Thanks in advance for your reply.

2

Answers


  1. Chosen as BEST ANSWER

    The solution that worked for me:

    var usersArray = JsonSerializer.Deserialize<List<UserLookup>>(usersArrayJson);
    
    var lookupIds = usersArray.Select(user => user.LookupId).ToArray();
    
    var requestBody = new ListItem();
    var fild = new FieldValueSet();
    fild.AdditionalData = new Dictionary<string, object>();
    fild.AdditionalData.Add("Title", item.Fields.AdditionalData["StatusLookupId"] ?? "Not specified");
    fild.AdditionalData.Add("Body", parseXMLDate.Description ?? "Not specified");
    fild.AdditionalData.Add("AssignedToLookupId", lookupIds);
    fild.AdditionalData.Add("[email protected]", "Collection(Edm.Int32)");
    requestBody.Fields = fild;
    
    var result = await _graphClient.Sites[siteName].Lists[taskListName].Items.Request().AddAsync(requestBody);```
    

  2. Not sure if it will make any difference, but AssignedTo should be collection of Edm.Int32, not Edm.String.

    var lookupIds = usersArray.Select(user => user.LookupId).ToArray();
    
    var requestBody = new ListItem
    {
        Fields = new FieldValueSet
        {
            AdditionalData = new Dictionary<string, object>
            {
                { 
                    "Title", item.Fields.AdditionalData["StatusLookupId"] ?? "Not specified"
                },
                {
                    "[email protected]", "Collection(Edm.Int32)"
                },
                {
                    "AssignedTo", lookupIds
                },
                { 
                    "Body", parseXMLDate.Description ?? "Not specified" 
                }
            }
        }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search