How to extend a ListView WebPart (from code)
-
06-12-2019 - |
Question
I'd like to extend the ListView WebPart so that it can read data from the url... I tried to extend a ListView WebPart but compiler told me it is sealed and so I cannot extend.. Then I tried to insert a ListView WebPart INSIDE my custom webpart but this way doesn't work...
How could I do?
This is my code!
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;
namespace UrlParametrizedListViewWebPart.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
private Microsoft.SharePoint.WebPartPages.ListViewWebPart myListView;
protected override void CreateChildControls()
{
base.CreateChildControls();
SPSite oSiteCollection = SPContext.Current.Site
string listName = "docs";
SPWeb oWebSite = SPContext.Current.Web
myListView = new Microsoft.SharePoint.WebPartPages.ListViewWebPart();
myListView.Visible = true;
myListView.EnableViewState = true;
SPList list = oWebSite.Lists[listName];
myListView.ListId = (System.Guid)list.ID;
myListView.ViewGuid = list.DefaultView.IT.ToString();
Controls.Add(myListView);
}
}
}
Result of this code:
< !-- #RENDER FAILED -->
I tried another way: add all the controls of the "son webpart" and not the webpart.. It seems working! I think is not the right way to do that.. So if everybody has some suggestion... =)
namespace UrlParametrizedListViewWebPart.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
private Microsoft.SharePoint.WebPartPages.ListViewWebPart myListView;
protected override void CreateChildControls()
{
base.CreateChildControls();
SPSite oSiteCollection = SPContext.Current.Site
string listName = "docs";
SPWeb oWebSite = SPContext.Current.Web
myListView = new Microsoft.SharePoint.WebPartPages.ListViewWebPart();
myListView.Visible = true;
myListView.EnableViewState = true;
SPList list = oWebSite.Lists[listName];
myListView.ListId = (System.Guid)list.ID;
myListView.ViewGuid = list.DefaultView.IT.ToString();
//ADDS MANUALLY THE WP CONTROLS:
foreach(Control c in myListView.Controls)
{
this.Controls.Add(c);
}
}
}
}
Solution
Don't know really HOW, but this code works!
Take a look =)
public partial class VisualWebPart1UserControl : UserControl
{
private Microsoft.SharePoint.WebPartPages.ListViewWebPart myListView;
protected override void CreateChildControls()
{
base.CreateChildControls();
SPSite oSiteCollection = SPContext.Current.Site;
string listName = "docs";
SPWeb oWebSite = SPContext.Current.Web;
myListView = new Microsoft.SharePoint.WebPartPages.ListViewWebPart();
myListView.Visible = true;
myListView.EnableViewState = true;
SPList list = oWebSite.Lists[listName];
myListView.ListName = list.ID.ToString("B").ToUpperInvariant();
myListView.TitleUrl = list.DefaultViewUrl;
myListView.WebId = list.ParentWeb.ID;
myListView.ListId = (System.Guid)list.ID;
myListView.ViewGuid = list.DefaultView.ID.ToString("B").ToUpperInvariant();
myListView.HelpMode = WebPartHelpMode.Modeless;
Controls.Add(myListView);
}
Now, to access the params simply use the Request.params and put in list name the list name that you want.. I create this cose (it's incomplete) to select the folder and the document library from URL in the webpart!
Hope this will help someone =)
OTHER TIPS
myListView.ViewGuid = list.DefaultView.ID.ToString("B").ToUpperInvariant();
did the trick for you...
If you set the ViewGuid property, it needs the Guid of the particular View Case sensitive!