Question

In my situation, I'm creating web part pages with XsltListViewWebParts on them. I need to set a predefined view on them, with custom filtering / columns setup in CAML. Therefore, I tried moving the XmlDefinition bit out of the web part so I can just reference the proper definition.

I also tried the following:

  • Saving the XsltListViewWebPart with the correct view setup as an .webpart file, and adding it to the web part gallery. This works great for the site on which the part was created, but as soon as I restore it elsewhere; the definition is reset. Notice that when I export; I explicitely choose the option to NOT use the local list, but a relative /Lists/Listname list.

  • When I programmatically add the the webpart to the page, it's also possible (with some hacks) to get the embedded view. Although it is possible to setup some things here (like the columns shown); this is not ideal. Also, it seems to always revert back to view BaseTypeId="0" which doesn't have paging. I haven't found any options to change this.

  • The default view shown (which I don't want) seemed to be the default view of the list itself. So I tried changing that before adding the webpart to the page, but that changes nothing.

  • Finally, there's an option called XmlDefinitionLink. This promises to load a custom view definition from an XML file. Since that's pretty much what I need, I tried that too. No matter how I set it up, it just won't work. I don't get any errors, but no results either. There seem to be 0 resources available on how to get this working, only an MSDN article telling me that it should be great (which it obviously isn't).

If someone has successfully reused the XSLTListViewWebPart with a custom view definition, please share your knowledge. I don't mind writing a bunch of code or wrapper classes to get there, as long as I know I can actually get there. It's taken up a lot of time up till now, without any result.

Was it helpful?

Solution

I prefer to do this in code in a custom provisioning provider (SPWebProvisioningProvider). The following kind of code should do the trick:

SPWeb web = SPContext.Current.Web;
SPList list = web.GetList("/sites/thesite/doclib");
SPView view = list.Views["TheView"];

string pageUrl = PublishingWeb.GetPagesListName(web) + "/thepage.aspx";
SPFile pageFile = web.GetFile(pageUrl);
pageFile.CheckOut();            
SPLimitedWebPartManager lwpm = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

XsltListViewWebPart wp = new XsltListViewWebPart();
wp.ListId = list.ID;
wp.Title = "Web Part Title";
wp.ChromeType = PartChromeType.TitleOnly;
wp.ViewGuid = view.ID.ToString();
wp.XmlDefinition = view.GetViewXml();
lwpm.AddWebPart(wp, "Header", 1);

pageFile.CheckIn("", SPCheckinType.MinorCheckIn);
pageFile.Publish("");

OTHER TIPS

I am working on displaying tags cloud of posts' categories in a blog. Since content by query webpart doesn't support LookupMulti fields, I used XsltListViewWebPart. Here are the steps :

  • add a view in "schema.xml" of posts with a new "BaseViewID"
  • set "XslLink" to a copy of "blog.xsl"
  • add necessary "FieldRef" in "ViewFields"
  • add a template matching the "BaseViewID"
  • add the webpart when provisionning the page

    <View List="301" BaseViewID="8" WebPartZoneID="Left" WebPartOrder="1"> <![CDATA[ <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <metaData> <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" /> <importErrorMessage>Cannot import this Web Part.</importErrorMessage> </metaData> <data> <properties> <property name="AllowConnect" type="bool">True</property> <property name="ChromeType" type="chrometype">None</property> <property name="AllowClose" type="bool">False</property> </properties> </data> </webPart> </webParts> ]]> </View>

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top