Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Remi Martel 32 posts 104 karma points
    Mar 06, 2018 @ 16:26
    Remi Martel
    0

    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)

    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;
                            }
                        }
                    }
                    //----------------
                }
            }
        }
    
  • Nigel Wilson 944 posts 2076 karma points
    Mar 06, 2018 @ 18:13
    Nigel Wilson
    0

    Hi Remi

    So your code above isn't working ? The alias usually has a lowercase first character, so...

    if (mediaItem.ContentType.Alias == "image")
    

    Hope it is this simple ! :-)

    Nigel

  • Remi Martel 32 posts 104 karma points
    Mar 06, 2018 @ 18:22
    Remi Martel
    0

    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)

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Mar 06, 2018 @ 18:29
    Alex Skrypnyk
    0

    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

  • Remi Martel 32 posts 104 karma points
    Mar 06, 2018 @ 18:41
    Remi Martel
    0

    enter image description here

    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

  • Remi Martel 32 posts 104 karma points
    Mar 06, 2018 @ 18:42
    Remi Martel
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft