3

I want to add new item to SharePoint list using web service. I use SharePoint 2013. I found on the net follow code, but I don't understand how to use?

<Batch OnError="Continue">
<Method ID="1" Cmd="New">
    <Field Name="Title">Abcdef</Field>
    <Field Name="Project_x0020_code">999050</Field>
    <Field Name="Status">Open</Field>    
</Method>

Where I write this code, where I publish? Is there any other way?

Source of the code: SharePoint - How do insert new items using the list web service?

1

Try using below code by passing ViewName,

<Batch OnError="Continue" ViewName="{D0E978D3-4D39-4CBE-ACEC-BCFB22344252}">
    <Method ID='1' Cmd='New'>
        <Field Name='Title'>The New Task Title</Field>
     </Method>
</Batch>

In another way we can create Web Site Project in Visual studio to illustrate. For that click on Add Web Reference and give the URL to the Web Service,

Its Demonstrated here,

http://sarangasl.blogspot.in/2009/12/sharepoint-list-web-service.html

http://sarangasl.blogspot.in/2009/12/addupdate-list-item-using-sharepoint.html

  • "Where I write this code, where I publish? Is there any other way?" is the question here – Robert Lindgren Nov 9 '15 at 8:23
  • Yes, where I write this code, where I running this code? In a VS SharePoint Project, or xml file? I don't know this – zengel Nov 9 '15 at 8:27
  • Yes yes I got to know about your question, here is the link that provides information about it hope this helps, "c-sharpcorner.com/Blogs/7655/…" – Niranjan Kulkarni Nov 9 '15 at 8:34
0

The XML in your question is passed as an argument to the UpdateListItems method. Below is an item update example from msdn: You will need to use this code in a method in your Visual studio project that will add a new list item. Update the strBatch string to your requirements.

I would suggest to look at new ways of working with SharePoint Lists using the following references: Using CSOM and Using REST

Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

string strBatch = "<Method ID='1' Cmd='Update'>" + 
    "<Field Name='ID'>4</Field>" +
    "<Field Name='Field_Number'>999</Field></Method>" +
    "<Method ID='2' Cmd='Update'><Field Name='ID' >6</Field>" +
    "<Field Name='Field_DateTime'>
        2003-11-11T09:15:30Z</Field></Method>"; 

XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError","Continue");
elBatch.SetAttribute("ListVersion","1");
elBatch.SetAttribute("ViewName",
    "0d7fcacd-1d7c-45bc-bcfc-6d7f7d2eeb40");

elBatch.InnerXml = strBatch;

XmlNode ndReturn = listService.UpdateListItems("List_Name", elBatch);

MessageBox.Show(ndReturn.OuterXml);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.