How to add new item to sharepoint list using web services in C sharp

I am trying to add a new item to a sharepoint list from a winform application in C # using web services. As a result, I get the useless exception "An exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."

I have a web link named WebSrvRef at http: //server/site/subsite/_vti_bin/Lists.asmx

And this code:

        XmlDocument xmlDoc;
        XmlElement elBatch;
        XmlNode ndReturn;
        string[] sValues;
        string sListGUID;
        string sViewGUID;

        if (lstResults.Items.Count < 1)
        {
            MessageBox.Show("Unable to Add To SharePoint\n" +
                "No test file processed. The list is blank.", 
                "Add To SharePoint", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        WebSrvRef.Lists listService = new WebSrvRef.Lists();
        sViewGUID = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"; // Test List View GUID
        sListGUID = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"; // Test List GUID

        listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

        frmAddToSharePoint dlgAddSharePoint = new frmAddToSharePoint();
        if (dlgAddSharePoint.ShowDialog() == DialogResult.Cancel)
        {
            dlgAddSharePoint.Dispose();
            listService.Dispose();
            return;
        }

        sValues = dlgAddSharePoint.Tag.ToString().Split('~');
        dlgAddSharePoint.Dispose();

        string strBatch = "<Method ID='1' Cmd='New'>" +
            "<Field Name='Client#'>" + sValues[0] + "</Field>" +
            "<Field Name='Company'>" + sValues[1] + "</Field>" +
            "<Field Name='Contact Name'>" + sValues[2] + "</Field>" +
            "<Field Name='Phone Number'>" + sValues[3] + "</Field>" +
            "<Field Name='Brand'>" + sValues[4] + "</Field>" +
            "<Field Name='Model'>" + sValues[5] + "</Field>" +
            "<Field Name='DPI'>" + sValues[6] + "</Field>" +
            "<Field Name='Color'>" + sValues[7] + "</Field>" +
            "<Field Name='Compression'>" + sValues[8] + "</Field>" +
            "<Field Name='Value % 1'>" + (((float)lstResults.Groups["Value 1"].Tag)*100).ToString("##0.00") + "</Field>" +
            "<Field Name='Value % 2'>" + (((float)lstResults.Groups["Value 2"].Tag)*100).ToString("##0.00") + "</Field>" +
            "<Field Name='Value % 3'>" + (((float)lstResults.Groups["Value 3"].Tag)*100).ToString("##0.00") + "</Field>" +
            "<Field Name='Value % 4'>" + (((float)lstResults.Groups["Value 4"].Tag)*100).ToString("##0.00") + "</Field>" +
            "<Field Name='Value % 5'>" + (((float)lstResults.Groups["Value 5"].Tag)*100).ToString("##0.00") + "</Field>" +
            "<Field Name='Comments'></Field>" +
            "<Field Name='Overall'>" + (fTotalScore*100).ToString("##0.00") + "</Field>" +
            "<Field Name='Average'>" + (fTotalAvg * 100).ToString("##0.00") + "</Field>" +
            "<Field Name='Transfered'>" + sValues[9] + "</Field>" +
            "<Field Name='Notes'>" + sValues[10] + "</Field>" +
            "<Field Name='Resolved'>" + sValues[11] + "</Field>" +
            "</Method>";

        try
        {
            xmlDoc = new System.Xml.XmlDocument();
            elBatch = xmlDoc.CreateElement("Batch");

            elBatch.SetAttribute("OnError", "Continue");
            elBatch.SetAttribute("ListVersion", "1");
            elBatch.SetAttribute("ViewName", sViewGUID);

            strBatch = strBatch.Replace("&", "&amp;");
            elBatch.InnerXml = strBatch;

            ndReturn = listService.UpdateListItems(sListGUID, elBatch);

            MessageBox.Show(ndReturn.OuterXml);
            listService.Dispose();                
        }
        catch(Exception Ex)
        {
            MessageBox.Show(Ex.Message +
                "\n\nSource\n" + Ex.Source +
                "\n\nTargetSite\n" + Ex.TargetSite +
                "\n\nStackTrace\n" + Ex.StackTrace, 
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            listService.Dispose();
        }

      

What am I doing wrong? What am I missing? Please, help!

Franc

+2


a source to share


4 answers


OK, now it works. Here's what I went wrong:

I didn't use FieldInternalName -> Thanks to Robert Williams



I was missing this line too, because even though my web reference points to the correct location, my listService.List still points to the server root site.

listService.Url = @"http://server/site/subsite/_vti_bin/Lists.asmx";

      

+1


a source


Most likely because of this line:

listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

      

Change it to userid / password set like this:



listService.Credentials = new NetworkCredential("UserID", "Password");

      

Make sure the user password / Password has access to create lists, etc.

+1


a source


If NeworkCredential is not the answer, you also need to be sure to use the correct field names.

Field Name must be FieldInternalName for SharePoint list fields. To get the FieldInternalName for this field, in SharePoint, select New from the list and then View Source of this page (right-click anywhere on the New page). You will see the fields along with their internal names towards the end of the source file.

+1


a source


The soap exception will contain node information with a more useful description of the error. Catch it in the debugger and be in it.

0


a source







All Articles