Calling WorkflowManager.MessageWorkflow() when not authenticated
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.
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();
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();
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();
I'm now getting error on dynamicModuleManager.SaveChanges() after upgrade to 5.1.
Can you please check this.
I too am getting an error on dynamicModuleManager.SaveChanges(); in 5.1
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,
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;