HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback


Note

Optimizely Forms is only supported by MVC-based websites and HTML5-compliant browsers.

Optimizely Forms uses DDS as its default storage mechanism. This may cause performance-related issues in special cases. You can base an alternative data storage scheme on MSSQL or another data provider to try different solutions. This topic uses MongoDB as an alternative storage provider, but you can use any data storage system.

## Comparison between DDS and MongoDB

1539


To implement a MongoDB data storage approach, first prepare your environment for Mongo. See <https://docs.mongodb.com>.

## Required NuGet packages

  • _MongoDB.Bson_

  • _MongoDB.Driver_

  • _MongoDB.Driver.Core_

  • _EPiServer.Forms.Core_ 4.6.1 (or higher)

## PermanentStorage class

`PermanentStorage` is the base class for data storage. This class exposes the necessary APIs for developers to manipulate form data with their data storage.

  1. Create a new class inheriting the `PermanentStorage` class. Put the `ServiceConfiguration` attribute on top as shown in the following example.


  1. Within the newly-created class constructor, set up initial storage information, such as connection string, configuration, and so on.

  2. Override the five functions below.

    • `SaveToStorage(FormIdentity formIden, Submission submission)` – This function saves data at the first step of a form. This may also be the last step, if a form only has one step. Using this method, based on these two parameters, developers can save data into database.

      • `formIden` – Contains form information, such as form id or language.

      • `submission` – Contains form submission data.

    • `UpdateToStorage(Guid formSubmissionId, FormIdentity formIden, Submission submission)` – This function is for saving data at the next steps or updating data at previous ones. Within this method, based on these two parameters, developers can update data in the database.

      • `submission` – Contains form submission data.

      • `formIden` – Contains form information, such as form id or language.

    • `LoadSubmissionFromStorage(FormIdentity formIden, DateTime beginDate, DateTime endDate, bool finalizedOnly = false)` – This function loads form submission data from the database within a specific time period and the status finalized.

    • `LoadSubmissionFromStorage(FormIdentity formIden, string[] submissionIds)` – Loads form submission data only based on the ids of the submission.

    • `Delete(FormIdentity formIden, string submissionId)` – Deletes a form submission based on submission Id.

### Example of using MongoDB as a data storage mechanism

Use the following example to replace DDS with MongoDB for data manipulation. The `MongoDbPermanentStorage` class implements the storage.

MongoDB uses the concept of "collection" as a table in Microsoft SQL. Each form has one collection, which stores form submission data. The collection also supports indexing, which significantly improves the performance of data queries. MongoDB's storage details are below.

  • `MongoDbPermanentStorage()` – Set the constructor of the storage. Within the constructor, you set up a connection string for the database connection, database name for data storage, and form configurations.

  • `GetFormCollection (FormIdentity formIden)` – Creates a collection for a specific form, based on form id. If a form was previously associated with a collection, it is returned and no new collection is created.

  • `IsCollectionExisted(IMongoDatabase database, string collectionName)` – Checks whether a collection exists in the database based on its name.

  • `SaveToStorage(FormIdentity formIden, Submission submission)` – Gets the collection associated with form submission data as a new item, and then saves it to the MongoDB.

  • `UpdateToStorage(Guid formSubmissionId, FormIdentity formIden, Submission submission)` – Updates the collection associated with the forms update submission data previously stored into it.

  • `LoadSubmissionFromStorage(FormIdentity formIden, DateTime beginDate, DateTime endDate, bool finalizedOnly = false)` – Loads submission from the collection associated with the form during a time  period.

  • `LoadSubmissionFromStorage(FormIdentity formIden, string[] submissionIds)` – Loads submission from the collectionassociated with the form, based on the list of submission ids.

  • `Delete(FormIdentity formIden, string submissionId)` – Deletes a submission previously stored in the collection.

### Beta version of MongoDBPermanentStorage

Sample code



## Configure the new database provider

The following example sets up the MongoDB configuration in the _forms.config_ file:

  1. Use the **\<add>** element to add a storage provider.

  2. Enter values for the properties below:

    • `name` – For example, _MongoDBPermanentStorage_.

    • `type` – The full namespace of your project's data storage and namespace. Use a comma to separate two values.

    • `connectionUri` – Database connection string.

    • `databaseName` – Your database name.

  3. Use new provider as the `defaultProvider` of the storage.

This is a sample _forms.config_ file.