Reputation: 7340
Is there a way that I can programmatically approve a content item in Sitecore?
Upvotes: 3
Views: 1840
Reputation: 494
Here is a blog that (quite descriptively) tells you how to change workflow state for items programmatically: http://www.cognifide.com/blogs/sitecore/changing-workflow-state-of-sitecore-items-programmatically/ Hope this helps!
Upvotes: 0
Reputation: 1119
Here's what you might want to do. Setup a checkbox per site that will allow admins to chose whether they want to use Awaiting Approval or not. An admin will simply check a checkbox to Skip Approval (Draft to Approved) or leave unchecked to maintain workflow (Draft>AA(Approve or reject)>Approved):
Add this code to your SkipAction class:
public class SkipAction
{
bool isSiteSettingsReached = false;
/// <summary>
/// This method facilitates grabbing of the skipapproval setting from setting>sectionsettings
/// </summary>
/// <param name="item"></param>
/// <param name="fieldId"></param>
/// <returns></returns>
protected bool GetSkipSetting(Item item, string fieldId)
{
bool result = false;
if (item.ID.Equals(null) || item.ID.Equals(ItemIDs.ContentRoot) || item.ID.Equals(ItemIDs.RootID))
{
result=false;
}
if (isSkipApprovalChecked(item, fieldId))
{
result = true;
}
if (!isSkipApprovalChecked(item, fieldId))
{
result = false;
}
if (!isSiteSettingsReached)
{
result=GetSkipSetting(item.Parent, fieldId);
}
return result;
}
/// <summary>
/// This method is initialized when skipaction is called
/// </summary>
/// <param name="args"></param>
public void Process(WorkflowPipelineArgs args)
{
var contentItem = args.DataItem;
var actionItem = args.ProcessorItem.InnerItem;
var parameters = WebUtil.ParseUrlParameters(actionItem["parameters"]);
var nextStateId = parameters["nextstateid"];
var skipFieldId = parameters["skipfieldid"];
if(nextStateId.IsNullOrEmpty() ||
skipFieldId.IsNullOrEmpty())
return;
bool skip = GetSkipSetting(contentItem, skipFieldId);
Sitecore.Data.Database web = Sitecore.Configuration.Factory.GetDatabase("web");
Sitecore.Data.Database production = Sitecore.Configuration.Factory.GetDatabase("production");
if (skip)
{
contentItem.PerformTransition(nextStateId, "auto skip");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
publishTo(web, contentItem);
publishTo(production, contentItem);
}
contentItem.Locking.Unlock();
args.AbortPipeline();
}
}
/// <summary>
/// this method is used to publish to environ by param
/// </summary>
/// <param name="targetToPublish"></param>
/// <param name="item"></param>
private void publishTo(Sitecore.Data.Database targetToPublish, Item item)
{
Sitecore.Data.Database sourceDB = Sitecore.Configuration.Factory.GetDatabase("master");
//// set publish options
Sitecore.Publishing.PublishOptions myOptions = new Sitecore.Publishing.PublishOptions(
sourceDB,
targetToPublish,
Sitecore.Publishing.PublishMode.Smart,
item.Language,
DateTime.Now);
myOptions.RootItem = item;
myOptions.Deep = false;
Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(myOptions);
Sitecore.Jobs.Job myPublishJob = publisher.PublishAsync();
myPublishJob.Start();
}
/// <summary>
/// This method check the actual skip approval field in settings>sectionsettings
/// </summary>
/// <param name="item"></param>
/// <param name="fieldId"></param>
/// <returns></returns>
public bool isSkipApprovalChecked(Sitecore.Data.Items.Item item, string fieldId)
{
if (item.HasChildren)
{
Sitecore.Data.Items.Item settingsItem = item.Axes.GetChild("Settings");
if (settingsItem == null)
{
return false;
}
else
{
isSiteSettingsReached = true;
Sitecore.Data.Items.Item sectionsettingsItem = settingsItem.Axes.GetChild("SectionSettings");
if (sectionsettingsItem.DisplayName == "SectionSettings" && sectionsettingsItem[fieldId] == "1")
{
return true;
}
else
{
return false;
}
}
}
return false;
}
}
}
Go Back to your SkipOver action, add parameters like this (nextstateid = Approval State id, skipfieldid = item id of skipapproval checkbox in your template):
Parameters [shared]: nextstateid={D0F57FA8-F472-4332-89D9-E429CD111E50}&skipfieldid={XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
Thanks, c
Upvotes: 3