Tuesday, January 14, 2014

Fix : Pagination not working properly in XsltListViewWebPart with custom view

Let’s assume that we need to create a XsltListViewWebPart programmatically with a custom view.We can use a feature receiver to do that. Following is a sample code to add the web part to the page

  1. var web = properties.Feature.Parent as SPWeb;
  2. web.AllowUnsafeUpdates = true;
  3. //Get the list
  4. var sharedDocuments = web.Lists.TryGetList("Shared Documents");
  5. //create a custom view to limit rows
  6. var viewCollection = sharedDocuments.Views;
  7. const string viewName = "HomeWebPartView";
  8. var homeWpView =
  9.     viewCollection.Cast<SPView>().FirstOrDefault(c => c.Title.Equals(viewName));
  10. if (homeWpView == null)
  11. {
  12.     var viewFields = new StringCollection
  13.                         { "Type", "LinkFilename", "Modified", "Editor" };
  14.     homeWpView =
  15.         viewCollection.Add(viewName, viewFields, string.Empty, 5, true, false);
  16.     sharedDocuments.Update();
  17. }
  18. //create the webpart
  19. var sharedDocsWebPart = new XsltListViewWebPart
  20.                           {
  21.                              ListId = sharedDocuments.ID,
  22.                              ViewGuid = homeWpView.ID.ToString("B").ToUpper(),
  23.                              Title = "Shared Documents",
  24.                              ChromeType = PartChromeType.TitleOnly,
  25.                              ID = Guid.NewGuid().ToString()
  26.                           };
  27. //add webpart to page
  28. var homePage = web.GetFile("SitePages/Home.aspx");
  29. using (var webPartManager = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
  30. {
  31.     webPartManager.AddWebPart(sharedDocsWebPart, "Left", 0);
  32.     homePage.Update();
  33. }
  34. web.Update();

But the added web part showed an unexpected behavior. for an example, the pagination does not work for other pages other than the first page. As you can see from the below image, pagination is missing from the second page.

image

To fix the issue we need to modify the code like below. The change I’ve done is to remove all view fields and add them again.

  1. var web = properties.Feature.Parent as SPWeb;
  2. web.AllowUnsafeUpdates = true;
  3. //Get the list
  4. var sharedDocuments = web.Lists.TryGetList("Shared Documents");
  5. //create a custom view to limit rows
  6. var viewCollection = sharedDocuments.Views;
  7. const string viewName = "HomeWebPartView";
  8. var homeWpView =
  9.     viewCollection.Cast<SPView>().FirstOrDefault(c => c.Title.Equals(viewName));
  10. if (homeWpView == null)
  11. {
  12.     var viewFields = new StringCollection
  13.                         { "Type", "LinkFilename", "Modified", "Editor" };
  14.     homeWpView =
  15.         viewCollection.Add(viewName, viewFields, string.Empty, 5, true, false);
  16.     sharedDocuments.Update();
  17. }
  18. //create the webpart
  19. var sharedDocsWebPart = new XsltListViewWebPart
  20.                           {
  21.                              ListId = sharedDocuments.ID,
  22.                              ViewGuid = homeWpView.ID.ToString("B").ToUpper(),
  23.                              Title = "Shared Documents",
  24.                              ChromeType = PartChromeType.TitleOnly,
  25.                              ID = Guid.NewGuid().ToString()
  26.                           };
  27. //add webpart to page
  28. var homePage = web.GetFile("SitePages/Home.aspx");
  29. using (var webPartManager = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
  30. {
  31.     webPartManager.AddWebPart(sharedDocsWebPart, "Left", 0);
  32.     homePage.Update();
  33.  
  34.     //get the webpart again from the webpart manager
  35.     var sharedDocsWp =
  36.         webPartManager.WebParts.Cast<WebPart>().FirstOrDefault
  37.         (wp => wp.Title.Equals("Shared Documents"))
  38.         as XsltListViewWebPart;
  39.     //modify the current view
  40.     homeWpView.ViewFields.DeleteAll();
  41.     homeWpView.ViewFields.Add("Type");
  42.     homeWpView.ViewFields.Add("LinkFilename");
  43.     homeWpView.ViewFields.Add("Modified");
  44.     homeWpView.ViewFields.Add("Editor");
  45.     homeWpView.Update();
  46.  
  47.     webPartManager.SaveChanges(sharedDocsWp);
  48.     homePage.Update();
  49. }
  50. web.Update();

After deploying the code, pagination of the web part work as expected.

image

No comments: