More in this section

Forums / Developing with Sitefinity / Set Event Owner in Code Behind

Set Event Owner in Code Behind

3 posts, 0 answered
  1. Richard
    Richard avatar
    14 posts
    Registered:
    23 Apr 2012
    17 May 2012
    Link to this post
    Hi,

    I am writing a custom control that will allow users to create events dynamically.  My problem comes when I try to set the event's owner.  When I do this the event doesn't save, and I don't seem to get any errors on the page or in the code, it just doesn't show up in the database.  But if I remove the code that sets the owner property, it saves correctly.  I have debugged through it and can see that the Guid that it is setting is correctly being found as the Id of my user row.  This is a snippet of the code I am using:

    EventsManager manager = EventsManager.GetManager();
     
    Event eventItem = manager.CreateEvent();
     
    eventItem.Title = eventData["title"];
    eventItem.UrlName = Regex.Replace(eventData["title"].ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
    eventItem.City = eventData["city"];
    eventItem.Country = eventData["country"];
    eventItem.EventStart = DateTime.Parse(eventData["start"]);
    eventItem.EventEnd = DateTime.Parse(eventData["end"]);
    eventItem.Content = eventData["description"];
     
    UserManager userManager = UserManager.GetManager();
    eventItem.Owner = userManager.GetUser(HttpContext.Current.User.Identity.Name).Id;
     
    manager.SaveChanges();
     
    Dictionary<String, String> bag = new Dictionary<String, String>();
    bag.Add("ContentType", typeof(Event).FullName);
     
    WorkflowManager.MessageWorkflow(eventItem.Id, typeof(Event), null, "Publish", false, bag);
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    17 May 2012
    Link to this post
    hi Richard, try this line instead of the userManager.GetUser method:

    eventItem.Owner = SecurityManager.GetCurrentUserId();

    Hopefully that should do it. hope this is helpful!
  3. Richard
    Richard avatar
    14 posts
    Registered:
    23 Apr 2012
    25 May 2012
    Link to this post
    Thanks, I actually ended up getting the UserManager to work
3 posts, 0 answered