2015-11-05 27 views
3

我创建了一个自定义Save Action,它将WFFM字段值写入第三方服务。自定义Save Action使用开箱即用的FieldMappings编辑器,以便内容编辑器可以指定哪些字段映射到哪些属性发送到服务。WFFM保存动作从编辑器获取字段映射

我有它的工作,所以所有的属性出现在编辑器中供用户选择相关的字段。

enter image description here

的问题是,我无法找到如何在Save ActionExecute方法的点得到这些映射。我已经对现有的字段Save Action进行了反编译,因为它也使用了MappingField编辑器,但它最终会忽略映射。

public class SaveToSalesForceMarketingCloud : ISaveAction 
{ 
    public string Mapping { get; set; } 

    public void Execute(ID formid, AdaptedResultList fields, params object[] data) 
    { 
     FormItem formItem = Sitecore.Context.Database.GetItem(formid); 
     if (formItem == null) 
      return; 

     string mappingXml = Mapping; 

     // Using the Property Name does not return the Mapped Field 
     var emailAddressField = fields.GetEntryByName("Email address"); 
     // Using the actual name of the Field on the Form returns the Field 
     var emailField = fields.GetEntryByName("Email"); 
    } 
} 

任何人都知道如何获得映射?

+0

当你说你不能得到映射,你是什么意思,'映射'在空执行方法是空的/空? – jammykam

+0

嗨鉴,通过编辑器对话框创建的映射。在其他SaveActions上看到它后,我尝试添加Mapping属性,并且可以解析XML。 –

回答

4

的映射存储在你的表格,然后将其填充到您定义的Mapping财产的保存操作字段中的键/值对。

检查您的表单的Save Field,您会注意到该字符串的格式类似于<mapping>key=value1|key=value2</mapping>。这是您在保存操作中可用的字符串值。你需要自己处理它,WFFM不会为你安排任何东西。为了访问映射,您使用Sitecore实用方法:

NameValueCollection nameValueCollection = StringUtil.ParseNameValueCollection(this.Mapping, '|', '='); 

这使您可以访问键/值对。然后,您需要枚举这些字段或提交的表单数据(如适用)以填充对象以进行进一步操作。

假设密钥在WFFM字段ID和价值是映射到外地,类似于此

foreach (AdaptedControlResult adaptedControlResult in fields) 
{ 
    string key = adaptedControlResult.FieldID; //this is the {guid} of the WFFM field 
    if (nameValueCollection[key] != null) 
    { 
     string value = nameValueCollection[key]; //this is the field you have mapped to 
     string submittedValue = adaptedControlResult.Value; //this is the user submitted form value 
    } 
} 

东西拿在Sitecore.Forms.Custom看看Sitecore.Form.Submit.CreateItem对于类似的操作和字段映射编辑器的示例在哪里使用。

+0

感谢Kam,看起来像使用SaveActions属性来获取映射是前进的方向 - 耻辱它不是一个更有用的格式 –

+0

创建一个帮助方法来提取映射并将值提交到另一个'List'属性中,这使得更多的操作可重复使用并更容易访问。由于动态性,无法获得强类型访问。 – jammykam

2

我认为它通过将字段与Save Action类中的公共属性进行匹配而得到连接。

因此,对于你的例子:

public string EmailAddress { get; set; } 
public string ConfirmEmailAddress { get; set; } 
public string Title { get; set ;} 
etc.. 
+0

这适用于此映射的字段Id。唯一的缺点是它的属性固定列表 –

相关问题