From b85378d20a5ffe77207accd127c36f4bce0516a1 Mon Sep 17 00:00:00 2001 From: Alexander Doroshenko Date: Fri, 9 Mar 2018 15:20:37 +0200 Subject: [PATCH] Added custom workflow action template --- Sitecore.Flow/Sitecore Flow.xml | 100 ++++++++++++- .../Workflow/Actions/ItemFieldsToFlow.cs | 134 +++++++++--------- 2 files changed, 168 insertions(+), 66 deletions(-) diff --git a/Sitecore.Flow/Sitecore Flow.xml b/Sitecore.Flow/Sitecore Flow.xml index dac4bc9..8a63289 100644 --- a/Sitecore.Flow/Sitecore Flow.xml +++ b/Sitecore.Flow/Sitecore Flow.xml @@ -182,8 +182,6 @@ Forms Editor - master - {9B170515-9510-4AFC-9C20-E84540C54CFB} False @@ -228,6 +226,8 @@ Forms Action + master + {9B170515-9510-4AFC-9C20-E84540C54CFB} @@ -243,6 +243,102 @@ Forms Editor Script + + False + + + + + + + + + Simple + + + CreatedFilter + + + + + + ModifiedFilter + + + + + + + True + + + + + + Created + + + + Modified + + + + + + + + Templates + master + {1179F625-38B5-4D3A-8BE5-1BB781B394A0} + + + False + + + + + + + + + Simple + + + CreatedFilter + + + + + + ModifiedFilter + + + + + + + True + + + + + + Created + + + + Modified + + + + + + + + Workflow Insert Option Rule + master + {10377E02-FDD5-42FB-A20C-136A401104E4} + diff --git a/Sitecore.Flow/Workflow/Actions/ItemFieldsToFlow.cs b/Sitecore.Flow/Workflow/Actions/ItemFieldsToFlow.cs index 04e89d3..85390f1 100644 --- a/Sitecore.Flow/Workflow/Actions/ItemFieldsToFlow.cs +++ b/Sitecore.Flow/Workflow/Actions/ItemFieldsToFlow.cs @@ -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(); - 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(); + 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(); + } + } } \ No newline at end of file