Howdy all,
A bit of a developer question here.... I've got a XsltListViewWebPart added
on a SP2010 site page, in which I want to swap in and out which view it is
using at will.
Just doing it though a console application which will do it for now (will be
a feature activated event).
Code:
using (SPSite site = new SPSite(siteurl))
{
using (SPWeb currentWeb = site.OpenWeb())
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
currentWeb.AllowUnsafeUpdates = true;
SPFile CurrentPage = currentWeb.GetFile(pageurl);
using (SPLimitedWebPartManager wpm =
currentWeb.GetLimitedWebPartManager(pageurl, PersonalizationScope.Shared))
{
const string SourceListName = "Shared
Documents";
const string SourceViewName = "Documents View";
const string DestViewName = "All Documents";
// get view ID
SPList SourceList =
currentWeb.Lists[SourceListName];
SPView SourceListView =
SourceList.Views[SourceViewName];
SPView DestListView =
SourceList.Views[DestViewName];
// loop though page, get LVWP
var listViews =
wpm.WebParts.Cast<System.Web.UI.WebControls.WebParts.WebPart>()
.Where(webpart => webpart is
XsltListViewWebPart)
.Cast<XsltListViewWebPart>();
// get first LVWP
XsltListViewWebPart listViewWP =
listViews.First();
listViewWP.ViewGuid =
DestListView.ID.ToString("B").ToUpper();
listViewWP.Visible = true;
listViewWP.Title = "changed!";
wpm.SaveChanges(listViewWP);
currentWeb.AllowUnsafeUpdates = false;
currentWeb.Update();
}
}); // end elev
}
}
Now its worth noting that the title does indeed change (lol), but the view
will always remain the embedded view from the web part.
Has anyone done something simular or have a work around at all? I've event
tried blanking / replacing the listViewWP.XmlDefinition with
DestListView.HtmlSchemaXml and DestListView.GetViewXml() without much joy.
Thanks in Advance,
Daniel
_______________________________________________
ozmoss mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss