Copied to clipboard

Flag this post as spam?

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


  • Chuck 71 posts 69 karma points
    Oct 13, 2011 @ 16:59
    Chuck
    0

    Newbie Get a specific node quesiton

    Hey guys, I got a doc type called Distributor with a property of email on it. All I want to do is redirect a user to that node when they log in. But how do I get the node knowing the emal address? This is what I have so far and it's not working.

    var memberNode = @Model.Distributor.email(currentUser.UserName);

    The currentUser.UserName is the email address, all users on this site will use their email address as their login.

    This razor stuff is really new to me. Sorry if this is really simple.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 13, 2011 @ 17:29
    Dan Diplo
    0

    Can you clarify: You basically want to find a node of a given DocType by the value of it's property, starting from anywhere in your site?

  • Chuck 71 posts 69 karma points
    Oct 13, 2011 @ 17:44
    Chuck
    0

    Yes, that is what I want to do.

    I have a node called CompanyDistributors with an id of 9864.
    Below that are distributors, nodes with the docType Distributor
    On the distributor document is a property called email.

    All I need to do is find the node with the email that matches the current members login... so this is what I have. 

    this gets the current user
    var currentUser = Membership.GetUser();

    this is my latest attempt to get the distributor node. This is throwing an error though. .
     var distNode = @Model.NodeById(9864).Descendants("Distributors").Where("email=" + currentUser.UserName);

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 13, 2011 @ 17:58
    Dan Diplo
    0

    OK, off the top of my head you should be able to do something like this:

    @Model.AncestorsOrSelf().DescendantsOrSelf("Distributor").Where("email == " + CurrentUser.UserName).FirstOrDefault();
  • Chuck 71 posts 69 karma points
    Oct 13, 2011 @ 18:14
    Chuck
    0

    No that gives me a wierd error
    Error loading Razor Script memberRedirect.cshtml
    'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'DescendantsOrSelf'

  • Alex 78 posts 136 karma points
    Oct 13, 2011 @ 18:32
    Alex
    0

    In the code snippet change AncestorsOrSelf() to AncestorOrSelf() (no "s" on Ancestor)

  • Chuck 71 posts 69 karma points
    Oct 13, 2011 @ 18:49
    Chuck
    0

    Ok, that does make a change, I now get a syntax error.

    this gets no error but returns a umbraco.MacroEngines.DynamicNodeList
    var distNode = @Model.AncestorOrSelf().DescendantsOrSelf("Distributor")

    this gives me a syntax error, so something is wrong in the Where clause. 
    var distNode = @Model.AncestorOrSelf().DescendantsOrSelf("Distributor").Where("email == " + currentUser.UserName).FirstOrDefault();;

  • Chuck 71 posts 69 karma points
    Oct 13, 2011 @ 18:50
    Chuck
    0

    ignore the double ; at the end of that line... That was a mistake with the copy and paste

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 13, 2011 @ 21:48
    Dan Diplo
    0

    I forgot the First() bit so try:

    var distNode = Model.AncestorsOrSelf().First().
    DescendantsOrSelf("Distributor").
    Where("email == " + currentUser.UserName).FirstOrDefault();
  • Chuck 71 posts 69 karma points
    Oct 13, 2011 @ 21:52
    Chuck
    0

    This is how I finally got it to work. For whatever reason it really didn't like the syntax for

    Where("email == "+ currentUser.UserName)

    No matter what I tried, it would always give me a syntax error for that, even though it looks right to me.

    But here is the working code, let me know what you think.

    @{
      var currentUser = Membership.GetUser();
    }

    @foreach(var page in Model.NodeById(9864).Children){
        
        if(@page.email == currentUser.UserName)
        {
        Response.Redirect(@page.Url);
        }     
                                                                           
    }

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 13, 2011 @ 22:16
    Dan Diplo
    0

    Realised the problem with the Where clause - you need to resolve it before passing it. So you could do

    string predicate = "email == \"" + currentUser.UserName + "\"";

    and then you should be able to do

    var distNode =Model.AncestorOrSelf().
    DescendantsOrSelf("Distributor").
    Where(predicate).First();
  • Chuck 71 posts 69 karma points
    Oct 14, 2011 @ 16:09
    Chuck
    0

    Thanks Dan, that worked! Very awesome!

Please Sign in or register to post replies

Write your reply to:

Draft