users list in a list

Novice User asked on June 18, 2018 18:36

I am using a static HTML Web part for populating a drop down list with all users in kentico. I am using this code but its not generating any results.

 <select name="users" id="users">
   {% var users = UserInfoProvider.GetUsers()#%}
   {% foreach (UserInfo user in users){%}
    <option value="{%users.UserName%}">{%users.UserName%}</option>
   {%}#%}
</select>

Correct Answer

Peter Mogilnitski answered on June 19, 2018 03:57

There is not var in macro, i.e. there is no variable declaration in k#/macro. Remove var and it will work. You can also simplify your macro:

<select name="users" id="users">
   {% GlobalObjects.Users.Transform("<option value='{%#UserName#}'>{#UserName#}</option>")#%}
</select>
1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 18, 2018 18:50

I'd highly suggest using a repeater webpart hooked up to a datasource to do this vs. the macro. The repeater and datasource will be much faster.

Secondly, your syntax is incorrect (check out the documentation):

<select name="users" id="users">
   {% var users = CMSContext.Current.GlobalObjects.Users%}
   {% foreach (user in users){%}
    <option value="{%user.UserName%}">{%user.UserName%}</option>
   {%}%}
</select>
1 votesVote for this answer Mark as a Correct answer

Novice User answered on June 18, 2018 21:40 (last edited on June 18, 2018 23:19)

This code just paste the big text of users in quotes. the for each loop is not creating any "option" tag. Is this a problem of static html web part or I am missing some thing here.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 18, 2018 22:20

What does your output look like? Your initial problem was you were not getting any results. How it's formatted you may need to work on yourself (vs. having someone do it for you), it will help you learn a bit more how it works.

1 votesVote for this answer Mark as a Correct answer

Novice User answered on June 18, 2018 23:17 (last edited on June 18, 2018 23:18)

The code down below should create multiple "option" tags depending upon the number of users. For now its just displaying var users, The for loop is not even executing. I was wondering if I am missing some thing here.

 <select name="users" id="users">
   {% var users = CMSContext.Current.GlobalObjects.Users%}
   {% foreach (user in users){%}
    <option value="{%user.UserName%}">{%user.UserName%}</option>
   {%}%}
</select>
0 votesVote for this answer Mark as a Correct answer

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