Please help me to reflect the view changes to the web part pages programmatically using C#
Is it possible ?
The following SSOM code for your reference.
var siteUrl = "http://sp2013/sites/team";
var pageUrl = siteUrl + "/SitePages/TestChangeView.aspx";
var listName = "TestCustomList";
var viewName = "TestView";
var webpartTitle = "TestCustomList";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile(pageUrl);
SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
SPList list = web.Lists[listName];
SPView view = list.Views[viewName];
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in mgr.WebParts)
{
if (wp.Title == webpartTitle)
{
XsltListViewWebPart xsltwp = wp as XsltListViewWebPart;
PropertyInfo pi = xsltwp.GetType().GetProperty("ContextView", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
SPView wpView = (SPView)(pi.GetValue(xsltwp, null));
wpView.SetViewXml(view.GetViewXml());
wpView.Update();
mgr.SaveChanges(xsltwp);
}
}
}
}