12

I'm using ListViewWebPart to display contents of SPFolder (subfolder inside a list) to display its contents.

Below is the code to implement this:

protected override void CreateChildControls()
  {
    try
    {
      base.CreateChildControls();

      webPart = new ListViewWebPart();

      using (var site = new SPSite(SPContext.Current.Web.Url))
      using (var web = site.OpenWeb())
      {
        clientName = DataLogic.Client.GetClientName(Constants.Session.Client_ClientID);
        var library = web.Lists["Account"];
        webPart.ListName = library.ID.ToString("B").ToUpper();
        webPart.ListId = library.ID;   
        SPFolder folder = web.GetFolder("/Account/" + clientName);
        if (folder.Item != null)
        {
          SPContentTypeId folderctid = folder.Item.ContentType.Id;

          //set the folder url
          SetPrivateFieldValue(webPart, "rootFolder", folder.Url);
          SetPrivateFieldValue(webPart, "folderCtId", folderctid.ToString());
          webPart.ListViewXml = library.DefaultView.GetViewXml();            
          webPart.ChromeType = PartChromeType.None;
          this.mainSec.Controls.Add(webPart);
        }
        else
        {
          lblWarning.Text = "There is no document library associated with client " + clientName;
        }           

      }

    }
    catch (Exception ex)
    {

    }
private static void SetPrivateFieldValue(object obj, string fieldName, string val)
  {
    FieldInfo fi = obj.GetType().GetField(fieldName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
    fi.SetValue(obj, val);
  }

The contents display correctly but the toolbar is the parent list's toolbar and not the folder's toolbar.

How can i modify the toolbar context to load the context of the spfolder?

1
  • I have the same requirement. Did you find a solution for this? Regards, H.
    – user8283
    May 10, 2012 at 7:19

1 Answer 1

1

Create a view that displays the folder content you need and use it in ListViewWebPart, setting ViewId and ListID and WebId, also you can change the SPView xml (and related query part) and set it to ListViewXml property of ListViewWebPart (thus you can modify view dynamically).

I think in that case toolbar will be shown as needed.

You can see a link for more details: http://ketulpatel.wordpress.com/2008/06/25/programatically-working-with-listviewwebpart-using-non-default-view/

1
  • Thanks for the reply but the only way to get the contents of the subfolder showing is by using reflection(i.e. setting the rootFolder and folderCtId properties of the webpart).Now the view is still the parent list view not the subfolder view.So the toolbar is still the parent lists toolbar.SPView view = library.GetUncustomizedViewByBaseViewId(0); webPart.ListViewXml = view.HtmlSchemaXml; This is still getting the parent list's toolbar as the view is the parent lists.
    – Tulika
    Jan 17, 2012 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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