Episerver forms - Overwriting submission data

Vote:
 

Hello, Iam trying to overwrite input values on a submission on the formssubmitting event. This is my code:

  [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    [InitializableModule]
    public class EpiserverFormsInitializationModule : IConfigurableModule
    {
        private Injected _formRepository;
        private Injected _contentLoader;
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
            context.Container.Configure(ConfigureContainer);
        }

        private static void ConfigureContainer(ConfigurationExpression container)
        {
            container.For().Use();
        }

        public void Initialize(InitializationEngine context)
        {
            FormsEvents.Instance.FormsSubmitting += Instance_FormsSubmitting;
        }

        private void Instance_FormsSubmitting(object sender, FormsEventArgs e)
        {
            var request = HttpContext.Current.Request;
            var pageReference = UrlResolver.Current.Route(new UrlBuilder(request.UrlReferrer)).ContentLink;
            var submission = e as FormsSubmittingEventArgs;
            IContent content = _contentLoader.Service.Get(e.FormsContent.ContentLink);
            ILocalizable localizable = content as ILocalizable;
            if (content == null || localizable == null || submission == null)
            {
                return;
            }
            FormIdentity formsId = new FormIdentity(e.FormsContent.ContentGuid, localizable.Language.Name);
            var friendlyNameInfos = _formRepository.Service.GetDataFriendlyNameInfos(formsId);
            var friendlyPageId = friendlyNameInfos.FirstOrDefault(x => x.FriendlyName == "PageID");
            if (friendlyPageId != null && !friendlyPageId.FriendlyName.IsNullOrWhiteSpace())
            {
                var pageId = submission.SubmissionData.Data.FirstOrDefault(x => x.Key == friendlyPageId.ElementId);
                submission.SubmissionData.Data.Remove(pageId);
                pageId = new KeyValuePair(pageId.Key, pageReference.ID);
                submission.SubmissionData.Data.Add(pageId);

            }
        }

        public void Uninitialize(InitializationEngine context)
        {
        }
    }

This code works and replaces the value of the in this case "pageID" input field that i have in my form. When i export the submission forms from episerver edit i get a list that represents the new value correctly. But in the "Forms submissions" tab in episerver edit in a form the view is empty. 

If i remove the code that adds a new keypair value and post my form again the view is full again and the forms where i changed the "pageID" are also there with the new value. 

I need a better way to go about this because i think iam missing something, thanks!

#150936
Jul 04, 2016 11:10
Vote:
 

Found the problem :) 

pageId = new KeyValuePair<string, object>(pageId.Key, pageReference.ID);



should have been 

pageId = new KeyValuePair<string, object>(pageId.Key, pageReference.ID.ToString());



Database was not expecting an int, its always the little things that get you..

#150951
Edited, Jul 05, 2016 10:41
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.