c# - 如何使用 .Net 更新 Sharepoint ListItem?
问题描述
基本上我查询 Sharepoint 并获取 ListItems 列表。我遍历列表并检查是否需要从外部数据库更新项目(该代码不存在)
这是我正在运行的一段代码,它不会更新 Sharepoint ListItem。我什至尝试了不同的凭据无济于事。
using(ClientContext ctx = new ClientContext(searchsiteurl)) {
//NetworkCredential credit = new NetworkCredential(prg.userName, prg.password, prg.domain);
//ctx.Credentials = credit;
Web web = ctx.Web;
List list = web.Lists.GetById(new Guid(site.ListGUID));
var q = new CamlQuery();
if (Fullsync) {
q.ViewXml = "<View><Query><Where><And><BeginsWith><FieldRef Name='SrNumber' /><Value Type='Text'>1</Value></BeginsWith>" + "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Draft</Value></Eq></And></Where></Query></View>";
}
else {
q.ViewXml = "<View><Query><Where><And><Contains><FieldRef Name='SrNumber' /><Value Type='Text'>1-</Value></Contains><And>" + "<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq>" + "<Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + last24hours + "</Value></Gt>" + "</And></And></Where></Query></View>";
}
var r = list.GetItems(q);
ctx.Load(r);
ctx.ExecuteQuery();
foreach(SP.ListItem lit in r) {
//do a whole bunch of stuff....
// this does NOT WORK
lit.FieldValues["Linked_x0020_CSRs"] = LinkedSRs;
lit.Update();
ctx.ExecuteQuery();
}
}
解决方案
文档的问题在于它没有明确指出您必须使用 .GetItemById() 函数来检索 ListItem 才能更新该 ListItem。
所以这里的代码应该可以帮助未来的人们寻找这个答案。我花了很长时间才弄清楚这一点。
using (ClientContext ctx = new ClientContext(searchsiteurl))
{
//NetworkCredential credit = new NetworkCredential(prg.userName, prg.password, prg.domain);
//ctx.Credentials = credit;
Web web = ctx.Web;
List list = web.Lists.GetById(new Guid(site.ListGUID));
var q = new CamlQuery();
if (Fullsync)
{
q.ViewXml = "<View><Query><Where><And><BeginsWith><FieldRef Name='SrNumber' /><Value Type='Text'>1</Value></BeginsWith>" +
"<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Draft</Value></Eq></And></Where></Query></View>";
}
else
{
q.ViewXml = "<View><Query><Where><And><Contains><FieldRef Name='SrNumber' /><Value Type='Text'>1-</Value></Contains><And>" +
"<Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>Approved</Value></Eq>" +
"<Gt><FieldRef Name='Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" + last24hours + "</Value></Gt>" +
"</And></And></Where></Query></View>";
}
var r = list.GetItems(q);
ctx.Load(r);
ctx.ExecuteQuery();
foreach (SP.ListItem lit in r)
{
//do a whole bunch of stuff....
/* this does NOT WORK
lit.FieldValues["Linked_x0020_CSRs"] = LinkedSRs;
lit.Update();
ctx.ExecuteQuery();
*/
// this works!
var KAToModify = list.GetItemById(lit.Id);
KAToModify["Linked_x0020_CSRs"] = LinkedSRs;
KAToModify.Update();
ctx.ExecuteQuery();
}
}
推荐阅读
- maven - artifactId Selenium-java.jar 从 3.3.1 更改为 3.4 导致我的 maven 项目中出现大量“无法访问”错误
- performance - 如何分解scala.js JS输出的大小
- entity-framework - 实体框架使用参数执行存储过程
- javascript - 一年后,仍然在单元测试、集成测试和 E2E 测试中苦苦挣扎
- c# - 本地数据库连接字符串不起作用,但上下文连接起作用
- javascript - 如何获取平均大小的 Web 应用程序使用的文件列表
- ios - 使用 plist 文件在 iPhone / iPad 上安装应用程序时如何正确显示图标和进度?
- c# - IBM.Data.DB2.Core DataReader.GetSchemaTable() 方法抛出 NotSupportedException C# .NET 标准
- google-sheets - 多层匹配的空白单元格
- ios - 为什么我的收藏视图单元格不显示...?