Get Username from FileModifiedByUserID or FileCreatedByUserID in a Transformation

Darren Stahlhut asked on March 4, 2015 01:22

In a transformation we would like to display the Username that Created or Modified a file in the Media Library.

I am using the MediaFileDataSource and I can get the UserID with Eval("FileModifiedByUserID") and Eval("FileCreatedByUserID").

But I cannot find a suitable Transformation Method to get the Username from the UserID.

I just wanted to know if something already exists before I go ahead and create a custom Transformation Method;

Public String GetUsername(int UserID);

Correct Answer

Charles Matvchuk answered on March 4, 2015 02:31

Rather than being lazy. Here is a better way, using the UserInfoProvider. I take the parameter as an object that way I do not have to clutter my method call with a conversion to int.

    public static string resolveUserName(object UserId)
{
    int userid = Convert.ToInt32(UserId);
    string resolvedUserName = "";
    var user = UserInfoProvider.GetUserInfo(userid);

    if(user != null)
    {
        resolvedUserName = user.UserName;
    }

    return resolvedUserName;

}

2 votesVote for this answer Unmark Correct answer

Recent Answers


Charles Matvchuk answered on March 4, 2015 01:41

Here is the code that I use. It is ado.net but it executes the fastest. Just cut and paste it in your custom functions, don't forget the proper usings.

    public static string resolveUserId(object mUserId)
{

    string userID = mUserId.ToString();
    string resolvedUsername = "";
    SqlDataReader rdr = null;
    string connPath = WebConfigurationManager.ConnectionStrings["CMSConnectionString"].ToString();
    SqlConnection conn = new SqlConnection(connPath);
    SqlCommand cmd = new SqlCommand("Select UserName FROM CMS_User where UserID = @UserID", conn);
    SqlParameter paramProgramID = new SqlParameter() { ParameterName = "@UserID", Value = userID };
    cmd.Parameters.Add(paramProgramID);

    try
    {
        conn.Open();
        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            string UserName = (string)rdr["UserName"];

            if (rdr["UserName"] != System.DBNull.Value)
            {
                resolvedUsername = UserName;

            }
            else
            {
                resolvedUsername = "";

            }



        }
    }
    finally
    {
        if (rdr != null)
        {
            rdr.Close();
        }
        if (conn != null)
        {
            conn.Close();
        }
    }
    return resolvedUsername;
}
0 votesVote for this answer Mark as a Correct answer

Darren Stahlhut answered on March 4, 2015 03:51

@Charles Thank you, I've implemented the UserInfoProvider API example you provided with a few minor tweaks.

    public static string GetUsername(object UserID)
    {
        string username = string.Empty;
        if (UserID != null)
        {
            int userID;
            if (Int32.TryParse(UserID.ToString(), out userID))
            {
                var user = UserInfoProvider.GetUserInfo(userID);
                if (user != null)
                {
                    username = user.UserName;
                }
            }
        }
        return username;
    }
0 votesVote for this answer Mark as a Correct answer

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