我使用 Microsoft.Graph,添加“人员”或“组”类型的字段时出现问题。现在显示的代码将一个新任务添加到 SharePoint 任务列表中,该任务基于服务器处理和发出的数据,但问题是没有显示应为其创建任务的 User,显然,它是未创建。
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;
}
}
}
}
该项目使用 Microsoft Graph 版本 4.54.0。我很高兴收到有关此版本和更新版本的信息。预先感谢您的回复。
不确定它是否会产生任何影响,但是
AssignedTo
应该是Edm.Int32
的集合,而不是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"
}
}
}
};