How to Display SharePoint list item by selecting id of the item by using list web services (lists.asmx).
1 Answer
You can use below code.
Change the ID in CAML query according to your needs. I am using get me item whose ID=1
public void getListData()
{
WS_Lists.Lists myservice = new WS_Lists.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://site/_vti_bin/Lists.asmx";
try
{
/* Assign values to pass the GetListItems method*/
string listName = "ListName";
string viewName = "ViewName";
string rowLimit = "1";
// 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");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
/*Use CAML query*/
query.InnerXml = "<Where><eq><FieldRef Name=\"ID\" />" +
"<Value Type=\"Counter\">1</Value></eq></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";
System.Xml.XmlNode nodes = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);
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")
{
Response.Write(node.ChildNodes[i].Attributes["ows_Title"].Value + "</br>");
}
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
-
Please mark it as answer if it's working. And thanks is nothing without an UpVote :)Varun Verma– Varun Verma2014-07-10 10:53:11 +00:00Commented Jul 10, 2014 at 10:53