Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
        }

        }
}
}
share|improve this question
1  
Please notice, that you must not dispose the SPContext.Current.Site and the SPContext.Current.Web as stated here: msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx. But this will probably not solve your problem. –  Matthias Dec 2 '11 at 9:22
add comment

2 Answers 2

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!

share|improve this answer
add comment
up vote 4 down vote accepted

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 =)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.