Saturday, July 4, 2009

Merging multiple SPList and binding to SPGridView

Scenario: Many instances it so happens that we have to read multiple list and merge them together and then bind the merge list to SPGridView. Here is one example of how to implement it. This is a vanilla code which works great. But its at your discretion that you can further simplify and refactor it.

Implentation:

I used 2 classes here. Again use your own ideas how you want to make it more better.
Before we start, I created 2 custom list (1 with name 'Srini Test' and other with 'New List') with columns added as 'URL' and 'Description' along with 'Title' (default OOB).

Util Class:
---------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using Microsoft.SharePoint;
namespace BlogIt
{
public class Util
{
public DataTable GetDataTableFromList(string site, string web, string listname)
{
DataTable dt = null;
using(SPSite oSPSite = new SPSite(site))
{
using(SPWeb oSPWeb = oSPSite.OpenWeb())
{
SPList oList = (SPList)oSPWeb.Lists[listname];
dt = oList.Items.GetDataTable();
}
}
return dt;
}
}
}

---------------------------------------------------------------------------
Webpart Class:
---------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
namespace BlogIt
{
public partial class _Default : System.Web.UI.Page
{
Util oUtil = new Util();
SPGridView oSPGridView = new SPGridView();
protected override void OnInit(EventArgs e)
{
oSPGridView.ID = "_spgv1";
oSPGridView.AutoGenerateColumns = false;
BuildGridColumns();
base.OnInit(e);
}
private void BuildGridColumns()
{
BoundField bfTitle = new BoundField();
bfTitle.HeaderText = "Title";
bfTitle.DataField = "Title";
oSPGridView.Columns.Add(bfTitle);
BoundField bfURL = new BoundField();
bfURL.HeaderText = "URL";
bfURL.DataField = "URL";
oSPGridView.Columns.Add(bfURL);
}
protected override void CreateChildControls()
{
this.Controls.Add(oSPGridView);
base.CreateChildControls();
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable oDt1 = oUtil.GetDataTableFromList("http://localhost:81/", string.Empty, "Srini Test");
DataTable oDt2 = oUtil.GetDataTableFromList("http://localhost:81/", string.Empty, "New List");
oDt1.Merge(oDt2);
oSPGridView.DataSource = oDt1;
oSPGridView.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{

}
}
}

---------------------------------------------------------------------------

2 comments:

Sandeep K Nahta said...

Its obvious but as a disclaimer : this will only work for any link list only. Also in case you have MOSS , Content Query web part is good candidate for doing this.

Srini Sistla said...

Oh yes, the intention as mentioned on the title is just for List alone. Regarding CQWP, I completely agree too. This one is just a programmatic approach.