Get and update a user by Username

So the APi doco is sparse on getting and setting a user by username. We have an issue where some of our usernames are all numbers and the API is treating them a UserID and not a user name. 

Getting a User

I can use this code to get a user in c#, but I am appending ?username=## to the url. So it seems like a hack

public dynamic GetTelligentUser(string username)
{
    ServicePointManager.Expect100Continue = false;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    var host = new ClientCredentialsRestHost(
        communitySettings.Username,
        communitySettings.Endpoint,
        communitySettings.OAuthClientId,
        communitySettings.OAuthSecret);

    var response = host.GetToDynamic(2, $"user.json?username={username}", true, new RestGetOptions
    {

    });
    return response;
}

Looking at the doco (community.telligent.com/.../show-user-rest-endpoint), I should be doing it this way, but it never returns a username

public dynamic GetTelligentUser(string username)
{
    ServicePointManager.Expect100Continue = false;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    var host = new ClientCredentialsRestHost(
        communitySettings.Username,
        communitySettings.Endpoint,
        communitySettings.OAuthClientId,
        communitySettings.OAuthSecret);

    var response = host.GetToDynamic(2, "users/{username}.json", true, new RestGetOptions
    {
        PathParameters = new System.Collections.Specialized.NameValueCollection {
            { "username", username }
        }
    });
    return response;
}

Updating a user

I followed the doco ( Update User REST Endpoint ) for updating a user, but always says "User not found" or "Lookupusername is required". Doing it by user ID was easy and works fine. 

var host = new ClientCredentialsRestHost(
communitySettings.Username,
communitySettings.Endpoint,
communitySettings.OAuthClientId,
communitySettings.OAuthSecret);

var response = host.PutToDynamic(2, "users/{lookupusername}.json", true, new RestPutOptions
{
PathParameters = new NameValueCollection {
    { "lookupusername", username }
},
PostParameters = new NameValueCollection()
{
    { "_ProfileFields_Address", profileData.ContactInfo.Address },
    { "_ProfileFields_City", profileData.ContactInfo.City },
    { "_ProfileFields_Country", profileData.ContactInfo.Country },
    { "_ProfileFields_Email", profileData.ContactInfo.Email },
    { "_ProfileFields_First_Name", profileData.ContactInfo.FirstName },
}

});

return response;



fix code
[edited by: Chris Auer at 10:01 PM (GMT 0) on Tue, Mar 2 2021]
Parents
  • It ended up being that I needed to call and get the user by username and then use the UserId from there on.

        public int GetUserIdByUsername(string username)
        {
            var host = new ClientCredentialsRestHost(
                communitySettings.Username,
                communitySettings.Endpoint,
                communitySettings.OAuthClientId,
                communitySettings.OAuthSecret);
    
            var response = host.GetToDynamic(2, $"user.json?username={username}", true);
    
            var telligentUser = ((dynamic)response).User;
    
            if (telligentUser != null)
            {
                var userId = Convert.ToInt32(telligentUser.Id);
                return userId;
            }
            return 0;
        }

Reply
  • It ended up being that I needed to call and get the user by username and then use the UserId from there on.

        public int GetUserIdByUsername(string username)
        {
            var host = new ClientCredentialsRestHost(
                communitySettings.Username,
                communitySettings.Endpoint,
                communitySettings.OAuthClientId,
                communitySettings.OAuthSecret);
    
            var response = host.GetToDynamic(2, $"user.json?username={username}", true);
    
            var telligentUser = ((dynamic)response).User;
    
            if (telligentUser != null)
            {
                var userId = Convert.ToInt32(telligentUser.Id);
                return userId;
            }
            return 0;
        }

Children
No Data