0

Please help me to reflect the view changes to the web part pages programmatically using C#

Is it possible ?

1 Answer 1

0

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);
            }
        } 
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.