Resolution: SharePoint throws an error while adding to lists

Cause

Column name's internal name is different than the column name

 

Resolution

We modified the list column names in the code sample by checking the actual column names from the list setting and after that we were able to add new items in the list.

To identify the column name
List -> Modify settings and columns -> column -> properties -> in URL we will have field value

To identify the view Guid
List -> Modify settings and columns -> Click on the view ( select your view, Ex. All items) -> In URL you will find something like this “View=%7B1F3EA68A%2D0212%2D4ECC%2D9958%2D5F8886A5D918%7D”

Replace
%7B  with {
%7D  with }
%2D  with -

 

Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ListUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //method1();
            //method2();
            CustomerCode();
        }

        private void CustomerCode()
        {
            string elements = @"<Method ID='1' Cmd='New'>
                        <Field Name='ID'>New</Field>
                        <Field Name='Title'>Tj</Field>
                        <Field Name='Entry_x0020_ID'>0D502EBD184B89</Field>
                        <Field Name='Status'>Not Started</Field>
                        <Field Name='Description'>Please open the attached document.</Field>
                        </Method>";

            string documentPath = @"C:\check.pdf";
            string strRowID = "";

            SendDocument(elements, documentPath, ref strRowID);
        }

        public void SendDocument(string elements, string documentPath, ref string strRowID)
        {
            XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement updates = xmlDoc.CreateElement("Batch");
            updates.SetAttribute("OnError", "Continue");
            updates.SetAttribute("ListVersion", "1");
            ////v3
            //updates.SetAttribute("ViewName", "{1F3E238A-0212-42CC-9958-5F2BB565D918}");

            //v2
            updates.SetAttribute("ViewName", "{62E66878-5D1C-4R25-A2F8-AA71888A55FF}");
            updates.InnerXml = elements;
            XmlNode node = null;
            //XmlNode items = null;
            //System.Xml.XmlNode _n = null;

            MyLists.Lists listService = new MyLists.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            ////v3
            //listService.Url = "https://terminator:30030/s1/_vti_bin/Lists.asmx";

            //v2
            listService.Url = "https://terminator:2004/sites/t2/_vti_bin/Lists.asmx";

            string listName = "Customer";
            ////v3
            //string listServiceURL = "https://terminator:30030/s1/_vti_bin/Lists.asmx";

            //v2
            string listServiceURL = "https://terminator:2004/sites/t2/_vti_bin/Lists.asmx";

            if (listServiceURL.Length > 0)
                listService.Url = listServiceURL;
            try
            {
                if (strRowID == "")
                {
                    node = listService.UpdateListItems(
                    listName,
                    updates);
                    XmlDocument rtnListItems = new XmlDocument();
                    rtnListItems.LoadXml(node.OuterXml);
                    XmlNamespaceManager nsmListItems = new XmlNamespaceManager(rtnListItems.NameTable);
                    nsmListItems.AddNamespace("foo", "https://schemas.microsoft.com/sharepoint/soap/");
                    XmlNode ndTemp = rtnListItems.SelectSingleNode("//*/*/*/@ows_ID", nsmListItems);
                    if (ndTemp != null)
                    {
                        strRowID = ndTemp.InnerText;
                    }
                    else
                        throw new Exception("The row ID could not be determined after the update of the list!");
                }
                System.IO.FileStream fStream =
                System.IO.File.OpenRead(documentPath);
                string fileName = System.IO.Path.GetFileName(fStream.Name);//fStream.Name.Substring(42);
                byte[] contents = new byte[fStream.Length];
                fStream.Read(contents, 0, (int)fStream.Length);
                fStream.Close();
                try
                {
                    string addAttach =
                    listService.AddAttachment(listName, strRowID, fileName, contents);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message.ToString());
            }

            MessageBox.Show("Done!");
        }

    }
}