0
votes

I have an InfoPath form with custom submit code to update a Sharepoint list by calling the Sharepoint Lists web service. The code runs without any exceptions, and I was able to set breakpoints to make sure that the variables contain the correct values before being sent to the web service. The values never get added to the Sharepoint list, though. Here is my code:

[InfoPathEventHandler(MatchPath = "Submit", EventType = InfoPathEventType.OnClick)]
    public void Submit_OnClick(DocActionEvent e)
    {
        ListsService.Lists listService = new Risk_Form.ListsService.Lists();
        listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

        string riskID = thisXDocument.DOM.selectSingleNode("//my:myFields/my:RiskID").text;
        string headline = thisXDocument.DOM.selectSingleNode("//my:myFields/my:RiskHeadline").text;

        XmlDocument doc = new XmlDocument();
        XmlElement batch = doc.CreateElement("Batch");
        batch.SetAttribute("OnError", "Continue");
        batch.SetAttribute("ListVersion", "1");

        batch.InnerXml = 
            "<Method ID='" + riskID + "' Cmd='New'>" +
                "<Field Name='RiskID'>" + riskID + "</Field>" +
                "<Field Name='Headline'>" + headline + "</Field>" + 
            "</Method>";
        try
        {
            // Update list using the list's GUID
            listService.UpdateListItems("2F6CA5F4-D78A-4716-B111-507917CF89E4", batch);
        }
        catch(Exception ex)
        {
            thisXDocument.DOM.selectSingleNode("//my:myFields/my:RiskStatement").text = ex.Message;
        }
    }
4
Not downvoting or voting to close yet, but... did you forget to ask a question?Greg Hurlman

4 Answers

1
votes

Two things:

  1. You might also need the default View ID in your batch when calling UpdateListItems().

  2. Instead of hardcoding the list guid, you can obtain it programatically by calling listService.GetListAndView().

Here is some code to demonstrate both items:

System.Xml.XmlNode ndListView = listService.GetListAndView(DISPLAYNAMEOFLIST, "");
string listGuid = ndListView.ChildNodes[0].Attributes["Name"].Value;
string listView = ndListView.ChildNodes[1].Attributes["Name"].Value;

batch.SetAttribute("ViewName", listView);

You can then just call UpdateListItems() with listGuid and batch.

1
votes

Ok, I finally figured this stupid bug out. There was a list on the root Sharepoint site with the same display name as the list I was trying to access on my subsite. Even though my service reference pointed to the Lists web service located on my subsite, it was still returning the wrong list. I used the internal name for my list and now it works.

0
votes

From the documentation on MSDN: It is recommended that you use the list GUID surrounded by curly braces (i.e., "{GUID}"), but you can also use the list display name.

Those curly braces seem to be missing in your call.

0
votes

I found a partial answer to my problem. When I added the service reference to the subsite I am working on, for some reason app.config still contained a reference to the root Sharepoint site. Therefore the list I was looking for did not exist. Now I'm having another problem, though. I check the return value of the UpdateListItems() call, and I get the following error: "One or more field types are not installed properly. Go to the list settings page to delete these fields." I searched around and all of the problems that cause this error seem to involve having a field name with a space in it. Neither of my fields have spaces in them, though. Here is my updated code:

        ListsService.Lists listService = new Risk_Form.ListsService.Lists();
        listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        XmlNode list = null;

        list = listService.GetListAndView("Risks", "");

        string listID = list.ChildNodes[0].Attributes["Name"].Value;
        string viewID = list.ChildNodes[1].Attributes["Name"].Value;

        string riskID = thisXDocument.DOM.selectSingleNode("//my:myFields/my:RiskID").text;
        string headline = thisXDocument.DOM.selectSingleNode("//my:myFields/my:RiskHeadline").text;

        XmlDocument doc = new XmlDocument();
        XmlElement batch = doc.CreateElement("Batch");
        batch.SetAttribute("OnError", "Continue");
        batch.SetAttribute("ListVersion", "1");
        batch.SetAttribute("ViewName", viewID);

        batch.InnerXml = 
            "<Method ID='1' Cmd='New'>" +
                "<Field Name='RiskID'>" + riskID + "</Field>" +
                "<Field Name='Headline'>" + headline + "</Field>" + 
            "</Method>";

        XmlNode ret = listService.UpdateListItems(listID, batch);
        MessageBox.Show(ret.OuterXml);