Media Library API Issue

Daniel Dilsaver asked on December 20, 2017 21:25

I'm trying to upload new files to a Media Library though a console application. I'm able to upload to a custom table in the console app so I know all the connections are correct to the database. However, when I try to upload a file using the Media Library API using the following method:

// Saves the media library file


I receive the following error:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_Media_File_FileSiteID_CMS_Site". The conflict occurred in database "StainSelector_dev_db", table "dbo.CMS_Site", column 'SiteID'.

Correct Answer

Peter Mogilnitski answered on December 20, 2017 22:10

Assuming you are using this api example. There are 2 tables in the DB reponsables for media libraries: Media_library and media_file. Examen them and see if the values that you are using for your api call are correct. Probably you are trying to insert a record with incorrect siteid, that is why you get constraint error.

mediaFile.FileSiteID = library.LibrarySiteID // get it from library instead of SiteContext.CurrentSiteID;
mediaFile.FileLibraryID = library.LibraryID;
2 votesVote for this answer Unmark Correct answer

Recent Answers

Daniel Dilsaver answered on December 21, 2017 13:05

Thanks, using library.LibraryID resolved the issue

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.