Calling WorkflowManager.MessageWorkflow() when not authentic

Posted by Community Admin on 04-Aug-2018 05:10

Calling WorkflowManager.MessageWorkflow() when not authenticated

All Replies

Posted by Community Admin on 09-Jul-2012 00:00

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.

Posted by Community Admin on 11-Jul-2012 00:00

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

Posted by Community Admin on 12-Jul-2012 00:00

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

Posted by Community Admin on 12-Jul-2012 00:00

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

Posted by Community Admin on 29-Jul-2012 00:00

I'm now getting error on dynamicModuleManager.SaveChanges() after upgrade to 5.1. 
Can you please check this.

Posted by Community Admin on 10-Aug-2012 00:00

I too am getting an error on  dynamicModuleManager.SaveChanges(); in 5.1

Did you ever fix this??

Posted by Community Admin on 15-Aug-2012 00:00

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

Posted by Community Admin on 23-Aug-2012 00:00

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.


This thread is closed