The timer job is running and creates lists but when I try and add list items to the list I get:
Additional information: The security validation for this page is invalid. Click Back
in your Web browser, refresh the page, and try your operation again
in debugging the code I see the error is when I try and update the list item.
I have tried many blog suggestions without success. I understand when using Elevated I need to be conscious of what web I am accessing.
here is the code block:
private void createAllCalendarLists(SPWeb oWeb)
{
string strListName = this.Properties["MasterCalendar"].ToString();
SPList sourcelist = oWeb.Lists.TryGetList(strListName);
if (sourcelist != null)
{
List<string> views = getAllViews(sourcelist);
foreach (string view in views)
{
//this needs to be the new web created linkedCalendars
using (SPWeb newWeb = oWeb.Webs["LinkedCalendars"])
{
createWebList(newWeb, view);
{
SPUtility.ValidateFormDigest();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(newWeb.Site.ID))
{
using (SPWeb web = site.OpenWeb(newWeb.ID))
{
//now add the items from the mastercalendar to list
SPListItemCollection items = getListItems(oWeb, view, sourcelist);
SPCalendarItemCollection calCollection = MakeCalendar(newWeb, items);
SPList calList = newWeb.Lists.TryGetList(view);
if (calCollection.Count > 0)
{
foreach (SPCalendarItem item in calCollection)
{
SPListItem newItem = calList.Items.Add();
newItem["EventDate"] = item.StartDate;
newItem["EndDate"] = item.EndDate;
newItem["Description"] = item.Description;
newItem["Location"] = item.Location;
newItem["Title"] = item.Title;
newItem["fRecurrence"] = item.IsRecurrence;
newItem["fAllDayEvent"] = item.IsAllDayEvent;
HttpContext Saved = HttpContext.Current;
SPWeb thisWeb = site.RootWeb;
try
{
thisWeb.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
web.Site.WebApplication.FormDigestSettings.Enabled = false;
HttpContext.Current = null;newItem.Update();}
catch (Exception ex)
{
//log ex
}
finally
{
thisWeb.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
web.Site.WebApplication.FormDigestSettings.Enabled = true;
HttpContext.Current = Saved;
}
}
}
}
}
});
}
}
}
}
}You can see by the try/catch/finally I have tried 4 different approaches. Maybe I am missing something. The timer job runs and creates a sub web, and 42calander lists based on the views in a master calendar, The purpose is to allow linked Calendars for outlook that are based on SharePoint Views.