More in this section

Forums / Developing with Sitefinity / EventsManager CreateEvent method Authorization error

EventsManager CreateEvent method Authorization error

5 posts, 2 answered
  1. Ajit
    Ajit avatar
    0 posts
    Registered:
    30 Dec 2015
    17 Oct
    Link to this post

    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

     

  2. Jonathan
    Jonathan avatar
    119 posts
    Registered:
    19 Dec 2012
    17 Oct in reply to Ajit
    Link to this post
    EventsManager eventsManager = EventsManager.GetManager();
    using(new ElevatedModeRegion(eventsManager )){
         var eventToAdd = eventsManager.CreateEvent();
    }
    Answered
  3. Ajit
    Ajit avatar
    0 posts
    Registered:
    30 Dec 2015
    17 Oct in reply to Jonathan
    Link to this post

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

     

  4. Ajit
    Ajit avatar
    0 posts
    Registered:
    30 Dec 2015
    17 Oct
    Link to this post

    The following code solved the issue. 

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

    Answered
  5. Jonathan
    Jonathan avatar
    119 posts
    Registered:
    19 Dec 2012
    18 Oct
    Link to this post
    Looks good. You should be able to run all the code within a single elevated privileges block.
5 posts, 2 answered