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 Reply
  • Are you having an issue with the Update endpoint for usernames that are not numbers only?  That should work fine. 

    While you are defining the path like:

    var response = host.PutToDynamic(2, "users/{lookupusername}.json", true, new RestPutOptions
    {
    PathParameters = new NameValueCollection {
    { "lookupusername", username }
    },

    The system just receives users/12345.json.  It has no idea you called that variable lookupusername.  It just sees the integer value and assumes its a userId.

Children
  •   Since it is an int, users/12345.json turns into userId = 12345. If it wasn't an int, it would work (I.e. users/1234X.json). So it seems like it does a int.TryParse and if it succeeds, it uses it as a userId else username. Not how I would write my API code.

    I think you are right, I need to get the user via username=12345 and then update via that user's true userid. 

  • >> Thanks John, the username property is only need if you are changing the username.

    Right, but if you are changing a value that you're also using for lookup, then you naturally would have to provide two parameters, one for the lookup (lookupUsername), then one for the new value you're setting (username)

    But with that said ...

    >> I think you are right, I need to get the user via username=12345 and then update via that user's true userid. 

    Yes, I'd recommend the same as a best practice.