Tuesday, July 6, 2010

How to get currently authenticated user in MSCRM SQL queries?

During developing SQL queries for MSCRM, it’s often required to fetch the currently authenticated user.
SUSER_SNAME() is a special function in SQL that returns the currently authenticated user. If your code doesn’t authenticate to SQL Server by using Windows authentication, your queries will never return any data, because all of the filtered views perform an inner join using systemuserid. Here is the query to fetch currently authenticated user in SQL server.

select SystemUserId, FullName from SystemUserBase
where SystemUserBase.DomainName = SUSER_SNAME()

above code referred from http://ayazahmad.wordpress.com/page/5/

No comments: