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'm using listviewwebpart to display contents of SPFolder (subfolder inside a list) to display its contents.

Beolw 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 ?

Thanks TulikarootFolder

share|improve this question
    
I have the same requirement. Did you find a solution for this? Regards, H. –  user8283 May 10 '12 at 7:19
add comment

1 Answer

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/

share|improve this answer
    
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 '12 at 11:52
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.