General DataQuery macro

Stephen Rushing asked on April 16, 2015 20:30

I'm having trouble finding docs on a way to do a general data query in a macro expression.

In other words, something like this:

{% query = new DataQuery(); query.From("CMS_User").Column("UserID").Where("UserName = 'bob'"); %}

Correct Answer

Jim Spillane answered on April 17, 2015 01:35

Hi Stephen. Give this a try...

{% 
query = GlobalObjects.Users.Columns("UserID,FullName").Where("UserName = 'bob'").FirstItem;
println(query.UserID + " " + query.FullName);
%}

or just for the FullName...

{% GlobalObjects.Users.Columns("FullName").Where("UserName = 'bob'").FirstItem.FullName %}

1 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on April 16, 2015 22:36

I would imagine that you will need to create a custom macro for this. The code that you have inside of your macro is actually C#, not K#. What you can do though is create a custom macro where you pass in the that string, and return the userids, really you can work it however you would want to, as I am not sure of your specific requirement.

0 votesVote for this answer Mark as a Correct answer

Stephen Rushing answered on April 16, 2015 23:45 (last edited on April 16, 2015 23:46)

Yeah, it was really more of a general thing, because I didn't want to have to create custom macro methods for trivial/arbitrary stuff. For instance, I'm passing the UserName as a query string to a page, and in a Static Text web part I'd like to output the FullName of that user (not CurrentUser). There's a GetUserFullName transformation method, but I need the ID for that. I assumed there'd be a general way of querying the database to grab a value or collection of records to transform (I was also wondering why I couldn't find a Users collection the K# API in the first place).

0 votesVote for this answer Mark as a Correct answer

Stephen Rushing answered on April 17, 2015 14:32

Thanks, that's what I needed! Not sure how I missed the GlobalObjects.Users collection...

0 votesVote for this answer Mark as a Correct answer

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