Sunday, January 3, 2010

SPView object Query property update does not work.

"I've updated the query property of the view as show in the code but the view does not work unless I select the view/ modify the view and hit the Ok button using the user interface.

bottom line: programmatic update of query does not work as expected."

private void UpdateViewV2()
    {
        SPSite site = SPContext.Current.Site;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite elevatedPrivilegesSiteColl = new SPSite(site.ID))
            {
                using (SPWeb oWebsite = elevatedPrivilegesSiteColl.RootWeb)
                {
                    oWebsite.AllowUnsafeUpdates = true;
                    SPList oList = oWebsite.Lists[this.listName];
                    SPView oView = oList.Views[this.viewName];
                    oView.Query = ""+
                                                "" + this.strStartDate + ""+
                                                "" + this.strEndDate + ""+
                                    "";
                    oView.Update();
                    oList.Update();
                    oWebsite.AllowUnsafeUpdates = false;
                    oWebsite.Update();
                }
            }
        });
        site.Close();
    }

0 comments: