3

I want to develop a custom web part for SharePoint 2007. How do I display the results of a CAML query on a data grid? The grid view should be customizable, and should not adversely affect performance.

2
  • Do you mean a using a custom field type, or an entirely custom data view web part? Commented May 16, 2011 at 17:28
  • entire data view webpart. Commented May 17, 2011 at 0:07

4 Answers 4

2

Haven't had a chance to test this code yet, but this here is the basic idea.

public static void TestOneFuncOne() { using (SPSite site = new SPSite("yoursiteurl")) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["Tasks"]; DataTable dt; SPQuery query = new SPQuery(); query.query = "Your CAML Query Here"; dt = list.GetItems(query).GetDataTable(); YourGridview.DataSource = dt; YourGridview.DataBind(); } } } 

Does it sufficiently answer your question Or you were looking for more information in terms of best practices to handle a large list etc?

2
  • i have a doubt, does the method "getdatatable" affect the page rendering time, anyhow the list will be having only 1000 items and the query will fetch only 5 items. Commented May 17, 2011 at 0:14
  • It really depends. Not sure if your question is if GetDataTable(); adds more processing time when compared to just executing the query and returning a SPListItemCollection. If so yes, but then you still have to manipulate the returned SPListItemCollection to present the content (render time). So I don't think it's really worse than any other method. Commented May 17, 2011 at 6:26
3

There are many different options/combinations to consider. The method you choose will probably depend on your requirements.

Get the data from SharePoint:

  • ObjectDataSource - The more general way to retrieve data. You would implement your own select method to return results from your CAML query.
  • SPDataSource - The "sharepointized" datasource. The link actually has a code sample using SPDataSource with a regular GridView.

Then, you can simply hook one of those up to a GridView or SPGridView. I can recommend a very good tutorial using an ObjectDataSource with SPGridView: Building A SPGridView Control – Part 1: Introducing the SPGridView.

2

As Stuart Pegg has alluded to, you should probably look at using DataViewWebPart for this. When you say customizable, I am assuming you mean customizable through SharePoint designer, which DVWP is.

You can put the CAML query in either through the code in the page that you are inserting the DVWP into, or you can create it using the GUI tools that SPD provides you.

0

Link : How to display data from list in applciation page using SPGridview

private void BindGrid() { try {using (SPSite site = new SPSite(SPContext.Current.Site.Url))

 using (SPWeb web = site.OpenWeb()) { //Creation of data table table = new DataTable(); table.Columns.Add("INDEX", typeof(Int32)); table.Columns.Add("ENTRY", typeof(string)); string dayOfWeek = DateTime.Now.DayOfWeek.ToString(); SPList list = site.RootWeb.Lists["Cafeteria"]; //Query to get the Entress items with Price big SPQuery query = new SPQuery(); query.Query = "<Where><And><Eq><FieldRef Name='Day' /><Value Type='Choice'>" + dayOfWeek + "</Value></Eq><Eq><FieldRef Name='Type' /><Value Type='Choice'>etr</Value></Eq></And></Where><OrderBy><FieldRef Name='PrBig' Ascending='False' /></OrderBy>"; SPListItemCollection coll = list.GetItems(query); GetDetails(web, coll); 

gvCafe.DataSource = table; gvCafe.DataBind(); private void GetDetails(SPWeb web, SPListItemCollection coll) { if (coll.Count > 0) { row = table.Rows.Add(); //SetDefaultImage(web); row["INDEX"] = 1; row["ENTRY"] = SPUtility.ConcatUrls(coll[0].Web.Url, coll[0].Url); } }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.