2014-03-04 85 views
3

如何在不使用提交按鈕的情況下保存表單(我在頁面上有另一個可用於運行保存代碼的觸發器)。該表單從來不會顯示給用戶,我只需要將其保存到營銷人員數據庫的Web表單中,除非有更明智的方式。以編程方式爲營銷商保存Sitecore Web表單

,我可以把它弄出來就好使用

Sitecore.Forms.Core.Data.FormItem form = 
Sitecore.Forms.Core.Data.FormItem.GetForm(formID); 

不過,我想

  1. 更改表單中的數據字段

(即類似)

form.Fields["Name"] = "Test name"; 
// this wont work, I dont know how to access the field's value before I store it. 
  1. 將表單數據保存在sitecore中營銷人員項目數據庫的Web表單中。

當我修改了字段後,我想保存它當然。

回答

6

這樣做的方法是首先獲取表單副本,使用AdaptedControlResult列表設置其字段並使用InsertForm保存它。這樣它就會正確地結束在WFFM的數據表中。

代碼中使用的customerInfo來自CartHelper,但它可以是任何東西。

代碼:

string formID = "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"; 
DataUri uri = new DataUri(new ID(formID)); 
Sitecore.Data.Items.Item formItem = Sitecore.Context.Database.GetItem(uri); 

// if the item exists, run through all the known fields and add the values from the customerinfo into them. 
if (formItem != null) 
{ 
    List<AdaptedControlResult> acrList = new List<AdaptedControlResult>(); 
    Sitecore.Data.Items.Item[] fieldItems = formItem.Axes.GetDescendants(); 

    foreach (Sitecore.Data.Items.Item fieldItem in fieldItems) //.Where(x => x.TemplateName == "Field")) 
    { 
     if (fieldItem.Name == "E-mail") 
     { 
     acrList.Add(new AdaptedControlResult(new ControlResult(fieldItem.Name, System.Web.HttpUtility.UrlDecode(customerInfo.Email), string.Empty) { FieldID = fieldItem.ID.ToString(), FieldName = fieldItem.Name, Value = System.Web.HttpUtility.UrlDecode(customerInfo.Email) }, true)); 
     continue; 
     } 

    // same for all other fields. 
    } 

    // save data 
    if (acrList.Count > 0) 
    { 
     AdaptedResultList arl = new AdaptedResultList(acrList); 

     try 
     { 
     Sitecore.Forms.Data.DataManager.InsertForm(formItem.ID, arl, Sitecore.Data.ID.NewID, null); 
     } 
     catch (Exception ex) 
     { 
     // Log data here 
     } 
    } 
} 
+1

老兄,如果我可以擁抱你我會... –

+0

這不再適用於Sitecore 8.0+。 'Sitecore.Forms.Data'不再有一個名爲'InsertForm'的方法。 – Paul

0

如果我理解你的問題,你在這裏混合了兩個原則。你想讓表單通過它自己的單獨提交例程 - 但它也看起來像你試圖寫自己的表單提交,使用服務器端代碼,永遠不會按照你描述的方式工作。

從頁面上的另一個控件提交WFM提交的最佳方法是讓它仍然使用表單上的「Submit」類型的按鈕,但是使用CSS來隱藏實際控件。它有一個獨特的CSS類可以用來隱藏它。然後 - 從頁面的其他區域控制提交,觸發隱藏控件的OnClick事件以獲取表單回發並保存內容。

您的代碼;我閱讀的方式;正在嘗試閱讀Sitecore表單項目,然後訪問該Sitecore項目的字段集合。這個字段集合與您的表單無直接關係,並且永遠不會保存網站用戶當前輸入的實際數據。

+0

我得到它的工作,但我是的我曾考慮渲染HTML,然後隱藏它有一個「虛擬」形式,只存在於服務器內存。 –

1

如果你正在嘗試將數據在數據庫編程提交給WFFM表,可以使用以下命令:

假設你有你的填充列表中的字段列表

public class WffmField 
    { 
     public string FieldName { get; set; } 
     public string FieldGuid { get; set; } 
     public string FieldValue { get; set; } 
    } 

場GUID是從Sitecore的的GUID:: enter image description here

這個類的

然後,您可以保存到數據庫WFFM:

// This should be populated with the data you want to send to the WFFM database 
var fields = new List<WffmField>(); 
var wffmDatabaseFields = fields.Select(GetWFFMDatabaseField).ToList(); 

