1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Calling WorkflowManager.MessageWorkflow() when not authenticated

Calling WorkflowManager.MessageWorkflow() when not authenticated

8 posts, 1 answered
  1. Axe
    Axe avatar
    16 posts
    Registered:
    11 Feb 2007
    09 Jul 2012
    Link to this post
    I have a module (Module Builder) where I insert and edit entries using api. This happens as a scheduled task so there is no authentication. I use SuppressSecurityChecks when doing this for the inserts and updates which works well however I get errors when calling WorkflowManager.MessageWorkflow(). Is there anyway to do this when not authenticated or are there any other solutions. I need to be able to programmatically create items and publish them without a user being logged in.
  2. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    11 Jul 2012
    Link to this post
    Hello Alex,

    I have prepared the following sample for your problem.

    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
     
                dynamicModuleManager.Provider.SuppressSecurityChecks = true;
     
                Type moduleType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyModule.Module");
                DynamicContent moduleItem = dynamicModuleManager.CreateDataItem(moduleType);
                 
                // This is how values for the properties are set
                moduleItem.SetValue("Title", "MyNewItem");
                moduleItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
                moduleItem.SetValue("PublicationDate", DateTime.Now);
                moduleItem.SetValue("Content", "Sample Content");
                moduleItem.SetString("UrlName", new Lstring("url-sample"));           
     
                 
                dynamicModuleManager.Lifecycle.Publish(moduleItem);
                moduleItem.ApprovalWorkflowState = "Published";
     
                // You need to call SaveChanges() in order for the items to be actually persisted to data store
                dynamicModuleManager.SaveChanges();

    You need to suppress security checks and publish the item through the Module Manager's lifecycle. After that set the item's ApprovalWorkflowState to "Published" so you can see it as such in Sitefinity's UI.  Kind regards,
    Pavel Benov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. Axe
    Axe avatar
    16 posts
    Registered:
    11 Feb 2007
    12 Jul 2012
    Link to this post
    Thanks that worked. When updating should I set to published before check in or other way around?
    Using the code you provided what would be best practice to update and publish an existing item? Not sure the exact order of things.


    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();dynamicModuleManager.Provider.SuppressSecurityChecks = true;
    Type moduleType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyModule.Module");
     
    var masterModuleItem = dynamicModuleManager.GetDataItems(propertyType).Where(p => p.GetValue<string>("UniqueID") == uniqueID && p.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master).SingleOrDefault();
    var checkedOutModuleItem = dynamicModuleManager.Lifecycle.CheckOut(masterModuleItem) as DynamicContent;
     
    //Update the item
    checkedOutModuleItem.SetValue("Content", "Updated Content")
     
    dynamicModuleManager.Lifecycle.Publish(checkedOutModuleItem);
    checkedOutModuleItem.ApprovalWorkflowState = "Published";
    masterModuleItem = dynamicModuleManager.Lifecycle.CheckIn(checkedOutModuleItem) as DynamicContent;
     
    dynamicModuleManager.SaveChanges();

  4. Axe
    Axe avatar
    16 posts
    Registered:
    11 Feb 2007
    12 Jul 2012
    Link to this post
    I tried what I posted for updates and worked out it should be the other way around.

    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();dynamicModuleManager.Provider.SuppressSecurityChecks = true;
    Type moduleType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyModule.Module");
      
    var masterModuleItem = dynamicModuleManager.GetDataItems(propertyType).Where(p => p.GetValue<string>("UniqueID") == uniqueID && p.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master).SingleOrDefault();
    var checkedOutModuleItem = dynamicModuleManager.Lifecycle.CheckOut(masterModuleItem) as DynamicContent;
      
    //Update the item
    checkedOutModuleItem.SetValue("Content", "Updated Content")
      
    // check in first
    masterModuleItem = dynamicModuleManager.Lifecycle.CheckIn(checkedOutModuleItem) as DynamicContent;
      
    // then publish
    dynamicModuleManager.Lifecycle.Publish(checkedOutModuleItem);
    checkedOutModuleItem.ApprovalWorkflowState = "Published";
     
    //save item
    dynamicModuleManager.SaveChanges();
  5. Axe
    Axe avatar
    16 posts
    Registered:
    11 Feb 2007
    29 Jul 2012
    Link to this post
    I'm now getting error on dynamicModuleManager.SaveChanges() after upgrade to 5.1. 
    Can you please check this.
  6. AJ
    AJ avatar
    11 posts
    Registered:
    17 Sep 2012
    10 Aug 2012
    Link to this post
    I too am getting an error on  dynamicModuleManager.SaveChanges(); in 5.1

    Did you ever fix this??
  7. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    15 Aug 2012
    Link to this post
    Hi guys,

    The most probable reason that you are getting an error with the code after the upgrade is, that in 5.1 you need to be authenticated in order to create and edit items. Please take a look at this KB article addressing the problem and a solution for it. 

    All the best,
    Pavel Benov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Nathalia
    Nathalia avatar
    25 posts
    Registered:
    16 Nov 2011
    23 Aug 2012
    Link to this post

    Hi,

       I have a same problem. I was creating a new template page when I publish it,  I have an error of requirement of authentication.

      "Unauthenticated request. Most likely your session has expired. Please login and try again.".

      It ocurred when I upgraded 5.1, I added "ElevatedModeRegion"  in my code but it doesn't resolved my error.

      var pageManager = PageManager.GetManager();
     
    using (ElevatedModeRegion elevatedModeRegion = new ElevatedModeRegion(pageManager))
    {
         pageManager.Provider.SuppressSecurityChecks = true;
     
         var template = pageManager.GetTemplates().Where(t => t.Id == id).SingleOrDefault();
     
         if (template == null)
         {
                  var pageTemplate = pageManager.CreateTemplate(id);
                  pageTemplate.Name = name;
                  pageTemplate.Title = title;
                  pageTemplate.MasterPage = masterPage;
                  pageTemplate.Themes = theme;
                  var draft = pageManager.EditTemplate(pageTemplate.Id);
                  var master = pageManager.TemplatesLifecycle.CheckOut(draft);
                  master = pageManager.TemplatesLifecycle.CheckIn(master);
                  pageManager.TemplatesLifecycle.Publish(master);
                  pageManager.SaveChanges();
         }
     
         pageManager.Provider.SuppressSecurityChecks = false;
                }

    Thanks, 
    Nathalia.


8 posts, 1 answered