Search code examples
smartsheet-apismartsheet-c#-sdk-v2

Impersonate user with C# SDK


I'm using the new C# SDK and can successfully create a connection with this code:

Token token = new Token();
token.AccessToken = "MYTOKEN";

SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();

This works good if I only want to access the data in my own Smartsheet account. But I'm building an application that will need to create Sheets that are owned by other users in my company. Using the SDK, how do I create a connection that runs under the identity of another user (without having an access token that belongs to that other user)?


Solution

  • Using a token that belongs to a Sys Admin user, you can impersonate another user by using "Assume-User" as described here: http://smartsheet-platform.github.io/api-docs/?csharp#assume-user.

    As the documentation shows, the C# code to create a connection using "Assume-User" is as follows:

    // Set access token.
    Token token = new Token();
    token.AccessToken = "SYS_ADMIN_TOKEN";
    
    // Create the 'smartsheet' object and set the 'Assume-User' header value to "[email protected]".
    SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).SetAssumedUser("[email protected]").Build();
    

    Once you've created a connection, you can verify that you're connecting as the user you intended by executing a "Get Current User" call with that connection:

    // Get current user (call gets executed as "[email protected]").
    UserProfile userProfile = smartsheet.UserResources.GetCurrentUser();
    

    The response to this call will contain the email, name, and id of the user by whom the call was executed. For example:

    {
        "email": "[email protected]",
        "firstName": "John",
        "lastName": "Doe",
        "id": 48569348493401200
    }