Today I will show you how to consume and use the SharePoint list.asmx. Let us directly get to the code....
////Instance of the Webservice added the /_vti_bin/list.asmx
ListsWebServices.Lists listService = new ListsWebServices.Lists();
////Passing Credentails...
//listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.UseDefaultCredentials = true;
////Creating XMLDocumetn to create XmlNodes
XmlDocument xmlDoc = new System.Xml.XmlDocument();
////Creating XmlNodes
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element,"Query","");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element,"ViewFields","");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element,"QueryOptions","");
////Assigning Values to XmlNotes
ndQuery.InnerXml = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Partha123</Value></Eq></Where>";
ndViewFields.InnerXml = "<FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='Project Name'/><FieldRef Name='Project Manager'/>";
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>";
////List to access both name and Guid
string strListName = "My Project Details";
string strListNamGuid = "{E586B4DB-3C4C-CMPI-8C86-77E1857B5TMC}";
////start calling the list service
try
{
////Method to call is GetListItem and 7 parameters
//XmlNode ndListItems = listService.GetListItems("{E586B4DB-3C4C-CMPI-8C86-77E1857B5TMC}", null, ndQuery, ndViewFields, null, ndQueryOptions, null);
//for check
Response.Write("Hello World");
XmlNode ndListItems = listService.GetListItems(strListNameGuid, null, ndQuery, ndViewFields, null, ndQueryOptions, null); Response.Write(ndListItems.ToString());
}
catch (System.Web.Services.Protocols.SoapException ex)
{
string strMessage = ex.Message;
//MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);
Got few error while coding and correct them as well ---
1. could not find list by name, need to change it to GUID
2. //There is no Web named "/sites/ABC –
3. There is no Web named" error when using GetListItems against a site collection
Ref:
http://social.msdn.microsoft.com/Forums/eu/sharepointdevelopment/thread/59d52380-b6bb-4c21-89ad-f5d8c0857d64
http://sharepoint.stackexchange.com/questions/8338/sharepoint-webservice-get-list-item-navigation-url
Finally got the code to work and fetch data
////Instance of the Webservice added the /_vti_bin/list.asmx
ListsWebServices.Lists listService = new ListsWebServices.Lists();
////Passing Credentails...
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
//listService.UseDefaultCredentials = true;
////Set the webservice url to call
string strWebServiceURL = "http://partha004:7011/sites/MyProjectCentre/_vti_bin/lists.asmx";
listService.Url = strWebServiceURL;
////Creating XMLDocumetn to create XmlNodes
XmlDocument xmlDoc = new System.Xml.XmlDocument();
////Creating XmlNodes
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element,"Query","");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element,"ViewFields","");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element,"QueryOptions","");
////Assigning Values to XmlNotes
ndQuery.InnerXml = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'> Partha123</Value></Eq></Where>";
ndViewFields.InnerXml = "<FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='Project Name'/><FieldRef Name='Project Manager'/>";
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>";
////List to access both name and Guid
string strListName = "My Project Details";
string strListNameGuid = "{E586B4DB-3C4C-CMPI-8C86-77E1857B5TMC}";
////start calling the list service
try
{
Response.Write("Hello World");
////Setting ndViewFields is restricting the data fetch, null is rather returning all data.
XmlNode ndListItems = listService.GetListItems(strListNameGuid, null, ndQuery, null, null, ndQueryOptions, null);
Response.Write("<br/>");
Response.Write(ndListItems.OuterXml);
DataSet listDS = new DataSet();
XmlNodeReader oNodeReader = new XmlNodeReader(ndListItems);
listDS.ReadXml(oNodeReader);
if (listDS != null && listDS.Tables.Count > 0)
{
string strTitle = listDS.Tables["row"].Rows[0]["ows_Title"].ToString();
string strProjectName = listDS.Tables["row"].Rows[0]["ows_Project Name"].ToString();
string strProjectStatus = listDS.Tables["row"].Rows[0]["ows_Project Status"].ToString();
string strProjectManager = listDS.Tables["row"].Rows[0]["ows_Project Manager"].ToString();
Response.Write("Please see project details:<br/><br/>");
Response.Write("Project ID : "+strTitle+"<br/>");
Response.Write("Project Name : " + strProjectName + "<br/>");
Response.Write("Project Status : " + strProjectStatus + "<br/>");
Response.Write("Project Manager : " + strProjectManager + "<br/>");
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
string strMessage = ex.Message;
}
After couple of changes, finally satisfied with the below code block.
////Instance of the Webservice added the /_vti_bin/list.asmx
ListsWebServices.Lists listService = new ListsWebServices.Lists();
////Passing Credentails...
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
//listService.UseDefaultCredentials = true;
////Set the webservice url to call
string strWebServiceURL = "http://partha004:7011/sites/ProjectCentre/_vti_bin/lists.asmx";
listService.Url = strWebServiceURL;
////Creating XMLDocumetn to create XmlNodes
XmlDocument xmlDoc = new System.Xml.XmlDocument();
////Creating XmlNodes
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element,"Query","");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element,"ViewFields","");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element,"QueryOptions","");
////Assigning Values to XmlNotes
ndQuery.InnerXml = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Partha123</Value></Eq></Where>";
ndViewFields.InnerXml = "<FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='Project Name'/><FieldRef Name='Project Manager'/>";
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc>";
////List to access both name and Guid
string strListName = "My Project Details";
string strListNameGuid = "{E586B4DB-3C4C-CMPI-8C86-77E1857B5TMC}";
////start calling the list service
try
{
Response.Write("Hello World");
////Setting ndViewFields is restricting the data fetch, null is rather returning all data.
XmlNode ndListItems = listService.GetListItems(strListNameGuid, null, ndQuery, null, null, ndQueryOptions, null);
ndListItems.InnerXml = ndListItems.InnerXml.Replace("ows_", "");
DataSet listDS = new DataSet();
XmlNodeReader oNodeReader = new XmlNodeReader(ndListItems);
listDS.ReadXml(oNodeReader);
if (listDS != null && listDS.Tables.Count > 0)
{
string strTitle = listDS.Tables["row"].Rows[0]["Title"].ToString();
string strProjectName = listDS.Tables["row"].Rows[0]["Project Name"].ToString();
string strProjectStatus = listDS.Tables["row"].Rows[0]["Project Status"].ToString();
string strProjectManager = listDS.Tables["row"].Rows[0]["Project Manager"].ToString();
Response.Write("Please see project details:<br/><br/>");
Response.Write("Project ID : "+strTitle+"<br/>");
Response.Write("Project Name : " + strProjectName + "<br/>");
Response.Write("Project Status : " + strProjectStatus + "<br/>");
Response.Write("Project Manager : " + strProjectManager + "<br/>");
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
string strMessage = ex.Message;
}
Important: Need to add the service url on page. The web or app .config links to the root site.
If you have a more elegant solution – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg
Partha (Aurum)