1 Reply Latest reply on Sep 18, 2018 9:06 AM by Binit Kumar

    Create and Update new Tableau User using REST API in C#

    Binit Kumar

      I am trying to Add User using the Tableau REST API in C# as documented in API Reference here.

      This works fine as shown below:

       

      When I try to call the Update User function after the Add User call in line 5 below, it doesn't update the details.

      I understand that the update doesn't work because the 'Add User' function in line 2 hasn't finished successfully and that's why the user is not found by the 'Update User' function.

       

      Can someone help me with insuring that the first 'Add user' call completes successfully before executing the 'Update User' function, how do I do that?

       

       

      // Calling Create user function
      CreateTableauUser(tabServer, siteId, token, tabUser);
      
      // Calling Update User function
      UpdateTableauUser(tabServer, siteId, token, tabUser, userFullName, user.Email); // This call doesn't update user details when being called right after the Add User call
      
      // Add user function
      private static string CreateTableauUser(string tabHost, string siteId, string token, string user)
           {
               XElement userRequest = new XElement("tsRequest",
                                      new XElement("user",
                                      new XAttribute("name",user),
                                      new XAttribute("siteRole","Viewer")
                                      )
          );
      
          string tabUrl = "https://" + tabHost + "/api/3.1/sites/" + siteId + "/users";
          var createUserResponse = SendTableauRequest(tabUrl, "POST", userRequest.ToString(), token);
      
          return createUserResponse;
      }
      
      // Update User function
      private static string UpdateTableauUser(string tabHost, string siteId, string token, string user, string userFullName, string userEmail)
              {
                  string userId = GetIdForTableauUser(tabHost, siteId, token, user);
                  XElement userRequest = new XElement("tsRequest",
                                              new XElement("user",
                                                  new XAttribute("fullName", userFullName),
                                                  new XAttribute("email", userEmail)
                                              )
                                          );
                  string tabUrl = "https://" + tabHost + "/api/3.1/sites/" + siteId + "/users/" + userId;
                  var updateUserResponse = SendTableauRequest(tabUrl, "PUT", userRequest.ToString(), token);
      
                  return updateUserResponse;
      }