EventsManager CreateEvent method Authorization error
Hi, I am kind of new to Sitefinity and needed some help getting started. I am trying to import some Calendar Events programatically during application startup (and potentially also on a schedule). It works fine if I give permissions to to the "Everyone" role to create events. However, if this permission is not set, I get an authorization error:
Error encountered: You are not authorized to 'Create 0' ('General').
at DynamicModule.ns.Wrapped_OpenAccessEventProvider_e187beddeb5047ed8268db4617c409cc.CreateEvent()
at Telerik.Sitefinity.Modules.Events.EventsManager.CreateEvent()
The current Principal is showing as "Anonymous".
My code looks something similar to:
EventsManager eventsManager = EventsManager.GetManager();
var eventToAdd = eventsManager.CreateEvent();
Is there a way to bypass permissions check for Application Startup, or Scheduled tasks?
Any help would be appreciated.
Thanks,
Ajit
EventsManager eventsManager = EventsManager.GetManager();
using(new ElevatedModeRegion(eventsManager ))
var eventToAdd = eventsManager.CreateEvent();
Thank you. The CreateEvent method now works fine, however, when I try to publish, I encounter the same error.
This the code that i use to publish the new events:
eventsManager.SaveChanges();
//Publish
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(Event).FullName);
WorkflowManager.MessageWorkflow(eventId, typeof(Event), null, "Publish", false, bag);
The following code solved the issue.
SystemManager.RunWithElevatedPrivilege(d => WorkflowManager.MessageWorkflow(eventId, typeof(Event), null, "Publish", false, bag); );
Looks good. You should be able to run all the code within a single elevated privileges block.