I'm trying to update a SPView query, it doesn't give any error but it also doesn't update the property (SPView.Query). In SPManager 2007 it works with the resulting query of that code.
Can you spot something wrong with the following code:
int tempYear = 2010;
Int32.TryParse(ddlAnos.SelectedValue, out tempYear);
DateTime dtInicio = new DateTime(tempYear, 1, 1);
DateTime dtFim = new DateTime(tempYear, 12, 31);
string tempQuery = "<Where><And><Geq><FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"TRUE\" Type=\"DateTime\">" +
SPUtility.CreateISO8601DateTimeFromSystemDateTime(dtInicio)
+ "</Value></Geq><Leq><FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"TRUE\" Type=\"DateTime\">" +
SPUtility.CreateISO8601DateTimeFromSystemDateTime(dtFim)
+ "</Value></Leq></And></Where><OrderBy><FieldRef Name=\"Created\" Ascending=\"False\" /></OrderBy>";
foreach (SPListItem tempView in webListaVistas.Lists[ListaViews].Items)
{
SPSite siteColl = SPContext.Current.Site;
SPUser user = SPContext.Current.Site.Owner;
SPWeb site = SPContext.Current.Site.OpenWeb(tempView[ListaViewsSPWeb].ToString());
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID, user.UserToken))
{
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
{
SPList listViewActual = ElevatedSite.Lists[tempView[ListaViewsNomeLista].ToString()];
for (int i = 0; i < listViewActual.Views.Count; i++)
{
if (listViewActual.Views[i].DefaultView == true)
{
listViewActual.Views[i].Query = tempQuery;
ElevatedSite.AllowUnsafeUpdates = true;
listViewActual.Views[i].Update();
listViewActual.Update();
ElevatedSite.Update();
ElevatedSite.AllowUnsafeUpdates = false;
lblInfo.Text = user.Name + "\n\n" + tempQuery + "\n\n" + listViewActual.Views[i].Query.ToString() + "\n\n" + listViewActual.Views[i].Title + "\n\nAno alterado com sucesso: " + tempYear.ToString();
}
}
}
}
});
}
SPList.Views[i]
twice, you get two separate instances of that View (SPList.Views[int i]
will returnnew SPView
), which are unrelated in terms of memory usage, so when you update the Query property of the first call, and the Update() method of the second call, the change from the first call (updating the Query property) is still persisted in memory from the first call, and the second call won't catch it. See my answer of a more efficient way to use the SPView class. – James Love Apr 18 '11 at 15:53