Sitecore.Forms.Data.DataManager.InsertForm(
    formId: new Sitecore.Data.ID("<Form guid here>"), 
    fields: new AdaptedResultList(wffmDatabaseFields), 
    sessionID: AnalyticsTracker.SessionId, 
    data: null); 

希望這有助於!

+0

這已經被問:http://stackoverflow.com/questions/16578237/web-forms-for-marketers-submit-form-data-programmatically – aceanindita

+1

這個答案是如此古老,它不適用,並已在其他地方陳述 –

+0

這不再適用於Sitecore 8.0+。 'Sitecore.Forms.Data'不再有一個名爲'InsertForm'的方法。 – Paul

0

我最近對這個問題有類似的要求。 The answer above from Jesper Hoff如果您只需要將表單數據保存到WFfM配置的數據庫中,那麼該功能非常棒。然而,它沒有做的是兌現與表單相關的保存操作。因此,如果您想將提交內容記錄到SQL Server中,那就沒有問題,但如果您需要通過電子郵件發送或將它們傳輸到CRM,則無濟於事。我需要使用我正在編寫的代碼進行電子郵件發送工作,所以已經做了一些更深入的WFfM內部工作:

如果您希望基於代碼提交來遵守表單的配置保存操作,你需要對上面的代碼中三個主要變化:

  • 首先,你需要利用Sitecore.Form.Core.Submit.SubmitActionManager.Execute()方法,而不是Sitecore.Forms.Data.DataManager.InsertForm()。這似乎是WFfM在發回有效表單時呼叫的內容。
  • 其次,您需要獲取並配置一組爲您的WFfM設置的保存操作以進行定義。爲了以正確的格式獲取這些數據,需要一些雜耍。
  • 第三,您需要收集表格數據爲ControlResult對象不是AdaptedControlResult。您傳入的數據會自動打包,作爲Execute()調用的一部分。

忽略錯誤檢查爲清楚起見,我結束了這樣的代碼:

List<ControlResult> results = new List<ControlResult>(); 
results.Add(makeControlResult(_nameFieldID, "Name", "Bob Jones")); 
results.Add(makeControlResult(_emailFieldID, "Email", "[email protected]")); 
results.Add(makeControlResult(_messageFieldID, "Message", "Lorem ipsum. Dolor sit amet.")); 

Item formItem = Sitecore.Context.Database.GetItem(_yourFormID); 

SitecoreSimpleForm simpleForm = new SitecoreSimpleForm(formItem); 
var saveActionXml = simpleForm.FormItem.SaveActions; 
var actionList = Sitecore.Form.Core.ContentEditor.Data.ListDefinition.Parse(saveActionXml); 

List<ActionDefinition> actionDefinitions = new List<ActionDefinition>(); 
actionDefinitions.AddRange(actionList.Groups.SelectMany(x => x.ListItems).Select(li => new ActionDefinition(li.ItemID, li.Parameters) { UniqueKey = li.Unicid })); 

Sitecore.Form.Core.Submit.SubmitActionManager.Execute(_formID, results.ToArray(), actionDefinitions.ToArray()); 

在組保存的行動及其配置保存在Item您的形式,但你必須包裹將一個SitecoreSimpleForm對象中的項目拿出來。你得到的是XML,所以你需要用Sitecore.Form.Core.ContentEditor.Data.ListDefinition.Parse()方法解析它以獲取一組反序列化的Save Action對象。但是,它們不是SubmitActionManager的正確對象,因此您需要將它們投影到ActionDefinition對象中以便使用它們。

(注意保存操作對象中的XML解析後,你不是在一個平面列表,所以你需要使用像SelectMany()在使用前將其平整)

上面提到的makeControlResult()方法只是爲了使片段更清晰。它只是創建一個從數據有關表單字段ControlResult對象:

public ControlResult makeControlResult(string fieldID, string fieldName, string fieldValue) 
{ 
    return new ControlResult(fieldName, fieldValue, string.Empty) 
    { 
     FieldID = fieldID, 
     FieldName = fieldName, 
     Value = fieldValue, 
     Parameters = string.Empty 
    }; 
} 

取決於你如何讓你的數據的保留,你可能更願意使用一個變化的方法,在對方的回答產生這種數據。我也wrote up a bit more detail of how I arrived at this answer on my blog。這篇文章還包含了一個關於如何找回Save Actions可能產生的錯誤的片段 - 當你使用多個Save Actions時這可能很重要。

相關問題