Skip to content

Commit

Permalink
Added custom workflow action template
Browse files Browse the repository at this point in the history
  • Loading branch information
adoprog committed Mar 9, 2018
1 parent b95624d commit b85378d
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 66 deletions.
100 changes: 98 additions & 2 deletions Sitecore.Flow/Sitecore Flow.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@
<Name>Forms Editor</Name>
</items>
<items>
<Database>master</Database>
<Root>{9B170515-9510-4AFC-9C20-E84540C54CFB}</Root>
<SkipVersions>False</SkipVersions>
<Converter>
<ItemToEntryConverter>
Expand Down Expand Up @@ -228,6 +226,8 @@
</Include>
<Exclude />
<Name>Forms Action</Name>
<Database>master</Database>
<Root>{9B170515-9510-4AFC-9C20-E84540C54CFB}</Root>
</items>
<xfiles>
<Entries>
Expand All @@ -243,6 +243,102 @@
<Exclude />
<Name>Forms Editor Script</Name>
</xfiles>
<items>
<SkipVersions>False</SkipVersions>
<Converter>
<ItemToEntryConverter>
<Transforms />
</ItemToEntryConverter>
</Converter>
<Include>
<ItemNameFilter>
<Pattern />
<FilterSearchType>Simple</FilterSearchType>
</ItemNameFilter>
<ItemDateFilter>
<FilterType>CreatedFilter</FilterType>
<NotOlderThan />
<ActionDateTo />
<ActionDateFrom />
</ItemDateFilter>
<ItemDateFilter>
<FilterType>ModifiedFilter</FilterType>
<NotOlderThan />
<ActionDateTo />
<ActionDateFrom />
</ItemDateFilter>
<ItemPublishFilter>
<PublishDate />
<CheckWorkflow>True</CheckWorkflow>
</ItemPublishFilter>
<ItemTemplateFilter>
<Templates />
</ItemTemplateFilter>
<ItemUserFilter>
<FilterType>Created</FilterType>
<Accounts />
</ItemUserFilter>
<ItemUserFilter>
<FilterType>Modified</FilterType>
<Accounts />
</ItemUserFilter>
<ItemLanguageFilter>
<Languages />
</ItemLanguageFilter>
</Include>
<Exclude />
<Name>Templates</Name>
<Database>master</Database>
<Root>{1179F625-38B5-4D3A-8BE5-1BB781B394A0}</Root>
</items>
<items>
<SkipVersions>False</SkipVersions>
<Converter>
<ItemToEntryConverter>
<Transforms />
</ItemToEntryConverter>
</Converter>
<Include>
<ItemNameFilter>
<Pattern />
<FilterSearchType>Simple</FilterSearchType>
</ItemNameFilter>
<ItemDateFilter>
<FilterType>CreatedFilter</FilterType>
<NotOlderThan />
<ActionDateTo />
<ActionDateFrom />
</ItemDateFilter>
<ItemDateFilter>
<FilterType>ModifiedFilter</FilterType>
<NotOlderThan />
<ActionDateTo />
<ActionDateFrom />
</ItemDateFilter>
<ItemPublishFilter>
<PublishDate />
<CheckWorkflow>True</CheckWorkflow>
</ItemPublishFilter>
<ItemTemplateFilter>
<Templates />
</ItemTemplateFilter>
<ItemUserFilter>
<FilterType>Created</FilterType>
<Accounts />
</ItemUserFilter>
<ItemUserFilter>
<FilterType>Modified</FilterType>
<Accounts />
</ItemUserFilter>
<ItemLanguageFilter>
<Languages />
</ItemLanguageFilter>
</Include>
<Exclude />
<Name>Workflow Insert Option Rule</Name>
<Database>master</Database>
<Root>{10377E02-FDD5-42FB-A20C-136A401104E4}</Root>
</items>
</Sources>
<Converter>
<TrivialConverter>
Expand Down
134 changes: 70 additions & 64 deletions Sitecore.Flow/Workflow/Actions/ItemFieldsToFlow.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,71 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Sitecore.Configuration;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Flow.Helpers;
using Sitecore.Links;
using Sitecore.Sites;
using Sitecore.Workflows.Simple;

namespace Sitecore.Flow.Workflow.Actions
{
public class ItemFieldsToFlow
{
public void Process(WorkflowPipelineArgs args)
{
Item dataItem = args.DataItem;
if (args.DataItem == null)
{
return;
}

var url = args.ProcessorItem.InnerItem["Parameters"];
var requestor = new Requestor();
if (dataItem != null && dataItem.Parent != null)
{
var json = GetFieldsJson(dataItem);
Task.Run(() => requestor.PostRequest((string)url, json));
}
}

private static string GetFieldsJson(Item item)
{
var sb = new StringBuilder();
sb.Append("{");
var fieldDescriptors = new List<string>();
var urlOptions = new UrlOptions();
urlOptions.AlwaysIncludeServerUrl = false;

// TODO: Take this value from action parameters
urlOptions.Site = SiteContext.GetSite("website");
var url = LinkManager.GetItemUrl(item, urlOptions);
url = url.Replace(" ", "%20");
url = Settings.GetSetting("Sitecore.Flow.Actions.ServerUrl") + url;
fieldDescriptors.Add($" \"ItemUrl\" : \"{url}\" ");

foreach (Field field in item.Fields)
{
if (field.Name.StartsWith("__"))
{
continue;
}

fieldDescriptors.Add($" \"{field.Name}\" : \"{HttpUtility.JavaScriptStringEncode(field.Value)}\" ");
}

sb.Append(fieldDescriptors.Aggregate((i, j) => i + "," + j));
sb.Append("}");
return sb.ToString();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Sitecore.Configuration;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Flow.Helpers;
using Sitecore.Links;
using Sitecore.Sites;
using Sitecore.Workflows.Simple;

namespace Sitecore.Flow.Workflow.Actions
{
public class ItemFieldsToFlow
{
private const string HttpPostUrlField = "HTTP POST URL";
private const string ContextSiteField = "Context Site";

public void Process(WorkflowPipelineArgs args)
{
Item dataItem = args.DataItem;
if (args.DataItem == null)
{
return;
}

var url = args.ProcessorItem.InnerItem[HttpPostUrlField];
var contextSite = args.ProcessorItem.InnerItem[ContextSiteField];
var requestor = new Requestor();
if (dataItem != null && dataItem.Parent != null)
{
var json = GetFieldsJson(dataItem, contextSite);
Task.Run(() => requestor.PostRequest((string)url, json));
}
}

private static string GetFieldsJson(Item item, string contextSite)
{
var sb = new StringBuilder();
sb.Append("{");
var fieldDescriptors = new List<string>();
var urlOptions = new UrlOptions();
urlOptions.AlwaysIncludeServerUrl = false;
if (!string.IsNullOrEmpty(contextSite))
{
urlOptions.Site = SiteContext.GetSite(contextSite);
}

var url = LinkManager.GetItemUrl(item, urlOptions);
url = url.Replace(" ", "%20");
url = Settings.GetSetting("Sitecore.Flow.Actions.ServerUrl") + url;
fieldDescriptors.Add($" \"ItemUrl\" : \"{url}\" ");

foreach (Field field in item.Fields)
{
if (field.Name.StartsWith("__"))
{
continue;
}

fieldDescriptors.Add($" \"{field.Name}\" : \"{HttpUtility.JavaScriptStringEncode(field.Value)}\" ");
}

sb.Append(fieldDescriptors.Aggregate((i, j) => i + "," + j));
sb.Append("}");
return sb.ToString();
}
}
}

0 comments on commit b85378d

Please sign in to comment.