I am using the the XsltListViewWebPart to display all the values of a list to the user and depending on the user and different properties of the list items I needed to filter the view as well as assign a different XSL file.
The biggest problem I had was that the list view GUID is created only at deploy time and I could find no other way to assign a different view.
I came across several posts that had examples of changing the view or adding a new XLVWP to the page at deploy time
http://sharepoint.stackexchange.com/questions/4843/reusing-the-xsltlistviewwebpart and http://social.technet.microsoft.com/Forums/zh/sharepoint2010programming/thread/983d2098-d742-4708-8bdc-437aa07b3b9d
So I wanted to take this a little further and be able to dictate what view was used on the fly. Using a QueryString of the ListID and the ListViewName I then assign the appropriate view in the OnInit event as shown below.
public partial class RecordHistory : DialogLayoutsPageBase { protected void Page_Load(object sender, EventArgs e) { } protected override void OnInit(EventArgs e) { base.OnInit(e); string[] strLists = Request.QueryString.GetValues("ListID"); string[] strListViewName = Request.QueryString.GetValues("ListViewName"); if (strLists == null || strLists.Length < 1) { throw new SPException(SPResource.GetString(Strings.MissingRequiredQueryString, "ListID")); } SPList spList = SPControl.GetContextWeb(Context).Lists.GetList(new Guid(strLists[0]), true); SPView view = spList.Views[strListViewName[0]]; this.xlvwpRecordHistory.ViewGuid = view.ID.ToString(); this.xlvwpRecordHistory.ViewId = int.Parse(view.BaseViewID); this.xlvwpRecordHistory.XmlDefinition = view.GetViewXml(); } }
As always there are many ways to do this that might be better but this worked and I found no other examples of this on the web.
Please let me know if you have a better idea