Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

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("");
share|improve this answer
add comment

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.

share|improve this answer
add comment

I did the same thing and blogged it on http://yoursandmyideas.wordpress.com/2011/08/11/xsltlistviewwebpart-in-sharepoint-2010-uncovered/

share|improve this answer
2  
Welcome to SharePoint! While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See How to Answer for general guidelines. –  Kit Menke Oct 28 '11 at 15:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.