如何验证 SharePoint 列表项更新是否成功
How to validate if SharePoint list item update was successfull
我正在尝试通过 InfoPath 表单更新 SharePoint 列表。输入开始日期 SelectedDAM,然后代码计算 6 个日期。必须在原始 SP 列表中更新新计算的日期。
下面的代码在调试模式下没有引发异常,甚至在生产中也是如此。我检查了 'jobs' 是否填充了数据,并且文件列表名称与它们的 SharePoint 名称一致。遗憾的是,当我之后查看 SharePoint 列表时,项目没有更新。
public void SetDAMButton_Clicked(object sender, ClickedEventArgs e)
{
string SelectedCEC = nav.SelectSingleNode("/my:mesChamps/my:DAMSection/my:CEC", this.NamespaceManager).Value;
string SelectedDAM = nav.SelectSingleNode("/my:mesChamps/my:DAMSection/my:DAM", this.NamespaceManager).Value;
//Query all jobs and dates from SharePoint List
XmlDocument xmlDoc = new XmlDocument();
XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode viewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
query.InnerXml = "<Where>" +
"<Contains><FieldRef Name='CEC'/>" +
"<Value Type='Text'>" + SelectedCEC.Trim() + "</Value></Contains>" +
"</Where>";
viewFields.InnerXml = "<FieldRef Name='ID' />" +
"<FieldRef Name='JRS_x0020_Avant' />"+ "<FieldRef Name='JRS_x0020_Pr_x00e9_vus' />"+
"<FieldRef Name='JRS_x0020_Prevus_x0020_2' />"+ "<FieldRef Name='JRS_x0020_Avant_x0020_2' />"+
"<FieldRef Name='JRS_x0020_Pr_x00e9_vus_x0020_3' />" + "<FieldRef Name='JRS_x0020_Avant_x0020_3' />";
XmlNode listItems = sp.getListService().GetListItems(SPJobsListGlobal, null, query, viewFields, "10000000", null, null);
//Extract jobs data to an internal list
List<string[]> jobs = new List<string[]>();
for (int i = 1; i != listItems.ChildNodes[1].ChildNodes.Count; i += 2)
{
string jobId = listItems.ChildNodes[1].ChildNodes[i].Attributes[0].InnerXml;
string JRSAvant = listItems.ChildNodes[1].ChildNodes[i].Attributes[1].InnerXml;
string JRSPrevus = listItems.ChildNodes[1].ChildNodes[i].Attributes[2].InnerXml;
string JRSPrevus2 = listItems.ChildNodes[1].ChildNodes[i].Attributes[3].InnerXml;
string JRSAvant2 = listItems.ChildNodes[1].ChildNodes[i].Attributes[4].InnerXml;
string JRSPrevus3 = listItems.ChildNodes[1].ChildNodes[i].Attributes[5].InnerXml;
string JRSAvant3 = listItems.ChildNodes[1].ChildNodes[i].Attributes[6].InnerXml;
jobs.Add(new string[] { jobId.Trim(), JRSAvant.Trim(), JRSPrevus.Trim(),JRSAvant2.Trim(), JRSPrevus2.Trim(), JRSAvant3.Trim(), JRSPrevus3.Trim() });
}
jobs.RemoveAt(jobs.Count - 1);
//Update each item of SharePoint list with dates calculated from new DAM
for(int i = 0; i != jobs.Count; i++)
{
XmlDocument doc = new XmlDocument();
XmlElement batch = doc.CreateElement("Batch");
batch.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>" + SecurityElement.Escape(jobs[i][0]) + "</Field>" +
"<Field Name='DateDebut'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][1])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='DateFin'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][1])).AddDays(validateJRS(jobs[i][2])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_D_x00e9_but_x0020_2'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][3])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_Fin_x0020_2'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][3])).AddDays(validateJRS(jobs[i][4])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_D_x00e9_but_x0020_3'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][5])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_Fin_x0020_3'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][5])).AddDays(validateJRS(jobs[i][6])).ToString("yyyy-MM-dd")) + "</Field>" +
"</Method>";
sp.UpdateListItems(SPJobsListGlobal, batch);
}
为什么不起作用?我可以添加任何额外的行来确认更新吗?
要确认更新,您可以检查 Lists.UpdateListItems
的 return 值,这是一个包含更新状态的 XmlNode
。
来自MSDN:
Return value
An XMLDATA fragment in the following form that shows the status of each method block posted through the updates parameter and that can be assigned to a System.Xml.XmlNode object. For items successfully updated, a row fragment is returned with the updated row values.
我正在尝试通过 InfoPath 表单更新 SharePoint 列表。输入开始日期 SelectedDAM,然后代码计算 6 个日期。必须在原始 SP 列表中更新新计算的日期。
下面的代码在调试模式下没有引发异常,甚至在生产中也是如此。我检查了 'jobs' 是否填充了数据,并且文件列表名称与它们的 SharePoint 名称一致。遗憾的是,当我之后查看 SharePoint 列表时,项目没有更新。
public void SetDAMButton_Clicked(object sender, ClickedEventArgs e)
{
string SelectedCEC = nav.SelectSingleNode("/my:mesChamps/my:DAMSection/my:CEC", this.NamespaceManager).Value;
string SelectedDAM = nav.SelectSingleNode("/my:mesChamps/my:DAMSection/my:DAM", this.NamespaceManager).Value;
//Query all jobs and dates from SharePoint List
XmlDocument xmlDoc = new XmlDocument();
XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode viewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
query.InnerXml = "<Where>" +
"<Contains><FieldRef Name='CEC'/>" +
"<Value Type='Text'>" + SelectedCEC.Trim() + "</Value></Contains>" +
"</Where>";
viewFields.InnerXml = "<FieldRef Name='ID' />" +
"<FieldRef Name='JRS_x0020_Avant' />"+ "<FieldRef Name='JRS_x0020_Pr_x00e9_vus' />"+
"<FieldRef Name='JRS_x0020_Prevus_x0020_2' />"+ "<FieldRef Name='JRS_x0020_Avant_x0020_2' />"+
"<FieldRef Name='JRS_x0020_Pr_x00e9_vus_x0020_3' />" + "<FieldRef Name='JRS_x0020_Avant_x0020_3' />";
XmlNode listItems = sp.getListService().GetListItems(SPJobsListGlobal, null, query, viewFields, "10000000", null, null);
//Extract jobs data to an internal list
List<string[]> jobs = new List<string[]>();
for (int i = 1; i != listItems.ChildNodes[1].ChildNodes.Count; i += 2)
{
string jobId = listItems.ChildNodes[1].ChildNodes[i].Attributes[0].InnerXml;
string JRSAvant = listItems.ChildNodes[1].ChildNodes[i].Attributes[1].InnerXml;
string JRSPrevus = listItems.ChildNodes[1].ChildNodes[i].Attributes[2].InnerXml;
string JRSPrevus2 = listItems.ChildNodes[1].ChildNodes[i].Attributes[3].InnerXml;
string JRSAvant2 = listItems.ChildNodes[1].ChildNodes[i].Attributes[4].InnerXml;
string JRSPrevus3 = listItems.ChildNodes[1].ChildNodes[i].Attributes[5].InnerXml;
string JRSAvant3 = listItems.ChildNodes[1].ChildNodes[i].Attributes[6].InnerXml;
jobs.Add(new string[] { jobId.Trim(), JRSAvant.Trim(), JRSPrevus.Trim(),JRSAvant2.Trim(), JRSPrevus2.Trim(), JRSAvant3.Trim(), JRSPrevus3.Trim() });
}
jobs.RemoveAt(jobs.Count - 1);
//Update each item of SharePoint list with dates calculated from new DAM
for(int i = 0; i != jobs.Count; i++)
{
XmlDocument doc = new XmlDocument();
XmlElement batch = doc.CreateElement("Batch");
batch.InnerXml = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>" + SecurityElement.Escape(jobs[i][0]) + "</Field>" +
"<Field Name='DateDebut'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][1])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='DateFin'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][1])).AddDays(validateJRS(jobs[i][2])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_D_x00e9_but_x0020_2'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][3])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_Fin_x0020_2'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][3])).AddDays(validateJRS(jobs[i][4])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_D_x00e9_but_x0020_3'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][5])).ToString("yyyy-MM-dd")) + "</Field>" +
"<Field Name='Date_x0020_Fin_x0020_3'>" + SecurityElement.Escape(Convert.ToDateTime(SelectedDAM).AddDays(-validateJRS(jobs[i][5])).AddDays(validateJRS(jobs[i][6])).ToString("yyyy-MM-dd")) + "</Field>" +
"</Method>";
sp.UpdateListItems(SPJobsListGlobal, batch);
}
为什么不起作用?我可以添加任何额外的行来确认更新吗?
要确认更新,您可以检查 Lists.UpdateListItems
的 return 值,这是一个包含更新状态的 XmlNode
。
来自MSDN:
Return value
An XMLDATA fragment in the following form that shows the status of each method block posted through the updates parameter and that can be assigned to a System.Xml.XmlNode object. For items successfully updated, a row fragment is returned with the updated row values.