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:
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
The solution that worked for me:
Not sure if it will make any difference, but
AssignedTo
should be collection ofEdm.Int32
, notEdm.String
.