How to get User Description based on UserID?

Aashish Khandelwal asked on April 6, 2015 11:15

I have created an Article Type Transformation wherein I wanted to display the description of an user based on userID.

Suggest...

Recent Answers


Brenden Kehren answered on April 6, 2015 13:30

You could try something like this:

CMS.Membership.UserSettingsInfoProvider.GetUserSettingsInfo(EvalInt("UserID", 0)).UserDescription
0 votesVote for this answer Mark as a Correct answer

Aashish Khandelwal answered on April 6, 2015 14:08

Not working in Text/XMl type of Transformation.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on April 6, 2015 15:34 (last edited on April 6, 2015 15:35)

Custom Function

    public static string GetUserDescription(object UserID)
{
    string resolved = string.Empty;
    if (UserID != null)
    {
        int userID;
        if (Int32.TryParse(UserID.ToString(), out userID))
        {
            var user = UserInfoProvider.GetUserInfo(userID);

            if (user != null)
            {
                resolved = user.UserDescription ?? string.Empty;
            }
        }
    }

    return resolved;
}
0 votesVote for this answer Mark as a Correct answer

Aashish Khandelwal answered on April 6, 2015 15:44

Thanks Charles ,

Could you please let me know the file name where I have to add this method?

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on April 6, 2015 17:09 (last edited on April 6, 2015 17:10)

You create a class file in your App_Code Directory. I call this one CustomFunctions.cs

You then call the function in your transformation as CustomFunctions.GetUserDescription("Put or get UserID Here")

Here is the complete class. Just create the file name above and paste this code into it. Or if you have a custom function class file already just past the code. Make sure your usings are correct.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using CMS.Helpers;
using CMS.Controls;
using CMS.EventLog;
using CMS.TreeEngine;
using CMS.DocumentEngine;
using CMS.ExtendedControls;
using CMS.PortalEngine;
using CMS.SettingsProvider;
using CMS.UIControls;
using CMS.SiteProvider;
using CMS.Membership;
using CMS.MembershipProvider;
using CMS.Protection;


/// <summary>
/// Custom Functions for Site.
/// </summary>
public class CustomFunctions
{


    public static string GetUserDescription(object UserID)
    {
        string resolved = string.Empty;
        if (UserID != null)
        {
            int userID;
            if (Int32.TryParse(UserID.ToString(), out userID))
            {
                var user = UserInfoProvider.GetUserInfo(userID);

                if (user != null)
                {
                    resolved = user.UserDescription ?? string.Empty;
                }
            }
        }

        return resolved;
    }
}   
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 7, 2015 14:35

Couldn't you use this macro without needing to create a custom method?

{% GlobalObjects.Users[53].UserDescription %}
0 votesVote for this answer Mark as a Correct answer

Aashish Khandelwal answered on February 2, 2016 17:34 (last edited on February 2, 2016 17:35)

{% GlobalObjects.Users[GetUserName(ArticleAuthor)].UserDescription  #%}

Here ArticleAuthor represents the userID.

Thanks Brenden Kehren. You showed me the correct path.

0 votesVote for this answer Mark as a Correct answer

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