Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Binding Data From SharePoint List to Gridview using SharePoint Default WebService.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Prasad;
using System.Xml;

public partial class Default6 : System.Web.UI.Page
{
DataSet dset = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
Prasad.Lists listService = new Prasad.Lists();
listService.UseDefaultCredentials=true;
listService.Url = "http://vlnrm:100/_vti_bin/Lists.asmx";
//string strListName = "{296A9F52-5A48-4CB9-9294-47F93DF2F85F}";
string strListName = "Tasks";
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");
viewFields.InnerXml = "<fieldref name="\&quot;ID\&quot;"><fieldref name="\&quot;Title\&quot;"><fieldref name="\&quot;Status\&quot;"><fieldref name="\&quot;Priority\&quot;"><fieldref name="\&quot;AssignedTo\&quot;">";

query.InnerXml = "<orderby><fieldref ascending="False" name="ID"></fieldref></orderby>";
System.Xml.XmlNode nodeListItems = listService.GetListItems(strListName, null, query, viewFields, "5", null, null);
//System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
DataSet ds = getData(nodeListItems);
DataTable dt = ds.Tables["row"];
GridView1.DataSource = dt;
GridView1.DataBind();
System.Xml.XmlElement batchElement = xmlDoc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
Response.Write("");          for (int i = 0; i &lt; dt.Rows.Count; i++)         
{             
string strID = dt.Rows[i]["ows_ID"].ToString();             
string strTitle = dt.Rows[i]["ows_Title"].ToString();            
 string strStatus = dt.Rows[i]["ows_Status"].ToString();             
string strPriority = dt.Rows[i]["ows_Priority"].ToString();             

string strAssignedTo = dt.Rows[i]["ows_AssignedTo"].ToString();             
strAssignedTo = strAssignedTo.Substring(strAssignedTo.IndexOf('#')+1);             
Response.Write("");             
Response.Write("");             
Response.Write("");             
if (strStatus == "Not Started")             
{                 
Response.Write("");
Response.Write("");
Response.Write("");
Response.Write("
");
Response.Write("
");
Response.Write("");
Response.Write("
");

}
Response.Write("</fieldref></fieldref></fieldref></fieldref></fieldref>
<table><tbody>
<tr><td>");
Response.Write(strID);
Response.Write("</td><td>");
Response.Write(strTitle);
Response.Write("</td><td bgcolor="Red">");

}
else if (strStatus == "Completed")
{
Response.Write("</td><td bgcolor="Green">");
}
else if (strStatus == "In Progress")
{
Response.Write("</td><td bgcolor="Orange">");
}
else if (strStatus == "Deferred")
{
Response.Write("</td><td bgcolor="Pink">");
}
Response.Write("<b>");
Response.Write("<span style="color: white;">");
Response.Write(strStatus);
Response.Write("</span>");
Response.Write("</b>");
Response.Write("</td><td>");
Response.Write(strPriority);
Response.Write("</td><td>");
Response.Write(strAssignedTo);
Response.Write("</td></tr>
<tr><td bgcolor="blue" colspan="5">");
Response.Write("</td></tr>
</tbody></table>
");
try
{
//listService.UpdateListItems(listName, batchElement);
//XmlNode strNode = listService.UpdateListItems(strListName, batchElement);
//Response.Write("Response " + strNode.OuterXml);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Response.Write("Message:" + ex.Message);
}
}
public DataSet getData(XmlNode xmlnodeinput)
{
if (xmlnodeinput != null)
{
XmlTextReader xtr = new XmlTextReader(xmlnodeinput.OuterXml, XmlNodeType.Element, null);
//dataset = new DataSet();
dset.ReadXml(xtr);
}
return dset;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[3].Text = e.Row.Cells[3].Text.Substring(e.Row.Cells[3].Text.IndexOf('#') + 1);
}
}

No comments:

Post a Comment