0

I need to develop a page to show a list of sitecore forms tagged this login user and then allow this user to export data as csv

we do not want user to go to forms and export data there so we need a way to access forms list and forms data without going to xdb, either using library and rest api

3 Answers 3

0

There is no OOTB API for Sitecore Forms to export data to given form.

There is Forms Client API but it only gives you list of form fields not the values -> https://doc.sitecore.com/xp/en/developers/102/sitecore-experience-manager/work-with-the-form-client-api.html

You can build your own API or extend layout service. Own API is recommended approach.

You can do it similarly to this example:

  1. Create a class which inherits from ApiController

    public class FormExportApiController : ApiController
    
  2. You need to create your method which represent api call :

    [System.Web.Http.HttpGet]
    [Route("formexport/{formId}")]
    public HttpResponseMessage ExportFormEntries(string formId)
    
  3. You need to register the ApiController

    public class Register : IServicesConfigurator
         {
             public void Configure(IServiceCollection serviceCollection)
             {
    
                 serviceCollection.Replace(ServiceDescriptor.Transient(typeof(FormExportApiController ),
                     typeof(FormExportApiController )));
             }
         }
    
  4. You need to register in the configuration :

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <services>
          <configurator 
             type="yourNameSpace.ServicesConfigurator,yourNamespace" />
        </services>
       </sitecore>
     </configuration>
    
  5. To call the api you need to call it in this way

    https://yourhostname/formexport/formId

You should check Sitecore.ExperienceForms.Data.SqlServer.SqlFormDataProvider from Sitecore.ExperienceForms.Data.SqlServer assembly located in bin folder on how to get values programatically for your API and incorporate them into your controller / method...

enter image description here

3
  • Thanks very much! does Headless Services provide some type of functions to get list of forms?
    – John
    Commented Feb 19 at 14:37
  • Great Thanks! How do I get a list of forms and their metadata without get all children from /sitecore/Forms?
    – John
    Commented Feb 19 at 14:51
  • I guess there is no other option then traversing all child items of /sitecore/forms node which is based on Form template @John. I would create an API also for this. So first getting list of forms and then api to export data Commented Feb 20 at 10:42
0

When you create a form in Sitecore, it works like an item and all its fields are the child items. Like this.

enter image description here

So I guess you will be able to get this using The RESTful API for the ItemService

You can use this to retrieve the children of Forms items like this.

/item/{id}/children?database&language&version&includeStandardTemplateFields&includeMetadata&fields

And for your next question where you want to export the data of forms, you can create a controller that will be inherited with ServicesApiController and create a get method to get the data. The code will look like this.

 ExportDataResult exportDataResult = this._exportDataProvider.Export(exportDataParameters.FormId, exportDataParameters.StartDate, exportDataParameters.EndDate);
          string str;
          if (exportDataResult == null)
          {
            httpResponseMessage1.StatusCode = HttpStatusCode.NoContent;
            str = "nodata";
          }
          else if (string.IsNullOrEmpty(exportDataResult.Content))
          {
            httpResponseMessage1.StatusCode = HttpStatusCode.NoContent;
            httpResponseMessage1.ReasonPhrase = Translate.Text("The date range is invalid. Please select a date range that is within the range of the list.");
            str = "nodatainrange";
          }
          else
          {
            httpResponseMessage1.Content = (HttpContent) new StringContent(exportDataResult.Content);
            httpResponseMessage1.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            httpResponseMessage1.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
              FileName = exportDataResult.FileName
            };
            str = exportDataParameters.Token;
          }
          if (HttpContext.Current != null)
            HttpContext.Current.Response.AppendCookie(new HttpCookie("fileDownloadToken" + exportDataParameters.Token, str));
        }

For more details on this you may refer to this thread.

Get all the Sitecore Form's saved data in code

Hope this helps.

0

As per my understanding there is a Sitecore API that the Forms Export : It would be something like this : When Date range filter is not given https://mysite.dev.local/sitecore/api/ssc/forms/exportdata?formId=%7BEFF92793-237E-45AF-A780-6561E49A105F%7D&isDateRange=false&token=1708350130310

When Date range filter is given and we have a start and end date : https://mysite.dev.local/sitecore/api/ssc/forms/exportdata?formId=%7BEFF92793-237E-45AF-A780-6561E49A105F%7D&isDateRange=true&startDate=2024-01-31T18%3A30%3A00.000Z&endDate=2024-02-14T18%3A29%3A59.000Z&token=1708350443983

This can be used to export the data of the Forms.

If you want to know the exact URL, Go to the Forms builder, select a form, click on export and Download. At the same time open your network tab and check the URL it builds. You can get the required URL.

I think the only requirement would be the User should be either Administrator or having this role "sitecore\Forms Data Administrator"

But to show the list of sitecore forms tagged to the login user I think you need to define your custom API

Hope this helps!

3
  • can I use Headless Services to get a list of forms?
    – John
    Commented Feb 19 at 14:33
  • how do you get that token?
    – John
    Commented Feb 19 at 14:33
  • Actually token is a unique value. It is currently populated from the ExportData.js file C:\inetpub\wwwroot\{hostname}\sitecore\shell\client\Applications\FormsBuilder\Dialogs\ExportDataDialogWindow as options.token = new Date().getTime(); You can use any combination of numbers to get unique value. You can use Headless Services to get the list of forms. I have not done that. Else you can call Sitecore Services Client itself to create custom endpoints to retrieve form information Commented Feb 19 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.