I am trying to get the id of my image in a MediaService,Saved event so that I can get the path (path always being /media/mediaID/imagename.extension)
This is my event:
void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
{
foreach (var page in e.SavedEntities)
{
//getting the image id
int id = 0;
foreach (var prop in page.Properties)
{
if (prop.Alias == "image")
{
id = prop.Id;//!!!!! - Problem is here, this return a 0 - !!!!
}
}
//Path to folder containing image
string path = "/media/" + id + "/";
//check if image needs to be resized by checking if size folders exists
if (System.IO.File.Exists(path + "tile") || System.IO.File.Exists(path + "carousel") || System.IO.File.Exists(path + "product") ||
System.IO.File.Exists(path + "mobileTile") || System.IO.File.Exists(path + "mobileProduct"))
{
//send id to resizer to resize each variant of the image
Resizer resizer = new Resizer();
resizer.Resize(id);
}
//else do nothing
}
}
The question is a little unclear as its hard to see what you are trying to do based on just the code and its a little confusing. It looks like you are trying to loop over the properties to find an image on a media entity but since you are using the media creation event this should not be needed as you already have all that.
You can access to the media Id by simply using the ID property on the saved entity.
foreach (var media in e.SavedEntities)
{
var id = media.Id;
}
You can also use the inbuilt methods to check if the image is new and as such likely to require the crops creating on the server.
However for functionality like this you might want to consider using the image cropper property on the page where you want to show the images. This will allow you to define the crops you want to use and then display them in a template using this simple syntax:
Get media Id in MediaService,Saved event
I am trying to get the id of my image in a MediaService,Saved event so that I can get the path (path always being /media/mediaID/imagename.extension)
This is my event:
Hi Remi,
The question is a little unclear as its hard to see what you are trying to do based on just the code and its a little confusing. It looks like you are trying to loop over the properties to find an image on a media entity but since you are using the media creation event this should not be needed as you already have all that.
You can access to the media Id by simply using the ID property on the saved entity.
You can also use the inbuilt methods to check if the image is new and as such likely to require the crops creating on the server.
However for functionality like this you might want to consider using the image cropper property on the page where you want to show the images. This will allow you to define the crops you want to use and then display them in a template using this simple syntax:
Paul
Thanks, Paul, this is exactly what I was trying to do, I just got confused and went too far in my code for no reason.
is working on a reply...