Developer forum

Forum » Development » Creating/saving user programatically

Creating/saving user programatically

Marco Santos
Reply
Hello.

I am developing a web service that will syncronize user information between two systems. I have a function defined like this :

        private void SaveUser(UserSyncRequest request)
        {
            Dynamicweb.Modules.UserManagement.User.set_Current(PagePermissionLevels.Backend, Dynamicweb.Modules.UserManagement.User.GetUserByID(4));

            string userSQL = string.Format("SELECT * FROM AccessUser WHERE AccessUserEmail = '{0}' AND AccessUserType = 15", request.Contact.Email.Trim());
            Dynamicweb.Modules.UserManagement.User userToSave = Dynamicweb.Modules.UserManagement.User.GetUserBySql(userSQL);

            if (userToSave == null)
            {
                userToSave = new Dynamicweb.Modules.UserManagement.User();
                userToSave.Type = UserType.ExtranetUser;
            }

            userToSave.UserName = request.LoginName;
            userToSave.Password = request.Password;

            userToSave.Name = string.Format("{0} {1}", request.Contact.Name, request.Contact.Surnames);
            userToSave.Email = request.Contact.Email;
            userToSave.Phone = request.Contact.MainPhoneNumber;

            userToSave.Address = request.Address.AddressLine1;
            userToSave.Address2 = request.Address.AddressLine2;
            userToSave.Zip = request.Address.PostCode;
            userToSave.City = request.Address.City;

            userToSave.Country = request.Address.Country;

            userToSave.Save();
        }

I'm getting this error though, and can't really see what is required to get rid of it.


Object reference not set to an instance of an object.
   at Dynamicweb.Modules.UserManagement.User.set_Current(PagePermissionLevels level, User value)
   at CRMIntegrationService.IntegrationService.SaveUser(UserSyncRequest request) in C:\Websites\DeltaQCRM\Application\Public\DeltaQCRM\IntegrationService.asmx.cs:line 76
   at CRMIntegrationService.IntegrationService.SyncIndividualUserDetails() in C:\Websites\DeltaQCRM\Application\Public\DeltaQCRM\IntegrationService.asmx.cs:line 39


Can anyone tell me what I am missing? Thanks.

Marco


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
Hi there,

The Dynamicweb API relies on the presence of Session state. A Web Service by default does not support Session state. So when you access the API from a Web Service, Session state is not available and you get Null Reference exceptions. If this is indeed the cause of your error (not entirely sure), fixing it is easy: just mark you service method (assuming an ASMX service) with the WebMethod attribute with its EnableSession property set to true:
[WebMethod(EnableSession = true)]

Hope this helps,

Imar
 
Morten Bengtson
Reply
Hi Marco,

Are you aware that there is a standard web service for synchronizing users?

/Admin/Public/WebServices/UserManagement/UserSync.asmx

You can create/update/delete basic user information using that web service. However it did not support custom fields last time i checked.
It requires that you specify a token, which is the installation checksum you can find in Management Center > Sytem information

The standard web service functionality is very limited, so it might not be suitable in this case.

/Morten
 
Marco Santos
Reply
I didi not know about this web service, but even tough it returns the result has being successfull, I am unable to update the details of an existing user, I can only create new users. Not the best option, but I think I am just going to update the table info.

Thanks for the help anyway.

 

You must be logged in to post in the forum