0
votes

I am trying to update a SharePoint list through an InfoPath form. A starting date SelectedDAM is input, and the code then calculates 6 dates. Newly calculated dates have to be updated in the original SP list.

The code below raises no exception in debug mode, and even in production. I checked that 'jobs' gets populated with data, and that le list names are as per their SharePoint name. Saddly, when I look to the SharePoint list afterward, items were not updated.

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);
        }

Why is it not working ? Is there any additionnal line I could add to confirm update.

1
@ama1111 Thanks again. Looking at the Results XmlNode '<ErrorCode>0x81020014</ErrorCode><ErrorText>One or more field types are not installed properly. Go to the list settings page to delete these fields.</ErrorText>' showed up. I will now try to find if error is due to field types, as stated, or to wrong list reference, as suggested in other post [link]stackoverflow.com/questions/1300656/… - Spelya
Above also raises another question: How can intercept that kind of inner error? When I run InfoPath it went unnoticed, and I thought it was successfull. - Spelya
I can now intercept the error using a condition 'If(Results.XmlText != "0x00000000"'. Thanks @ama1111. - Spelya
You're welcome! I'm glad you can detect the error now. - ama1111

1 Answers

0
votes

To confirm the update you can check the return value of Lists.UpdateListItems, which is an XmlNode that contains the status of the update.

From 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.