I’ going to discuss how we can Retrive, ADD, DELETE, UPDATE data from a SharePoint List using its Web Services. First to know about SharePoint Web Services please refer this. We can user List Web Service which provides methods for working with SharePoint Lists, Content Types, List Items, and Files to read a List. Here we are going to use GetListItems method for Retriving List Items and use UpdateListItems method for ADD, UPDATE, DELETE List Items.
To use the above method we should know the GUIDs of the target list and view.
This is how you can Add Web Reference; I’ll get created Web Site Project in Visual studio to illustrate. Actually it is simple, first click on Add Web Reference and give the URL to the Web Service.
Screen I:
Screen 2:
I. SharePoint List Web Service GetListItems
This is a sample code to read SharePoint list,
protected void Retrive_Click(object sender, EventArgs e)
{
WS_Lists.Lists myservice = new WS_Lists.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://win-bg3773518li:17350/_vti_bin/Lists.asmx";
try
{
/* Assign values to pass the GetListItems method*/
string listName = "{C509686B-EE32-4297-A7EF-6C85FA841910}";
// string viewName = "{75E689B4-5773-43CB-8324-58E42E1EB885}";
string rowLimit = "100";
// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
/*Use CAML query*/
query.InnerXml = "<Query><FieldRef Name=\"ID\" />" +
"</Query>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" /><FieldRef Name=\"StudName\" />";
System.Xml.XmlNode nodes = myservice.GetListItems(listName, string.Empty, query, viewFields, rowLimit, null, null);
Response.Write(" Title StudName");
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Name == "rs:data")
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name == "z:row")
{
string strName = "";
if (node.ChildNodes[i].Attributes["ows_StudName"] != null)
strName = node.ChildNodes[i].Attributes["ows_StudName"].Value;
Response.Write(node.ChildNodes[i].Attributes["ows_Title"].Value + " " + strName + "</br>");
}
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
II. SharePoint List Web Service UpdateListItems
This is a sample code to ADD Items into SharePoint list,
protected void Add_Click(object sender, EventArgs e)
{
WS_Lists.Lists list = new WS_Lists.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string strTitle = " New Added Title" + DateTime.Now.ToShortDateString();
string item = "<Method ID=\"101\" Cmd=\"New\">" + "<Field Name=\"ID\">New</Field>" + "<Field Name=\"Title\">" + strTitle + "</Field>" + "</Method>";
batch_element.InnerXml = item;
list.UpdateListItems("List2", batch_element);
}
II. SharePoint List Web Service UpdateListItems
This is a sample code to UPDATE Item of SharePoint list,
protected void Update_Click(object sender, EventArgs e)
{
string ItemID="7";
WS_Lists.Lists list = new WS_Lists.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string strTitle ="Updated Title" + DateTime.Now.ToShortDateString();
string item = "<Method ID=\"1\" Cmd=\"Update\">" + "<Field Name=\"ID\">"+ItemID+"</Field>" + "<Field Name=\"Title\">" + strTitle + "</Field>" + "</Method>";
batch_element.InnerXml = item;
list.UpdateListItems("List2", batch_element);
}
II. SharePoint List Web Service UpdateListItems
This is a sample code to DELETE Item of SharePoint list,
protected void Delete_Click(object sender, EventArgs e)
{
string ItemID = "7";
WS_Lists.Lists list = new WS_Lists.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string item = "<Method ID=\"1\" Cmd=\"Delete\">" + "<Field Name=\"ID\">"+ItemID+"</Field>" + "</Method>";
batch_element.InnerXml = item;
list.UpdateListItems("List2", batch_element);
}
To use the above method we should know the GUIDs of the target list and view.
This is how you can Add Web Reference; I’ll get created Web Site Project in Visual studio to illustrate. Actually it is simple, first click on Add Web Reference and give the URL to the Web Service.
Screen I:
Screen 2:
I. SharePoint List Web Service GetListItems
This is a sample code to read SharePoint list,
protected void Retrive_Click(object sender, EventArgs e)
{
WS_Lists.Lists myservice = new WS_Lists.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://win-bg3773518li:17350/_vti_bin/Lists.asmx";
try
{
/* Assign values to pass the GetListItems method*/
string listName = "{C509686B-EE32-4297-A7EF-6C85FA841910}";
// string viewName = "{75E689B4-5773-43CB-8324-58E42E1EB885}";
string rowLimit = "100";
// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
/*Use CAML query*/
query.InnerXml = "<Query><FieldRef Name=\"ID\" />" +
"</Query>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" /><FieldRef Name=\"StudName\" />";
System.Xml.XmlNode nodes = myservice.GetListItems(listName, string.Empty, query, viewFields, rowLimit, null, null);
Response.Write(" Title StudName");
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Name == "rs:data")
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name == "z:row")
{
string strName = "";
if (node.ChildNodes[i].Attributes["ows_StudName"] != null)
strName = node.ChildNodes[i].Attributes["ows_StudName"].Value;
Response.Write(node.ChildNodes[i].Attributes["ows_Title"].Value + " " + strName + "</br>");
}
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
II. SharePoint List Web Service UpdateListItems
This is a sample code to ADD Items into SharePoint list,
protected void Add_Click(object sender, EventArgs e)
{
WS_Lists.Lists list = new WS_Lists.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string strTitle = " New Added Title" + DateTime.Now.ToShortDateString();
string item = "<Method ID=\"101\" Cmd=\"New\">" + "<Field Name=\"ID\">New</Field>" + "<Field Name=\"Title\">" + strTitle + "</Field>" + "</Method>";
batch_element.InnerXml = item;
list.UpdateListItems("List2", batch_element);
}
II. SharePoint List Web Service UpdateListItems
This is a sample code to UPDATE Item of SharePoint list,
protected void Update_Click(object sender, EventArgs e)
{
string ItemID="7";
WS_Lists.Lists list = new WS_Lists.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string strTitle ="Updated Title" + DateTime.Now.ToShortDateString();
string item = "<Method ID=\"1\" Cmd=\"Update\">" + "<Field Name=\"ID\">"+ItemID+"</Field>" + "<Field Name=\"Title\">" + strTitle + "</Field>" + "</Method>";
batch_element.InnerXml = item;
list.UpdateListItems("List2", batch_element);
}
II. SharePoint List Web Service UpdateListItems
This is a sample code to DELETE Item of SharePoint list,
protected void Delete_Click(object sender, EventArgs e)
{
string ItemID = "7";
WS_Lists.Lists list = new WS_Lists.Lists();
list.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument doc = new XmlDocument();
XmlElement batch_element = doc.CreateElement("Batch");
string item = "<Method ID=\"1\" Cmd=\"Delete\">" + "<Field Name=\"ID\">"+ItemID+"</Field>" + "</Method>";
batch_element.InnerXml = item;
list.UpdateListItems("List2", batch_element);
}