Saturday, January 1, 2011

HTTP Request to get the data from the sharepoint list using jQuery.

HTTP Request to get the data from the sharepoint list using javascript/jQuery.

Create a aspx page and place under the layouts folder and name it getcommentsfromlist.aspx

Add following code on OnLoad Method
public void OnLoad()
{
SPSite site = new SPSite(Request.QueryString["URL"]); //Site URL
SPWeb web = site.OpenWeb();

SPList list = web.Lists["Comments"]; //List in which Comments will be stored
String qPageId = Request.QueryString["PageID"]; //Page ID

SPQuery query = new SPQuery();
query.Query = "" + qPageId.ToString() + "";
SPListItemCollection itemsReturnedFromQuery = list.GetItems(query);


int totalPages;

query = new SPQuery();
query.Query = "" + qPageId.ToString() + "";


foreach (SPListItem item in itemsReturnedFromQuery)
{

retData += "Author : " + item["Author"].ToString() + "Date : " + item["Created"].ToString() + "Author : " + item["Comment"].ToString()
}


RenderHtmlControls(retData);
}


protected void RenderHtmlControls(string html)
{
Response.Write(html.ToString());
Response.End();//can be removed if you want to get the other static data which sharepoint places on the page
}


We can call the above aspx page using following JQuery code to get the data

var htmlData = $.ajax({
url: "/_layouts/getcommentsfromlist.aspx?URL=" + sCommentsURL + "&PageID=" + hwPageID,
async: false
}).responseText;

No comments:

Post a Comment