2

I want to place a list view of the document library using XsltListViewWebPart.

My is as below:

using (SPSite site = new SPSite("XXX/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPFile page = web.GetFile("Pages/webparts.aspx");
        if (page != null)
        {                        
            page.CheckOut();
            SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
            SPList docLib = web.Lists["Documents"];
            SPView docView = docLib.DefaultView;
            if (docLib != null)
            {
                XsltListViewWebPart view = new XsltListViewWebPart();                            
                view.ListId = docLib.ID;                            

                SPView lstView = docLib.DefaultView;
                view.Toolbar = "";
                lstView.RowLimit = 10;
                lstView.Paged = true;                            
                lstView.Update();                          

                view.AllowClose = false;

                view.AllowConnect = false;

                view.AllowEdit = false;

                view.AllowHide = false;

                view.AllowMinimize = false;

                view.AllowZoneChange = false;
                view.ViewGuid = lstView.ID.ToString("B").ToUpper();


                view.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
                wpmgr.AddWebPart(view, "Header", 0);
            }
        }
    }
}

After executing above code web part is added successfully with paging but I am facing an issue that I can not go to previous page as I can go with standard page library view paging. For example, I have given RowLimit = 10 and if I have 15 documents in the library, I can go to next page but cannot go back to previous page.

Below are images of pages where the XsltListViewWebPart is placed. And I have 13 documents in my documents library.

Page 01 of XsltListViewWebPart

enter image description here

How should I tackle this?

2

After doing some experiments on the page where the web part was added.

First I edited the web part then simply save the web part. It started showing the button to go previous page.

Finally, got the answer for my issue that I needed just single line of code make it work.

The final code is :

view.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
wpmgr.AddWebPart(view, "Header", 0);
wpmgr.SaveChanges(view);

Remaining things are same in my code: just add wpmgr.SaveChanges(view); after adding the web part in the page. It would work perfectly.

Hope, this saves the time of others..!!! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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