Wednesday, December 12, 2012

SharePoint List WebService to Insert Item

SharePoint List WebService to Insert Item

Using the SharePoint List WebService to insert an item into a list...
WebService Reference: http://<servername>/_vti_bin/Lists.asmx
try
{
 ws_list.Lists list = new ws_list.Lists();
 list.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
 XmlDocument doc = new XmlDocument();
 XmlElement batch_element = doc.CreateElement("Batch");
 string item = "<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"ID\">New</Field>" + "<Field Name=\"Title\">This is a test</Field>" + "</Method>";
 batch_element.InnerXml = item;
 list.UpdateListItems("ListTest", batch_element);
}
catch(Exception ex)
{
string err = ex.Message;
}

Another Example
            try
            {

                /*Declare and initialize a variable for the Lists Web service.*/
                siteWebServiceLists.Lists listService = new siteWebServiceLists.Lists();

                /*Authenticate the current user by passing their default credentials to the Web service from the system credential cache.*/
                listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                listService.Url = url + "/_vti_bin/Lists.asmx";
                System.Xml.XmlNode ndListView = listService.GetListAndView(list, "");
               
                /*Create an XmlDocument object and construct a Batch element and its attributes. Note that an empty ViewName parameter causes the method to use the default view. */
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
               
                batchElement.SetAttribute("OnError", "Continue");
                batchElement.SetAttribute("ListVersion", "1");
                batchElement.InnerXml = "<Method ID='1' Cmd='New'>" +
                "<Field Name='Title'>Title</Field>" +
                "<Field Name='First_x0020_Name'>Wayne</Field>" +
                "<Field Name='Last_x0020_Name'>Magnum</Field>" +
                "</Method>";

                /*Update list items. This example uses the list GUID, which is recommended, but the list display name will also work.*/
                System.Xml.XmlNode n = listService.UpdateListItems(list, batchElement);
                XmlTextReader xr = new XmlTextReader(n.OuterXml, XmlNodeType.Element, null);
                while (xr.Read())
                {
                    if (xr.ReadToFollowing("z:row"))
                    {
                        if (xr["ows_ID"] != null)
                        {
                            Console.WriteLine(xr["ows_ID"].ToString());
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }


You can get filtered list items using Lists.GetListItems method using SharePoint 2010′s Lists.asmx webservice.
Lets look at a simple example :
In this example we will get items where a Number field contains a value greater than 5,000.  Also, I am using uses an XmlDocument object to create XmlNode objects for parameters.
//Add reference to the web service in your project.
Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element,”Query”,”");
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element,”ViewFields”,”");
XmlNode ndQueryOptions =
xmlDoc.CreateNode(XmlNodeType.Element,”QueryOptions”,”");
ndQueryOptions.InnerXml =
“<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>” +”<DateInUtc>TRUE</DateInUtc>”;
ndViewFields.InnerXml = “<FieldRef Name=’Field1′ /><FieldRef Name=’Field2′/>”;
ndQuery.InnerXml = “<Where><Gt><FieldRef Name=’Field1′/>” + “<Value Type=’Number’>5000</Value></Gt></Where>”;
try
{
XmlNode ndListItems = listService.GetListItems(“List_Name”, null, ndQuery, ndViewFields, null, ndQueryOptions, null);
MessageBox.Show(ndListItems.OuterXml);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
MessageBox.Show(“Message:\n” + ex.Message + “\nDetail:\n” + ex.Detail.InnerText + “\nStackTrace:\n” + ex.StackTrace);
}
 

No comments:

Post a Comment