Copied to clipboard

Flag this post as spam?

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


  • Dan Sharpe 8 posts 88 karma points
    Mar 22, 2017 @ 09:05
    Dan Sharpe
    0

    TypedContentAtXPath and Images

    Hi everyone,

    looking for some advice into how i can efficiently get hold of the images in the results of my TypedContentAtXPath query, here is my code:

    string query = $"//homePage/demosHome/demo[@isDoc and number(translate(demoDate,'-T:','')) < '{String.Format("{0:yyyyMMddHHmmss}", enddate)}' and number(translate(demoDate,'-T:','')) > '{String.Format("{0:yyyyMMddHHmmss}", startdate)}']";
    
            var demos = Umbraco.TypedContentAtXPath(query).ToList();           
    
            //var homenode7 = Umbraco.TypedContentAtXPath($"//homePage/demosHome/demo[@isDoc and number(translate(substring(demoDate,1,10),'-','')) < number({String.Format("{0:yyyyMMdd}", enddate)})]").ToList();
            //var homenode8 = Umbraco.TypedContentAtXPath($"//homePage/demosHome/demo[@isDoc and number(translate(demoDate,'-T:','')) < '{String.Format("{0:yyyyMMddHHmmss}", enddate)}' and number(translate(demoDate,'-T:','')) > '{String.Format("{0:yyyyMMddHHmmss}", startdate)}']").ToList();
            return demos
                //.Cast<DynamicPublishedContent>()
                .Select(d => new Demo {
                    Date = Convert.ToDateTime(d.GetProperty("demoDate").Value),
                    Title = (string)d.GetProperty("demoTitle").Value,
                    Url = d.Url//,
                               //ImageUrl = d.GetProperty<>("demoImage").HasValue ? d.GetProperty<IPublishedContent>("demoImage").Value
                }
                )
                .ToList();
    

    basically, im trying to populate the ImageUrl property of my return class from within the select part, the only thing is, im not sure what i need to cast the object to, to get hold of the image properties, i can see them in the debugger but they are part of the Umbraco.Web.PublishedContentModels namespace that i cant seem to get hold of.

    could anybody advise how i can get the Url property of the image in the demoImage property of my document type?

  • Sven Geusens 169 posts 881 karma points c-trib
    Mar 22, 2017 @ 16:51
    Sven Geusens
    0

    ImageUrl = d.GetPropertyValue

    But you will have to use the Our.Umbraco.CoreValueConverters package (nuget or otherwise) unless you are using 7.6 beta.

  • Dan Sharpe 8 posts 88 karma points
    Mar 23, 2017 @ 10:30
    Dan Sharpe
    0

    i tried what you said, but when i do:

    ImageUrl = d.GetProperty("demoImage").HasValue ? d.GetProperty("demoImage").Url
    

    i get the error: 'IPublishedProperty' does not contain a definition for 'Url' and no extension method 'Url' accepting a first argument of type 'IPublishedProperty' could be found (are you missing a using directive or an assembly reference?)

    i have the CoreValueconverters package installed but that only seems to affect properties on the view (Model.Content and CurrentPage) rather than in the code behind or in this case, the APIController.

    is the only way to do it to use dynamics?

  • Sven Geusens 169 posts 881 karma points c-trib
    Mar 23, 2017 @ 10:33
    Sven Geusens
    0

    Spot the differences: I use GetPropertyValue instead of GetProperty

  • Dan Sharpe 8 posts 88 karma points
    Mar 23, 2017 @ 11:44
    Dan Sharpe
    0

    d'oh, now i see, i had to tweak your example a bit but i ended up with this:

    ImageUrl = d.GetProperty("demoImage").HasValue ? d.GetPropertyValue<IPublishedContent>("demoImage").Url : null
    

    i'm hoping thats the correct way of doing it, but it works

    many thanks for the help though

Please Sign in or register to post replies

Write your reply to:

Draft