How do I get a list of the content of a page to loop through? I have a document type with an image picker and I want to check the width of my image with a Saving or Publishing event (doesn't really matters at this point)
private void ContentServiceSaved(IContentService sender, SaveEventArgs<IContent> e)
{
foreach (var node in e.SavedEntities)
{
if (node.ContentType.Alias == "productDetail")
{
//Need foreach to loop through node's content
//This forloop is the one I used in a media saving event, but I need to do this
//with the image picker of my document before it gets published or maybe saved
//----------------
foreach (var mediaItem in e.SavedEntities)
{
//if it's an image, the content type will tell you.
if (mediaItem.ContentType.Alias == "Image")
{
var width = Convert.ToDouble(mediaItem.Properties["umbracoWidth"].Value);
var height = Convert.ToDouble(mediaItem.Properties["umbracoHeight"].Value);
if (height / width != 1)
{
//Sending a message will cancel the process, so you don't
//need an else (unless you want to do something else with the image of course.
string msg = "Ratio is not 1:1. Please make sure the width and height of your image is the same.";
e.DisposeIfDisposable();
e.CancelOperation(new EventMessage("Wrong Ratio", msg, EventMessageType.Error));
e.Messages.Add(new EventMessage("Wrong Ratio", msg, EventMessageType.Error));
return;
}
}
}
//----------------
}
}
}
That is not the issue, this code is working. My issue is with the foreach (var mediaItem in e.SavedEntities). This line of code is purely to explain my point, what I would need is something like (var mediaItem in node.Children) . I need the get the image of the imagePicker inside my page "productDetail" (node)
So "productDetail" is my page as shown in this screenshot and I want to get the width of the image from my image picker before the page is published so that I can check if the image corresponds to what I want. If this is not the case, I want to reject the publish or save and ask the user for another image respecting what I want.
The image picker alias is "image". I know in my code it is written "Image", but I don't even reach this point
I used breakpoints and I know that if (node.ContentType.Alias == "productDetail") properly refers to this page, but I don't know how to get the content of the page
Content Event loop through content
How do I get a list of the content of a page to loop through? I have a document type with an image picker and I want to check the width of my image with a Saving or Publishing event (doesn't really matters at this point)
Hi Remi
So your code above isn't working ? The alias usually has a lowercase first character, so...
Hope it is this simple ! :-)
Nigel
That is not the issue, this code is working. My issue is with the foreach (var mediaItem in e.SavedEntities). This line of code is purely to explain my point, what I would need is something like (var mediaItem in node.Children) . I need the get the image of the imagePicker inside my page "productDetail" (node)
Hi Remi
You have to get the node and get all images with ContentService and MediaService
So "productDetail" contains id of Content node? What is an alias of property containing media id?
/Alex
So "productDetail" is my page as shown in this screenshot and I want to get the width of the image from my image picker before the page is published so that I can check if the image corresponds to what I want. If this is not the case, I want to reject the publish or save and ask the user for another image respecting what I want.
The image picker alias is "image". I know in my code it is written "Image", but I don't even reach this point
I used breakpoints and I know that if (node.ContentType.Alias == "productDetail") properly refers to this page, but I don't know how to get the content of the page
is working on a reply...