There are many SharePoint web parts which ship as Out of the box with the SharePoint and one of those is the View web part. When you want to show the data in your lists and libraries there are many approaches including customizations but this can also be done by simply adding the view web parts on pages but this depends on requirements. A List or Library view web parts can be added directly in web part zones on pages and you are done to show the list or library data.
But I was more curious to take a look at how we can add this web parts programmatically? I searched on the internet (that's what the first thing we all doJ), and I got many posts to do this, some were really useful.
When we simply add the web part on the page through code behind using an object of class "ListViewWebPart" then this works very fine (once the web part properties are correctly initialized) but the problem is that when I was adding using this approach, the tool bar was not getting added correctly. So I again found one more approach by which we add the tool bar explicitly and then remove the already added tool bar with "ListViewWebPart".
So I have created a simple check box web browsable web part's property which allows us to select to show tool bar or not. If we don't choose to show the tool bar then web part simply renders the list view without tool bar, a link at the bottom of List view to add / upload documents will be added.
Here is the code.
public class GListView : WebPart
{
#region Prive Variables
private string _listName;
private Guid _siteID;
private Guid _webID;
private SPList _list;
private SPContext _context;
private ListViewWebPart _listViewWebPart;
private SPView _listView;
private bool _showToolBar;
#endregion
#region Public Properties
[Personalizable(true),
Category("Custom"),
WebBrowsable(true),
WebDisplayName("List Name"),
WebDescription("List Name")]
public string ListName
{
get { return _listName; }
set { _listName = value; }
}
[Personalizable(true),
Category("Custom"),
WebBrowsable(true),
WebDisplayName("Show Tool Bar"),
WebDescription("Check To Show Tool Bar")]
public bool ShowToolBar
{
get { return _showToolBar; }
set { _showToolBar = value; }
}
#endregion
#region Overrides - CreateChildControls
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
_siteID = SPContext.Current.Site.ID;
_webID = SPContext.Current.Web.ID;
//Getting List
if (!string.IsNullOrEmpty(ListName))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(_siteID))
{
using (SPWeb web = site.OpenWeb(_webID))
{

Joe ZhouPosted Apr 18, 2013, 6:24 AM
comment out _listViewWebPart.GetDesignTimeHtml(); to get items
Swapnil SawantPosted Sep 13, 2012, 8:09 AM
hii tried ur code, but same as James even m not getting any of the items in list. plz help us out
james niesenPosted Jun 19, 2012, 10:19 AM
Thanks for the correction. The toolbar is getting added correctly. I can upload to the list and export the list to a spreadsheet. However, none of the list item's are appearing. I'm not sure what's going on because the code looks correct. I will try and figure it out and post back what I find.
Bhushan GawalePosted Jun 18, 2012, 11:58 PM
Thanks for notification James, corrected the line now.
james niesenPosted Jun 18, 2012, 4:25 PM
Hi I tried copying your code to test it out, but in the last method "_elevatedContext" is not defined.