WebsitePanel API: Adding a User

Following on from my initial post on how to connect to the WebsitePanel API from ASP.net, I will demonstrate how to write data to the API by creating a new WebsitePanel user account.

As before, my example is a simple ASP.Net Webform. For details of prerequisites and the development environment I am using, please read the first post.

HTML:

Create User

Fields in bold are required.

Owner:
Load Error:
Send Email Alert

Role



Status




Is Demo
Username
Password
First name
Last Name
Email
Company Name
Address
City
State
Zip/Postal Code
Primary Phone
Secondary Phone
Fax
Instant Messenger
SecondaryEmail
HTML Email
 

Result:
Error:

Code Behind:


Imports System.Web.Services
Imports Microsoft.Web.Services3
Imports WebsitePanel.EnterpriseServer

Partial Class CreateUser
Inherits System.Web.UI.Page

#Region "Events & Initial Loading"

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then PopulateUsersDdl()
End Sub

Private Sub PopulateUsersDdl()
' Note: Uses userId of 1 and returns all users

' Create proxy configurator object
Dim config As New EnterpriseServerProxyConfigurator()
config.EnterpriseServerUrl = INSERT_WEBSITEPANEL_ENTERPRISESERVER_URL
config.Username = INSERT_WEBSITEPANEL_ENTERPRISESERVER_USERNAME
config.Password = INSERT_WEBSITEPANEL_ENTERPRISESERVER_PASSWORD

' Create & configure proxy object
Dim users As New WebsitePanel.EnterpriseServer.esUsers()
config.Configure(users)

' Perform lookup
Try
Dim lst As WebsitePanel.EnterpriseServer.UserInfo() = users.GetUsers(1, True)
ddlOwner.DataValueField = "UserId"
ddlOwner.DataTextField = "Username"
ddlOwner.DataSource = lst
ddlOwner.DataBind()
lst = Nothing
Catch ex As Exception
' If error, output and hide create button
ltlLoadError.Text = ex.Message
btnCreateUser.Visible = False
End Try

' Clean up
' - little bit of paranoia
users.Dispose()
users = Nothing
config = Nothing
End Sub

#End Region

#Region "User Creation"

Protected Sub btnCreateUser_Click(sender As Object, e As EventArgs) Handles btnCreateUser.Click
' Reset Output
ltlError.Text = ""
ltlResult.Text = ""

' Get details
Dim user As New WebsitePanel.EnterpriseServer.UserInfo
user.OwnerId = ddlOwner.SelectedValue

' Mandatory Parameters
user.RoleId = ddlRole.SelectedValue
user.StatusId = ddlStatus.SelectedValue
user.IsDemo = chkIsDemo.Checked
user.Username = txtUsername.Text
user.Password = txtPassword.Text
user.FirstName = txtFirstname.Text
user.LastName = txtLastname.Text
user.Email = txtEmail.Text

' Optional Parameters
user.CompanyName = txtCompanyName.Text
user.Address = txtAddress.Text
user.City = txtCity.Text
user.State = txtState.Text
user.Zip = txtPostcode.Text
user.PrimaryPhone = txtPrimaryPhone.Text
user.SecondaryPhone = txtSecondaryPhone.Text
user.Fax = txtFax.Text
user.InstantMessenger = txtInstantMessenger.Text
user.HtmlMail = chkHtmlEmail.Checked
user.SecondaryEmail = txtSecondaryEmail.Text

' Create proxy configurator object
Dim config As New EnterpriseServerProxyConfigurator()
config.EnterpriseServerUrl = ConfigurationManager.AppSettings("WebsitePanelServerUrl")
config.Username = ConfigurationManager.AppSettings("WebsitePanelUsername")
config.Password = ConfigurationManager.AppSettings("WebsitePanelPassword")

' Create & configure proxy object
Dim users As New WebsitePanel.EnterpriseServer.esUsers()
config.Configure(users)

' Attempt to save
Try
Dim result As Int32 = users.AddUser(user, chkSendEmail.Checked)

If (result > 0) Then
ltlResult.Text = String.Format("New UserId: {0}", result)
Else
Select Case result
Case -2
ltlResult.Text = "User creation failed: Invalid OwnerId specified"
Case -100
ltlResult.Text = "User creation failed: Username already exists"
Case -111
ltlResult.Text = "User creation failed: Username not specified"
Case Else
ltlResult.Text = String.Format("User creation failed: Result code of {0} returned", result)
End Select
End If
Catch ex As Exception
ltlError.Text = ex.Message
End Try

users = Nothing
user = Nothing
End Sub

#End Region

End Class

If successful, the value of result will be a positive integer which is the ID of the new user account. If a negative value is returned and error has occurred and the value is the error code. The error codes I have discovered so far are:

  • -2 = Invalid OwnerId specified
  • -100 = Username already exists
  • -111 = Username not specified

Testing Notes

  • Although the API documentation states that there are 10 required parameters, I have successfully created a new user by just specifying valid values for OwnerId, RoleId, StatusId and Username
  • An XML exception will occur if non-recognised values are specified for RoleId and StatusId

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.