diff --git a/src/Altinn.App.Core/Internal/Data/InstanceDataUnitOfWork.cs b/src/Altinn.App.Core/Internal/Data/InstanceDataUnitOfWork.cs
index d763eed78..c5e374ad0 100644
--- a/src/Altinn.App.Core/Internal/Data/InstanceDataUnitOfWork.cs
+++ b/src/Altinn.App.Core/Internal/Data/InstanceDataUnitOfWork.cs
@@ -448,6 +448,14 @@ DataElementChange change
new MemoryAsStream(bytes),
authenticationMethod: GetAuthenticationMethod(change.DataType)
);
+
+ if (change.Metadata is not null)
+ {
+ dataElement.Metadata = [.. change.Metadata];
+ change.Lock = true;
+ await _dataClient.Update(Instance, dataElement);
+ }
+
// Update caches
_binaryCache.Set(dataElement, bytes);
change.DataElement = dataElement; // Set the data element so that it can be referenced later in the save process
diff --git a/src/Altinn.App.Core/Models/DataElementChanges.cs b/src/Altinn.App.Core/Models/DataElementChanges.cs
index 44966e348..1abc0eee3 100644
--- a/src/Altinn.App.Core/Models/DataElementChanges.cs
+++ b/src/Altinn.App.Core/Models/DataElementChanges.cs
@@ -42,6 +42,11 @@ internal DataElementChange(ChangeType type, DataType dataType, string contentTyp
ContentType = contentType;
}
+ ///
+ /// The metadata of the data element
+ ///
+ private List? _metadata = [];
+
///
/// The type of update: Create, Update or Delete
///
@@ -67,6 +72,34 @@ internal DataElementChange(ChangeType type, DataType dataType, string contentTyp
/// The contentType of an element in storage
///
public string ContentType { get; }
+
+ ///
+ /// The metadata of the data element
+ ///
+ internal IReadOnlyCollection? Metadata => _metadata;
+
+ ///
+ /// If true, no more metadata can be added
+ ///
+ internal bool Lock { get; set; }
+
+ ///
+ /// Add metadata to a created data element
+ ///
+ public void AddMetadata(string key, string value)
+ {
+ if (Type != ChangeType.Created)
+ {
+ throw new InvalidOperationException("Metadata can only be added to created data elements");
+ }
+
+ if (Lock)
+ {
+ throw new InvalidOperationException("Metadata already locked");
+ }
+ _metadata ??= [];
+ _metadata.Add(new KeyValueEntry { Key = key, Value = value });
+ }
}
///
diff --git a/test/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt b/test/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
index eddebdb6f..51f097d03 100644
--- a/test/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
+++ b/test/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
@@ -3961,6 +3961,7 @@ namespace Altinn.App.Core.Models
public Altinn.App.Core.Models.DataElementIdentifier DataElementIdentifier { get; }
public Altinn.Platform.Storage.Interface.Models.DataType DataType { get; }
public Altinn.App.Core.Models.ChangeType Type { get; }
+ public void AddMetadata(string key, string value) { }
}
public sealed class DataElementChanges
{