Copied to clipboard

Flag this post as spam?

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


  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 09:41
    Kurt Moskjaer Andersen
    0

    Numeric selection

    I am trying to get all the pages with two numeric properties, where the one is greater than the other, like this:

    var Node = @Model.NodeById(1157);

    foreach (var page in Node.Children.Where("Visible").Where("maal.AsInt() > opnaaet.AsInt()"))

    I does not return any result though, even though maal has a value of 100000 and opnaaet has a value of 2000. If I change the selection to:

    foreach (var page in Node.Children.Where("Visible").Where("maal.AsInt() > 2000"))

    I get the results needed, but it is not an option to make the selection like this.

    What am I doing wrong?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 10:46
    Dave Woestenborghs
    0

    It is probably because you where statement cannot get parsed by the engine.

    You are using the dynamic object Model. Try using the strongly typed DynamicNode

    Like this :

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        var node = new DynamicNode(1157);
        foreach (var page in node.GetChildrenAsList.Items.Where(n => n.Visible && (n.GetPropertyValue("maal", "0").AsInt() > n.GetPropertyValue("opnaaet", "0").AsInt())))
        {
            //do your stuff with page here
        }
    }

    Dave

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 12:30
    Kurt Moskjaer Andersen
    0

    Well, it just gives me an error like:

    umbraco.MacroEngines.DynamicNode' does not contain a definition for 'GetPropertyValue'

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 12:32
    Dave Woestenborghs
    0

    Did you have the @using and @inherits directives in your macro cshtml script ?

     

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 12:35
    Kurt Moskjaer Andersen
    0

    Yes, I have:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @using umbraco.cms.businesslogic.member
    @using System.Net.Mail
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 12:36
    Dave Woestenborghs
    0

    Try putting the inherits as last

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 12:39
    Dave Woestenborghs
    0

    What version of Umbraco are you using ?

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 12:54
    Kurt Moskjaer Andersen
    0

    Well, no change :(

    I'm using version 4.7.0

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 13:07
    Dave Woestenborghs
    0

    Can you post your entire code ?

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 13:42
    Kurt Moskjaer Andersen
    0

    Yes, of course:

    @using umbraco.MacroEngines
    @using umbraco.cms.businesslogic.member
    @using System.Net.Mail
    @inherits DynamicNodeContext

    @{
    var members = Member.GetAll;
    try {
    foreach(var item in members.Where(x => x.getProperty("afsluttedeProjekter").Value.ToString() == "21")) {
    var Node = new DynamicNode(1157);
    var Content = "Følgende projekter er afsluttet:\n\n";
    var Projects = "";
    int i;
    foreach (var page in Node.GetChildrenAsList.Items.Where(n => n.Visible && (n.GetPropertyValue("maal", "0").AsInt() > n.GetPropertyValue("opnaaet", "0").AsInt()))) {
            Projects = Projects + "Opnået: " + @page.opnaaet + " - Mål: " + @page.maal + "<br>";
    }
    if(Projects != "") { @SendMyMail(@item.Email.ToString(), "Afsluttede projekter", @Content + @Projects); }
    }
            catch(Exception ex) {
    Response.Write(ex);
    }
    }

    @helper SendMyMail(string email, string subject, string projects) {
    try {
    if(email != "") {
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("[email protected]");
    mail.To.Add("[email protected]");
    mail.Subject = subject;
    mail.IsBodyHtml = false;
    mail.Body = projects + "\n\n"
    + "Venlig hilsen";
    SmtpClient smtp = new SmtpClient();
    smtp.Send(mail);
    }
    }
    catch(Exception ex) {
    Response.Write(ex);
    }
    }

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 13:56
    Dave Woestenborghs
    0

    Can you try this :

     

    @using umbraco.MacroEngines
    @using umbraco.cms.businesslogic.member
    @using System.Net.Mail
    @inherits DynamicNodeContext
    
    @{
    var members = Member.GetAll;
    try {
    foreach(var item in members.Where(x => x.getProperty("afsluttedeProjekter").Value.ToString() == "21")) {
    DynamicNode Node = new DynamicNode(1157);
    var Content = "Følgende projekter er afsluttet:\n\n";
    var Projects = "";
    int i;
    foreach (DynamicNode page in Node.GetChildrenAsList.Items.Where(n => n.Visible && (n.GetPropertyValue("maal", "0").AsInt() > n.GetPropertyValue("opnaaet", "0").AsInt()))) {
            Projects = Projects + "Opnået: " + @page.GetPropertyValue("opnaaet") + " - Mål: " + @page.GetPropertyValue("maal") + "
    "; } if(Projects != "") { @SendMyMail(@item.Email.ToString(), "Afsluttede projekter", @Content + @Projects); } } catch(Exception ex) { Response.Write(ex); } } @helper SendMyMail(string email, string subject, string projects) { try { if(email != "") { MailMessage mail = new MailMessage(); mail.From = new MailAddress("[email protected]"); mail.To.Add("[email protected]"); mail.Subject = subject; mail.IsBodyHtml = false; mail.Body = projects + "\n\n" + "Venlig hilsen"; SmtpClient smtp = new SmtpClient(); smtp.Send(mail); } } catch(Exception ex) { Response.Write(ex); } }
  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 14:25
    Kurt Moskjaer Andersen
    0

    I have tried it, but still same error:

    Testalert.cshtml(14): error CS1061: 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'GetPropertyValue' and no extension method 'GetPropertyValue' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 14:40
    Dave Woestenborghs
    0

    one more try :

     

    @using umbraco.MacroEngines
    @using umbraco.cms.businesslogic.member
    @using System.Net.Mail
    @inherits DynamicNodeContext
    
    @{
    var members = Member.GetAll;
    try {
    foreach(var item in members.Where(x => x.getProperty("afsluttedeProjekter").Value.ToString() == "21")) {
    DynamicNode Node = new DynamicNode(1157);
    var Content = "Følgende projekter er afsluttet:\n\n";
    var Projects = "";
    int i;
    foreach (DynamicNode page in Node.GetChildrenAsList.Items.Where(n => n.Visible && (n.GetPropertyValue("maal").AsInt() > n.GetPropertyValue("opnaaet").AsInt()))) {
            Projects = Projects + "Opnået: " + @page.GetPropertyValue("opnaaet") + " - Mål: " + @page.GetPropertyValue("maal") + "
    "; } if(Projects != "") { @SendMyMail(@item.Email.ToString(), "Afsluttede projekter", @Content + @Projects); } } catch(Exception ex) { Response.Write(ex); } } @helper SendMyMail(string email, string subject, string projects) { try { if(email != "") { MailMessage mail = new MailMessage(); mail.From = new MailAddress("[email protected]"); mail.To.Add("[email protected]"); mail.Subject = subject; mail.IsBodyHtml = false; mail.Body = projects + "\n\n" + "Venlig hilsen"; SmtpClient smtp = new SmtpClient(); smtp.Send(mail); } } catch(Exception ex) { Response.Write(ex); } }
  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 14:52
    Kurt Moskjaer Andersen
    0

    Still same error, but related to the following line:

    Projects = Projects + "Opnået: " + @page.GetPropertyValue("opnaaet") + " - Mål: " + @page.GetPropertyValue("maal") + "";

    Update: Sorry, my mistake - it seems it is still the following line which causes the error:

    foreach (DynamicNode page in Node.GetChildrenAsList.Items.Where(n => n.Visible && (n.GetPropertyValue("maal").AsInt() > n.GetPropertyValue("opnaaet").AsInt()))) {
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 14:55
    Dave Woestenborghs
    0

    Change @page to page.

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 14:59
    Kurt Moskjaer Andersen
    0

    No change :(

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 15:01
    Dave Woestenborghs
    0

    Maybe it's the AsInt() function causing the problem ? Can you remove that and use a normal cast to int ?

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 15:06
    Kurt Moskjaer Andersen
    0

    Do you mean like Convert.ToInt32(var)?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 15:06
    Dave Woestenborghs
    0

    Yep

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 15:09
    Kurt Moskjaer Andersen
    0

    Does not change the outcome. If I change it like this:

    foreach (DynamicNode page in Node.GetChildrenAsList.Items.Where(n => n.Visible && (Convert.ToInt32(n.GetProperty("maal").Value) > Convert.ToInt32(n.GetProperty("opnaaet").Value)))) {
                    Projects = Projects + "Opnået: " + @page.GetProperty("opnaaet").Value + " - Mål: " + @page.GetProperty("maal").Value + "";            }

    I don't get any errors, but no usefull output either.

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 15:15
    Kurt Moskjaer Andersen
    0

    It seems to be working now. I get emails as I should.

    Thank you very much for your help, Dave

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 15:16
    Dave Woestenborghs
    0

    What did you change to get it working ?

     

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 15:22
    Kurt Moskjaer Andersen
    0

    Well, it is sort of embarrassing :) Earlier on, I made a Response.Write to output the value of the variable Projects, to the screen, but in the latest version it just spits out an e-mail, and no output on the screen.

    It seems though, I cannot use the GetPropertyValue(var) in this version of Umbraco, but only GetProperty(var).Value.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 08, 2013 @ 15:32
    Dave Woestenborghs
    0

    Glad you worked it out

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 08, 2013 @ 15:36
    Kurt Moskjaer Andersen
    0

    Not without your help :) Thanks

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 09, 2013 @ 09:19
    Kurt Moskjaer Andersen
    0

    It's me again :)

    The DynamicNode still puzzles me. If I want to select all nodes with the property slutdato equal to yesterdays date, I get this error:

    System.NullReferenceException: Object reference not set to an instance of an object

    When trying to do the following:

    foreach (DynamicNode page in Node.GetChildrenAsList.Items.Where(n => n.Visible && (Convert.ToDateTime(n.GetProperty("slutdato").Value) == DateTime.Today.AddDays(-1)) && (Convert.ToInt32(n.GetProperty("maal").Value) > Convert.ToInt32(n.GetProperty("opnaaet").Value)))) {
    Some code stuff...
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 09, 2013 @ 09:26
    Dave Woestenborghs
    0

    Does your date property have a value ?

    Dave

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 09, 2013 @ 09:51
    Kurt Moskjaer Andersen
    0

    Yes, it does. It is a Datepicker-property.

  • Kurt Moskjaer Andersen 40 posts 95 karma points
    Apr 09, 2013 @ 11:06
    Kurt Moskjaer Andersen
    0

    Well, I've got it solved - damn case-sensitive coding :)

Please Sign in or register to post replies

Write your reply to:

Draft