Load all the List Items of a List into a data table in share point 2010 using a web part
Step 1:- Go to File-> New->Project->SharePoint-> Empty SharePoint Project
Once a new Empty Share point project is created add a web part
Step2:- Right Click on the project -> Add New Item->Web Part
The web part will be added to the project.
We need to override the “CreateChildControls()” of the web part and add our controls into that. Let’s add a button control into the function.
protected Button btnLoad;
protected override void CreateChildControls()
{
base.CreateChildControls();
btnLoad = new Button();
btnLoad.Text = "Load List";
btnLoad.Click += LoadDataTable;
this.Controls.Add(btnLoad);
}
In the button click event handler we will add the code to download the share point list Items of a particular list.
public void LoadDataTable(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SPList myList = null;
SPSite site = null;
SPWeb web = null;
SPList list = null;
try
{
site = SPContext.Current.Site;
{
web = site.RootWeb;
list = web.GetList("/Lists/ListName");
myList = list;
dt = myList.Items.GetDataTable();
}
}
catch (Exception ex)
{
//Handle Exception
}
}
}
This approach is good if we want to download with in the sharepoint server if we want to download it with in some other web or windows application then we need to add the list service and download.The example for the same has been discussed in one of my blogs.
No comments:
Post a Comment