Cannot create AvontusUser

Avontus version: 7.1.1070.106

Code:

user = AvontusUser.GetUser (UserName);

user.LastName = parts[parts.Length-1];
user.LastName = parts[parts.Length];

user.Save();

Error:
—> System.Security.SecurityException: User not authorized to create object type AvontusUser at Avontus.Rental.Wrapper.VendorService.SetBusinessPartner(String XML) — End of inner exception stack trace —

Utilized both Administrator and Corporate Manager profiles that have user creation rights.

The same code on previous versions of Avontus worked OK.

Odd chunk of code there. What is it that you’re trying to accomplish by setting quantities on the last name? If you need a bit of a storage container, you can store xml in attachments somewhere.

Anyhow, this is likely caused by not logging in or wrong credentials when you log in, before you try and add a user. When you log in, make sure you’re grabbing the boolean that confirms that you’ve authenticated properly. Also be sure to make a call to Logout() on the principal object before you load the new one.

        // Logout to clear cache
        AvontusPrincipal.Logout();
        AvontusPrincipal.LoadPrincipal("username");

        // Log in
        bool success = AvontusPrincipal.Login("username", "yourpassword");

        // Write or fail depending on if logged in
        if (success)
            Console.WriteLine("Login successful");
        else
        {
            Console.WriteLine("Login failed");
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
            return;
        }

If that still doesn’t work can you send a bit more code in to troubleshoot?

Here is the code to log in, all values are stored in an XML config file.

_config = new ConfigObject ();

_config.server = rootWebConfig1.AppSettings.Settings [“LocalServerName”].Value;
_config.profile = rootWebConfig1.AppSettings.Settings [“AvontusProfile”].Value;
_config.user = rootWebConfig1.AppSettings.Settings [“AvontusUser”].Value;
_config.password = rootWebConfig1.AppSettings.Settings[“AvontusPassword”].Value;

CommonConfigurationSettings.IsDataPortal = false;
CommonConfigurationSettings.LocalServerName = _config.server;

AvontusPrincipal.LoadPrincipal(rootWebConfig1.AppSettings.Settings[“AvontusProfile”].Value);
AvontusPrincipal.Logout();

_config.connected = AvontusPrincipal.Login(_config.user, _config.password);

After login, that is when I issued the user and save command.

user = AvontusUser.NewUser();
user.FirstName = xnContact [“FirstName”].InnerText;
user.LastName = xnContact [“LastName”].InnerText;
… (etc)
user.Save();

So, I logout of the principal object before logging back in, I save the boolean and error out in case it did not log in.

Also, besides trying to save the user a save a vendor object in the same code block, and that does not show any issues.

Please advise.

Thanks.

Here is the code block that tries to create the user:

/* update contacts begin */
vendor.Save(); // this command works fine, I am able to save the vendor object

foreach (XmlNode xnContact in xnContactList)
{
VendorID = xnContact [“VendorNumVendorID”].InnerText;
UserName = xnContact [“VendorNumVendorID”].InnerText + “-” + xnContact [“PerConID”].InnerText;

appLog.WriteEntry ("each contact list");

user = AvontusUser.GetUser (UserName);

if (user.RelatedID == vendor.BusinessPartnerID) 
{
	if (xnContact ["FirstName"].InnerText == string.Empty || xnContact ["LastName"].InnerText == string.Empty)
	{
		string[] parts = xnContact ["Name"].InnerText.ToString().Split(' ');

		user.FirstName = parts[0];

		if (parts.Length > 0)
			user.LastName = parts[parts.Length-1];
		else
			user.LastName = parts[parts.Length];
	}

	else
	{
		user.FirstName = xnContact ["FirstName"].InnerText;
		user.LastName = xnContact ["LastName"].InnerText;
	}

	user.Initials = xnContact ["Initials"].InnerText;
	user.Title = xnContact ["ContactTitle"].InnerText;
	user.EmailAddress = xnContact ["EmailAddress"].InnerText;
	user.PhoneNumber = xnContact ["PhoneNum"].InnerText;
	user.FaxNumber = xnContact ["FaxNum"].InnerText;
	user.CellNumber = xnContact ["CellPhoneNum"].InnerText;

	appLog.WriteEntry ("existing user");

	user.Save();
} 

else 
{
	user = AvontusUser.NewUser();

	if (xnContact ["FirstName"].InnerText == string.Empty || xnContact ["LastName"].InnerText == string.Empty)
	{
		string[] parts = xnContact ["Name"].InnerText.ToString().Split(' ');

		user.FirstName = parts[0];

		if (parts.Length > 0)
			user.LastName = parts[parts.Length-1];
		else
			user.LastName = parts[parts.Length];
	}

	else
	{
		user.FirstName = xnContact ["FirstName"].InnerText;
		user.LastName = xnContact ["LastName"].InnerText;
	}

	user.Initials = xnContact ["Initials"].InnerText;
	user.Title = xnContact ["ContactTitle"].InnerText;
	user.EmailAddress = xnContact ["EmailAddress"].InnerText;
	user.PhoneNumber = xnContact ["PhoneNum"].InnerText;
	user.FaxNumber = xnContact ["FaxNum"].InnerText;
	user.CellNumber = xnContact ["CellPhoneNum"].InnerText;
	user.Username = UserName;
	user.Password = "integrationpwd1";
	user.RelatedID = vendor.BusinessPartnerID;
	user.UserType = UserTypes.Vendor;

	appLog.WriteEntry ("new user");

	user.Save();
}

}

/* update contacts end */

Are you trying to create a user (seen in the users tab) or a contact (related to and seen in the list of contacts on a customer or vendor)? I see code that could be used for either here…

This is to create a contact.

Still having the issue on both customer or vendor.

We switched from using the “Corporate Manager” profile (which had user creation perimissions) to the Administrator profile.

We were still seeing issues until we manually logged in with the account using the Avontus UI and now we are able to create contacts for vendors and customers.

Yes, for this you’ll want to create a specific type of user. Sample code below for a customer:

BusinessPartner customer = BusinessPartner.NewBusinessPartner(PartnerTypes.Customer);
customer.Name = “My New Customer”;
customer.Save();

        AvontusUser user = AvontusUser.NewUser(UserTypes.Customer);
        user.RelatedID = customer.BusinessPartnerID;
        user.FirstName = "First Name";
        user.LastName = "Last Name";

        // Get/set the role for the customer. Note that these
        // are enum IDs that may match to a different name
        RoleList roles = RoleList.GetRoles(false, true);
        user.RoleID = roles.GetRoleByType(AvontusRoleType.Customer).RoleID;

        // Assign random part of guid so that the username is unique
        // If you assign a specific username you can trap an exception
        // during save and detect if the unique rule is broken. Let
        // me know if you'd like code to see how this works.
        user.Username = Guid.NewGuid().ToString().Substring(0, 5);
        user.Password = Guid.NewGuid().ToString().Substring(0, 5); 

        if (user.IsSavable)
            user.Save();
        else
        {
            foreach (Avontus.Core.Validation.BrokenRule rul in user.BrokenRulesCollection)
            {
                Console.WriteLine(rul.Description);
            }
        }