Last modified: Oct 2, 2024

Prefilling data using custom code

How to code custom prefill of data in an app.

Altinn apps enable prefill of an instance with custom data, whether it is the result of an API call, calculations done under instantiation or other logic.

    In version 7 the way to do custom code instantiation has changed. We now use an dependency injection based approach instead of overriding methods. If you previously used to place your custom code in the DataCreation method in the _ InstantiationHandler.cs_ class you will see that it’s mostly the same.

    1. Create a class that implements the IInstantiationProcessor interface found in the Altinn.App.Core.Features namespace.
      You can name and place the file in any folder you like within your project, but we suggest you use meaningful namespaces like in any other .Net project. The example below populates the field Bruker.FulltNavn in the model Datamodell with the value “Test Testesen”.
      using System.Collections.Generic;
      using System.Threading.Tasks;
      using Altinn.App.Core.Features;
      using Altinn.App.Models;
      using Altinn.Platform.Storage.Interface.Models;
      
      public class Instantiation: IInstantiationProcessor
      {
          public async Task DataCreation(Instance instance, object data, Dictionary<string, string> prefill)
          {
              if (data.GetType() == typeof(Datamodell))
              {
                  Datamodell skjema = (Datamodell)data;
      
                  Bruker b = new Bruker();
                  b.Navn = new Name();
                  b.FulltNavn = "Test Testesen";
      
                  skjema.Bruker = b;
              }
      
              await Task.CompletedTask;
          }
      }
      
    2. Register you custom implementation in the Program.cs class
      services.AddTransient<IInstantiationProcessor, Instantiation>();
      
      This ensures your custom code is known to the application and that it will be executed.

    This is implemented in the method DataCreation in the file InstantiationHandler.cs that can be found in the application repo under App/logic.

    The example below populates the field Bruker.FulltNavn in the model Datamodell with the value “Test Testesen”.

    public async Task DataCreation(Instance instance, object data)
    {
        if (data.GetType() == typeof(Datamodell))
        {
            Datamodell model = (Datamodell)data;
            model.Bruker.FulltNavn = "Test Testesen";
        }
    }
    

    Replace Data model with the name on the C# class that has been generated based on the xsd uploaded to Altinn Studio. If you use a suitable code editor you will be able to define fields to be populated using intellisense.

    Keep in mind that if you have complex types in your model, these will need to be instantiated before you can assign a value to one of the type’s sub elements. See the example below where we assume that Bruker and Name are separate C# classes.

    public async Task DataCreation(Instance instance, object data)
    {
        if (data.GetType() == typeof(Datamodell))
        {
            Datamodell model = (Datamodell)data;
            Bruker b = new Bruker();
            b.Navn = new Name();
            b.Navn.FulltNavn = "Test Testesen";
        }
    }