I have a content item the has a field with a library of files. I created a separate content item to store the metadata for the files, which includes an integer field called SortOrder that is used for controlling the order of images on the page.
To make the sorting faster at the client's request, I created a page for admins with a jQuery sortable grid of the images that enables drag-and-drop ordering. This works very well by updating the metadata each time an image is dragged.
The problem is that a metadata entry isn't automatically created each time a new image is dragged into the library, so in this case there is no metadata to update. I can manually create the content item, but there doesn't seem to be a way in the API to tag it as belonging to a particular library item.
Hopefully someone can help me find a way using the 2SXC WebAPI or c# code to add metadata to any library item that doesn't exist. I see in the database that the entities are linked in the ToSIC_EAV_Entities using the AssignmentObjectTypeID and KeyString fields, but I would prefer to not make direct database changes. These changes also seem to require clearing the cache and restarting the application pool to be reflected in the UI.
Based on your pointers, I was able to create a solution using a custom WebAPI:
var contentTypeName = "PhotoDetails";
var userName = Dnn.User.Username;
var fileId = metadata.FileId.ToString();
MetadataFor target = new MetadataFor()
{
TargetType = 10, // hardcoded based on other content item metadata for no until further understood
KeyString = "file:" + fileId // file number comes from dnn's file table
};
var values = new Dictionary<string, object>()
{
{"Title", metadata.Title},
{"SortOrder", (fileId+99)}, // make new images appear at the end by adding 99 to the fileid so they stay in the upload order
{"IsPrimary", false} // image is not a primary photo by default
};
App.Data.Create(contentTypeName, values, userName, target);
Thanks again for the advice!