Tuesday, January 3, 2012

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://agmsm:9999/sites/mnet/venkat/_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=\"ID\" /><FieldRef Name=\"Title\" /><FieldRef Name=\"Status\" /><FieldRef Name=\"Priority\" /><FieldRef Name=\"AssignedTo\" />";
        
        query.InnerXml = "<OrderBy><FieldRef Name='ID' Ascending='False' /></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("<Table width='100%'>");
        for (int i = 0; i < 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("<Tr>");
            Response.Write("<Td>");
            Response.Write(strID);
            Response.Write("</Td>");
            Response.Write("<Td>");
            Response.Write(strTitle);
            Response.Write("</Td>");
            if (strStatus == "Not Started")
            {
                Response.Write("<Td bgcolor='Red'>");
                
            }
            else if (strStatus == "Completed")
            {
                Response.Write("<Td bgcolor='Green'>");
            }
            else if (strStatus == "In Progress")
            {
                Response.Write("<Td bgcolor='Orange'>");
            }
            else if (strStatus == "Deferred")
            {
                Response.Write("<Td bgcolor='Pink'>");
            }
            Response.Write("<Strong>");
            Response.Write("<Font Color='White'>");
            Response.Write(strStatus);
            Response.Write("</Font>");
            Response.Write("</Strong>");
            Response.Write("</Td>");
            Response.Write("<Td>");
            Response.Write(strPriority);
            Response.Write("</Td>");
            Response.Write("<Td>");
            Response.Write(strAssignedTo);
            Response.Write("</Td>");
            Response.Write("</Tr>");
            Response.Write("<Tr>");
            Response.Write("<Td bgcolor='blue' colspan='5'>");
            Response.Write("</Td>");
            Response.Write("</Tr>");

        }
        Response.Write("</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