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
- var web = properties.Feature.Parent as SPWeb;
- web.AllowUnsafeUpdates = true;
- //Get the list
- var sharedDocuments = web.Lists.TryGetList("Shared Documents");
- //create a custom view to limit rows
- var viewCollection = sharedDocuments.Views;
- const string viewName = "HomeWebPartView";
- var homeWpView =
- viewCollection.Cast<SPView>().FirstOrDefault(c => c.Title.Equals(viewName));
- if (homeWpView == null)
- {
- var viewFields = new StringCollection
- { "Type", "LinkFilename", "Modified", "Editor" };
- homeWpView =
- viewCollection.Add(viewName, viewFields, string.Empty, 5, true, false);
- sharedDocuments.Update();
- }
- //create the webpart
- var sharedDocsWebPart = new XsltListViewWebPart
- {
- ListId = sharedDocuments.ID,
- ViewGuid = homeWpView.ID.ToString("B").ToUpper(),
- Title = "Shared Documents",
- ChromeType = PartChromeType.TitleOnly,
- ID = Guid.NewGuid().ToString()
- };
- //add webpart to page
- var homePage = web.GetFile("SitePages/Home.aspx");
- using (var webPartManager = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
- {
- webPartManager.AddWebPart(sharedDocsWebPart, "Left", 0);
- homePage.Update();
- }
- 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.
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.
- var web = properties.Feature.Parent as SPWeb;
- web.AllowUnsafeUpdates = true;
- //Get the list
- var sharedDocuments = web.Lists.TryGetList("Shared Documents");
- //create a custom view to limit rows
- var viewCollection = sharedDocuments.Views;
- const string viewName = "HomeWebPartView";
- var homeWpView =
- viewCollection.Cast<SPView>().FirstOrDefault(c => c.Title.Equals(viewName));
- if (homeWpView == null)
- {
- var viewFields = new StringCollection
- { "Type", "LinkFilename", "Modified", "Editor" };
- homeWpView =
- viewCollection.Add(viewName, viewFields, string.Empty, 5, true, false);
- sharedDocuments.Update();
- }
- //create the webpart
- var sharedDocsWebPart = new XsltListViewWebPart
- {
- ListId = sharedDocuments.ID,
- ViewGuid = homeWpView.ID.ToString("B").ToUpper(),
- Title = "Shared Documents",
- ChromeType = PartChromeType.TitleOnly,
- ID = Guid.NewGuid().ToString()
- };
- //add webpart to page
- var homePage = web.GetFile("SitePages/Home.aspx");
- using (var webPartManager = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
- {
- webPartManager.AddWebPart(sharedDocsWebPart, "Left", 0);
- homePage.Update();
- //get the webpart again from the webpart manager
- var sharedDocsWp =
- webPartManager.WebParts.Cast<WebPart>().FirstOrDefault
- (wp => wp.Title.Equals("Shared Documents"))
- as XsltListViewWebPart;
- //modify the current view
- homeWpView.ViewFields.DeleteAll();
- homeWpView.ViewFields.Add("Type");
- homeWpView.ViewFields.Add("LinkFilename");
- homeWpView.ViewFields.Add("Modified");
- homeWpView.ViewFields.Add("Editor");
- homeWpView.Update();
- webPartManager.SaveChanges(sharedDocsWp);
- homePage.Update();
- }
- web.Update();
After deploying the code, pagination of the web part work as expected.
No comments:
Post a Comment