Sharepoint 2007/ MOSS 2007 - Getting/ Reverting data stored in list using web services
This program demonstrates about how to retrieve data from lists at sharepoint server into your website using lists web service .This Demonstration consists of two parts .In first part we create a list in sharepoint site and add items in that list. In second part we create a simple web site and this web site retrieve list data from share point server using Lists web service.
PART I
Steps
1 . At sharepoint site create a list named product with folloing fields
Figure 1
Steps for creating sharepoint list are:
1.1 Click on Site Actions on top right corner of Figure 1.
1.2 Click on Create option.
1.3 Select custom List under Custom Lists option.
1.4 For adding your own columns .Click on Settings option in Figure1 and click on Create Column option in Column.
2. Adding items in list
Click on New option in Figure1 and select Additems.
PART II
Steps
1.Create a website in visual studio with Default.aspx design as shown: This web site will be accessing the product list data in following manner.
Figure 2
This web site will be accessing the product list data in following manner. Upon selecting the category, subcategory will automatically be updated and the corresponding product details will be displayed in products table.Ctegory ,Subcategory and products detail table data is stored in list created in sharepoint webserver.
2.Right click in solution bar and select Add Web Reference option as shown:
Figure 3
This is the way to access lists web service at sharepoint server.In URL textbox type
i.e http://sereverName/sites/siteName/_vti_bin/lists.asmx?WSDL as shown in Figure 4
Figure 4
After successfull adding reference Solution Explorer will look as shown in Figure 5.
Figure 5
2. Code for the Program
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Net;
using System.Web.UI.HtmlControls;
using System.Xml;
using win_kqs5crbcani; // NOTE :Web Service name
public partial class _Default : System.Web.UI.Page
{
private const string url = "http://win-kqs5crbcani/sites/Eknoor";
private Lists proxy;
protected void Page_Load(object sender, EventArgs e)
{
proxy = new Lists();
proxy.Url = url + "/_vti_bin/lists.asmx";
proxy.Credentials = CredentialCache.DefaultCredentials;
if (!Page.IsPostBack)
PopulateCategories();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{ }
protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
PopulateSubCategories();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
PopulateProducts();
}
private void PopulateCategories()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<ViewFields><FieldRef Name='Category' /><FieldRef Name='Title' /></ViewFields>");
XmlNode results =proxy.GetListItems("product",null,null,doc.DocumentElement,null,null,null);
XmlNodeReader nr = new XmlNodeReader(results);
DataSet ds = new DataSet("resultdataSet");
ds.ReadXml(nr);
nr.Close();
DataView dv = new DataView(ds.Tables[1]);
DataTable dt = dv.ToTable(true,"ows_category");
DropDownList1.DataTextField = "ows_category";
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
PopulateSubCategories();
}
private void PopulateSubCategories()
{
string category = DropDownList1.SelectedItem.Text;
XmlDocument fieldsDoc = new XmlDocument();
fieldsDoc.LoadXml("<ViewFields><FieldRef Name='SubCategory' /><FieldRef Name='Title' /></ViewFields>");
string query = "<Query><Where><Eq><FieldRef Name = 'Category'/><Value Type = 'Text'>{0}</Value></Eq></Where></Query>";
query = string.Format(query, category);
XmlDocument queryDoc = new XmlDocument();
queryDoc.LoadXml(query);
XmlNode result = proxy.GetListItems("product", null,queryDoc.DocumentElement,fieldsDoc.DocumentElement,null, null, null);
XmlNodeReader nr = new XmlNodeReader(result);
DataSet ds = new DataSet("result");
ds.ReadXml(nr);
nr.Close();
DataView dv = new DataView(ds.Tables[1]);
DataTable dt = dv.ToTable(true, "ows_subcategory");
DropDownList2.DataTextField = "ows_subcategory";
DropDownList2.DataSource = dt;
DropDownList2.DataBind();
PopulateProducts();
}
private void PopulateProducts()
{
string category = DropDownList1.SelectedItem.Text;
string subcategory = DropDownList2.SelectedItem.Text;
XmlDocument fieldsDoc = new XmlDocument();
fieldsDoc.LoadXml("<ViewFields><FieldRef Name='ProductName' /><FieldRef Name='ProductNumber' /></ViewFields>");
string query = "<Query><Where><And><Eq><FieldRef Name='Category'/><Value Type='Text'>{0}</Value></Eq><Eq><FieldRef Name='SubCategory'/><Value Type='Text'>{1}</Value></Eq></And></Where></Query>";
query = string.Format(query,category, subcategory);
XmlDocument queryDoc = new XmlDocument();
queryDoc.LoadXml(query);
XmlNode result = proxy.GetListItems("product", null, queryDoc.DocumentElement, fieldsDoc.DocumentElement, null, null, null);
XmlNodeReader nr = new XmlNodeReader(result);
DataSet ds = new DataSet("resultdataSet");
ds.ReadXml(nr);
nr.Close();
DataView dv = new DataView(ds.Tables[1]);
DataTable dt = dv.ToTable(true, new string[] { "ows_ProductName"});
Session["TblData"] = ds.Tables[1];
GridView1.DataSource = ds.Tables[1].DefaultView;
GridView1.DataBind();
}
}
POINTS TO NOTE
1.Data is retrieved by using CAML query.
2.Web Service name extension is asmx. (Common mistakes done by beginners)
3. asmx files are available from http://siteUrl/ _vti_bin ,_vti_bin is a virtual directory pointing to ..\12\ISAPI
-Happy Coding
Parminder Josan





No comments:
Post a Comment