首页 > 解决方案 > SharePoint 2019 CSOM 不保存 FieldUserValue

问题描述

当我在 SharePoint2019 列表中插入或更新包含用户列的项目时,我遇到了一个奇怪的问题,保存完成且没有错误,但用户列始终具有空值,

下面是我的代码

            context.Load(context.Web, web => web.Lists);
            await context.ExecuteQueryAsync();

            List RiskList = context.Web.Lists.GetByTitle("Risks");

            context.Load(RiskList);
            context.Load(RiskList, r => r.Fields);
            await context.ExecuteQueryAsync();

            ListItem listItem = RiskList.GetItemById(projectRisks.Id);
            List<ListItemFormUpdateValue> listItemFormUpdateValue = new List<ListItemFormUpdateValue>();
            if (projectRisks.AssignedTo.HasValue)
            {
                listItem["AssignedTo"] =  new FieldUserValue() { LookupId = projectRisks.AssignedTo.Value };
            }
            if (projectRisks.Owner.HasValue)
            {
                User OwnerUser = context.Web.SiteUsers.GetById(projectRisks.Owner.Value);
                context.Load(OwnerUser);
                await context.ExecuteQueryAsync();
                listItem["Owner"] = new FieldUserValue() { LookupId = OwnerUser.Id };
            }
            listItemFormUpdateValue.Add(new ListItemFormUpdateValue() { FieldName = "Category", FieldValue = GetSPSelectedChoiceValue(context, RiskList, "Category", projectRisks.CategoryName).Result });
            listItemFormUpdateValue.Add(new ListItemFormUpdateValue() { FieldName = "Status", FieldValue = GetSPSelectedChoiceValue(context, RiskList, "Status", projectRisks.StatusName).Result });
            listItem["Contingency_x0020_plan"] = projectRisks.ContingencyPlan;
            listItem["Cost"] = projectRisks.Cost;
            listItem["Cost_x0020_Exposure"] = string.Empty;
            listItem["Description"] = projectRisks.Description;
            listItem["DueDate"] = projectRisks.DueDate;
            listItem["Exposure"] = projectRisks.Exposure;
            listItem["Impact"] = projectRisks.Impact;
            listItem["Mitigation_x0020_plan"] = projectRisks.MitigationPlan;
            listItem["Probability"] = projectRisks.Probability;
            listItem["Title"] = projectRisks.Name;
            listItem.ValidateUpdateListItem(listItemFormUpdateValue, false, string.Empty);
            listItem.Update();
            await context.ExecuteQueryAsync();

正如您在 AssignTo 和 Owner Columns 中看到的那样,无论我如何尝试保存用户值,该列表都将采用默认值为 null。

I have made sure that there is values in the assigned to and Owner properties我试过使用 ListItemFormUpdateValue 但没有运气。

提前致谢

标签: csomsharepoint-2019

解决方案


推荐阅读