1

I need to create entity object programmatically and fill it with data. One field needs to be of a file type. So I managed to create entity without file, upload file in ADAM using this sample of code. However it seems that I didn't bind it as it is binded when file is uploaded manually. When a file is uploaded to entity field manually, you can see content like file:421 .../asdf.docx. However when I repeat code sample from link above, field contains that file available to choose and already uploaded, but field value is null. IFile.Url seems to write correct data via App.Data.Update method, but no id is displayed in admin panel.

Dictionary<string, object> fileDict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) {
        { "File", file.Url }
    }; // file is ToSic.Sxc.Adam.IFile, returned by SaveInAdam

App.Data.Update(entityObj.EntityId, fileDict); // entityObj is ToSic.Eav.Data.IEntity, returned by App.Data.Create

I wonder if that's going to have some bad conscequences if it has no binding like manual upload and how to do that correctly?

  • Your question and my question have one big thing in common. Its very difficult to understand what the object needs to be, or how to properly cast or create it, to have it store without errors AND correctly. I wish there was more guidance and insight here. https://stackoverflow.com/questions/64762167/is-there-a-way-clone-one-or-many-entities-records-in-code – Jeremy Farrance Nov 26 '20 at 02:45

1 Answers1

1

To add files, you actually add them to a field (so that the UI can then offer them as if they were added by a normal user). Check out https://docs.2sxc.org/api/dot-net/ToSic.Sxc.Dnn.Web.IDynamicWebApi.html#ToSic_Sxc_Dnn_Web_IDynamicWebApi_SaveInAdam_System_String_System_IO_Stream_System_String_System_String_System_Nullable_System_Guid__System_String_System_String_

BTW: Best check out MobiusForms to see how it's done.

And I forgot to mention: here the docs https://docs.2sxc.org/web-api/custom-web-api-dotnet-saveinadam.html

To further explain:

  1. SaveInAdam will place the file in the ADAM folder of the item. Since it could be that many files are added, it's just assuming that the field will be the library type - which doesn't store anything in the entity, it just automatically finds the folder that belongs to the field.
  2. If you wish to not use the library feature but actually just the single-field with link, then you must also save the term like "file:74" into the value of the field.
iJungleBoy
  • 5,325
  • 1
  • 9
  • 21
  • That's exactly the way I did it. I used method `SaveInAdam` just like in those links you've just provided (and like it's done in mobius forms). But it still behaves like I described in question. If I save that entity object programmatically, file field returns null and in admin I can open that file field and see the file uploaded programmatically as available choice in the adam window, but it's not chosen there. It's like deselected file, just available. – Constantine Ketskalo Dec 07 '20 at 14:14
  • 1
    Just added explanation how to do that. – iJungleBoy Dec 11 '20 at 06:26
  • Thank you so much! ) – Constantine Ketskalo Dec 11 '20 at 14:05