Forum Discussion

WiskiOne's avatar
WiskiOne
New Contributor III
2 days ago
Solved

Issue Enabling User via API – SaveUser() Fails with “User Already Exists” Error

Hi!

We’ve developed a custom REST API in our OneStream application to automate user management. So far, the following methods are working successfully:

  • CreateUser – Creates a new user
  • DisableUser – Disables an existing user

Now, we are trying to add a new method:

EnableUser – This method receives a user name and sets the IsEnabled property to True.

The logic is quite simple. We retrieve the existing user using:

Public Sub EnableUser()
 
    ' Get user name
    Dim username As String = Me._UserProperties("name")
 
    If String.IsNullOrEmpty(username) Then Throw New Exception($"Supplied name is empty")
 
    ' Get user 
    Dim userToEnable As UserInfo = BRApi.Security.Authorization.GetUser(Me._Si, username)
 
    If userToEnable Is Nothing Then Throw New Exception($"User '{username}' was not found in OneStream")
 
    ' Set IsEnabled = True
    userToEnable.User.IsEnabled = True
 
    Try
        BRApi.Security.Admin.SaveUser(Me._Si, userToEnable.User, False, Nothing, TriStateBool.TrueValue)
    Catch ex As Exception
        Throw New Exception("Problem enabling user, Error message: " + Environment.NewLine + ex.GetBaseException.Message)
    End Try
 
    Me._Logger.AppendToLogger($"User '{username}' has been enabled")
 
End Sub
 

However, when we try to execute this method, we get the following error:

Problem enabling user, Error message:
Error saving User. 'Test' already exists.

This seems misleading because we're not trying to create a new user—we're retrieving an existing one and updating it. The createNewUser flag is explicitly set to False, and we are using the original UserInfo.User object returned by the API.

We’ve ensured that all key properties like Name, UserType, Email, etc., are populated.

It seems that SaveUser() is still attempting to create a new user instead of updating the existing one.

Has anyone encountered this behavior before? Is there a specific requirement or workaround to ensure SaveUser() correctly recognizes an update instead of a creation?

Any insights or guidance would be greatly appreciated.

Regards,

Xavi

  • In your SaveUser() call, you have set the fifth parameter, IsNew=True.  I would think with it set True, it will try to create the user, rather than update the user.

2 Replies

  • rhankey's avatar
    rhankey
    Contributor II

    In your SaveUser() call, you have set the fifth parameter, IsNew=True.  I would think with it set True, it will try to create the user, rather than update the user.

    • WiskiOne's avatar
      WiskiOne
      New Contributor III

      Rhankey,

      It is correct, it now works as expected. 

      Thank you