EventsManager CreateEvent method Authorization error

Posted by Community Admin on 04-Aug-2018 17:32

EventsManager CreateEvent method Authorization error

All Replies

Posted by Community Admin on 17-Oct-2017 00:00

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

 

Posted by Community Admin on 17-Oct-2017 00:00

EventsManager eventsManager = EventsManager.GetManager();
using(new ElevatedModeRegion(eventsManager ))
     var eventToAdd = eventsManager.CreateEvent();

Posted by Community Admin on 17-Oct-2017 00:00

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

 

Posted by Community Admin on 17-Oct-2017 00:00

The following code solved the issue. 

SystemManager.RunWithElevatedPrivilege(d => WorkflowManager.MessageWorkflow(eventId, typeof(Event), null, "Publish", false, bag); );

Posted by Community Admin on 18-Oct-2017 00:00

Looks good. You should be able to run all the code within a single elevated privileges block.

This thread is closed