Kentico 8 - Macro Expressions for Current Logged in User

Kyle G asked on December 9, 2014 19:28

Hi, I have a custom table that stores user favorited documents. I'm trying to do a query against a view of all content to return a list of user's favorites. I need to get the user id of the currently logged in user using a macro expression if possible.

I've tried the following but they don't really give me what I want:

CurrentUser.UserID MembershipContext.AuthenticatedUser.UserID

This is what's returned instead of my id, "199":

{% CurrentUser.UserID |(user)My Username|(hash)1fd0af3ef3b5a8a657e6e8cd082d7919759b392721ab5df30b528c881f3df799%}

My Query:

SELECT * from View_CMS_Tree_Joined where NodeID in (SELECT FavoriteDocumentID from Favourites WHERE UserID= {% CurrentUser.UserID #%})

Thanks

Correct Answer

Brenden Kehren answered on December 10, 2014 16:59

Are you specifying your WHERE in the webpart or in the query? Better to do it in the webpart and specify the WHERE macro in the query for this reason. Your query should be something like:

SELECT * FROM YourTableOrView WHERE ##WHERE## ORDER BY ##ORDERBY##

Then in your where statement on the webpart, add your macro condition nodeId in (SELECT FavoriteDocumentID from T4G_Favourites WHERE UserID = {% CurrentUser.UserID @%})

using the macro editor for that field.

3 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on December 9, 2014 20:05

Your macro is signed for security reasons which is why you see that hash in there. You can update the macro to be unsigned by using the @ symbol in your macro like so {% CurrentUser.UserID @%}

2 votesVote for this answer Mark as a Correct answer

Kyle G answered on December 9, 2014 20:22

Hi Brenden, I tried that as well but it just translates to "{% CurrentUser.UserID @%})".

select * from View_CMS_Tree_Joined where nodeId in (SELECT FavoriteDocumentID from T4G_Favourites WHERE UserID= {% CurrentUser.UserID @%})

Caused exception: Incorrect syntax near '%'.

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 9, 2014 23:43

Are you using the macro editor? The arrow ( > ) next to the textbox or label? That is what you should be using and not typing it in freehand.

0 votesVote for this answer Mark as a Correct answer

Kyle G answered on December 10, 2014 16:08

I'm using a repeater with a custom query. The query is under one of my Page Types.

0 votesVote for this answer Mark as a Correct answer

Kyle G answered on December 10, 2014 18:13

Thank you, thank you, thank you. That worked like a charm!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.