I have a script to upload images to the media library (works fine) and then assign each image to a particular document field which also works. The only issue is the preview thumbnails aren't showing:
The code to update the field is pretty straight forward:
private static void AssignImageToProject(string projectID, int index, string imgUrl)
{
var projectPage = _projectPages
.Where(p => p.GetStringValue("ProjectID", null) == projectID)
.FirstOrDefault();
if (projectPage == null) return;
projectPage.CheckOut();
projectPage.SetValue($"ProjectImage{index+1}", $"{imgUrl}");
// Updates the page in the database
projectPage.Update();
// Creates a new version of the updated page (required even when not using content locking)
// If using content locking, checks in the page
projectPage.CheckIn();
projectPage.Publish();
}
The image url is correct as per the DB and the images display as expected on the front end. Is there a method I can call to generate the previews?
I'm using Kentico 11 by the way.
Edit: In my DB the field value starts with a tilda '~' but as you can see in the screenshot the page does not prefix the value with the tilda. However, if I click Select, it brings me to the correct image and if I select it, it updates the field with a tilda and I get the image preview
The default Kentico control saves URL with "~" symbol in the beginning even though it doesn't display it in the admin UI. If you amend your code like this:
projectPage.SetValue($"ProjectImage{index+1}", $"~{imgUrl}");
It should help probably. Make sure in the actual field in the database you see something like this:
~/getmedia/16b40b07-9b41-4812-9194-4bb35470ad8c/Bakery-And-Coffee.jpg?width=849&height=565&ext=.jpg
UPDATE: it seems it is also important to keep an extension parameter in the URL '&ext=.jpg'