Show List View web part in visual web part in SharePoint 2010

InfoPath alternatives for form designing SharePoint

In this post, we will discuss how to show List View Web Part in a custom visual web part in SharePoint 2010. Also, you can check out:

Hide a column from List Form(new/edit/display) in SharePoint Server 2010

Add lookup field type column in the list using visual studio 2013 by autohosted apps in sharepoint 2013

How to synchronize new user from Active Directory in SharePoint 2010?

1. First, we create a visual web part using visual studio 2010

2. Now we add the following code under web part.cs file in CreateChildControls() method for showing the list view web part under the visual web part.

XsltListViewWebPart XLV = new XsltListViewWebPart();

SPWeb web1 = SPContext.Current.Web;

SPList lists = web1.Lists[“Leave Details”]; //your list name

XLV.ListId = lists.ID;

XLV.ViewGuid = lists.DefaultView.ID.ToString(); //default view

this.Controls.Add(XLV);

3. Now your webpart.cs file looks like this. See the whole code

using System;

using System.ComponentModel;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace LeaveApplication.LeaveWebPart

{

[ToolboxItemAttribute(false)]

public class LeaveWebPart : Microsoft.SharePoint.WebPartPages.WebPart

{

// Visual Studio might automatically update this path when you change the Visual Web Part project item.

private const string _ascxPath = @”~/_CONTROLTEMPLATES/LeaveApplication/LeaveWebPart/LeaveWebPartUserControl.ascx”;

protected override void CreateChildControls()

{

LeaveWebPartUserControl control = Page.LoadControl(_ascxPath) as LeaveWebPartUserControl;

XsltListViewWebPart XLV = new XsltListViewWebPart();

SPWeb web1 = SPContext.Current.Web;

SPList lists = web1.Lists[“Leave Details”];

XLV.ListId = lists.ID;

XLV.ViewGuid = lists.DefaultView.ID.ToString();

this.Controls.Add(XLV);

if (control != null)
{

control.lwebpart = this;

}

Controls.Add(control);
}

protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
}

4. Now save and deploy your web part.

5. Now open your site in browser where you deploy web part.


6. Now add that web part in your page where you want to show.

7. Now you see your list view web part under your custom web part. See in fig

Show List View web part in visual web part in SharePoint 2010
Show List View web part in visual web part in SharePoint 2010

You May Also like the Following SharePoint Online Tutorials:

About Navneet Singh

I am Navneet Singh, Working at C3it software solutions pvt. ltd. in Pune. My core skills are SharePoint 2013, Office 365, SharePoint 2010, SharePoint Designer, SharePoint Designer Workflow, Nintex Workflow, Infopath, Webpart, CSOM/SSOM, Power-shell, C#, ASP.NET and SQL Server.

View all posts by Navneet Singh →

Leave a Reply