Forum


HomeHomePremiumPremiumDevelopmentDevelopmentCan't Update UserInfo.FirstName Can't Update UserInfo.FirstName
Previous
 
Next
New Post
5/12/2017 6:44 AM
 

I'm building a module to replace the default DNN user profile. I've had success but I have encountered an issue I haven't been able to work around. I can update profile properties and see that the changes in the database however when I go to retrieve the properties it's the old value... One thought I had was there was a caching issue. I tried DataCache.ClearUserCache(PortalSettings.PortalId, UserInfo.Username); and DataCache.ClearCache(); which had no effect. Something that I just found is there are actually two FirstName properties in the ProfilePropertyDefinition table: one PropertyCategory 'Name' and one in category 'Basic'. I can see that the 'Name' value is used by Superusers and the 'Basic' property is used by all other users (not tested). The 'Basic':FirstName value never updates with my code and no entry for 'Name':Firstname is added. I thought some of the other properties were updating correctly but it turns out this is not the case. It seems like sometimes the profile updates correctly and sometimes it doesn't... Is there some kind of flag RecentlyUpdated that doesn't allow you to update your properties multiple times in a day/hour? I can't figure out what's going on here. Here's my update code:

switch (property.Name)
                {
                    case "FirstName":
                        UserInfo.FirstName = property.Value;
                        break;
                    case "LastName":
                        UserInfo.LastName = property.Value;
                        break;
                    default:
                        UserInfo.Profile.SetProfileProperty(property.Name, property.Value);
                        break;
                }
                
                DotNetNuke.Entities.Profile.ProfileController.UpdateUserProfile(UserInfo);
                UserController.UpdateUser(PortalSettings.PortalId, UserInfo);
                DataCache.ClearUserCache(PortalSettings.PortalId, UserInfo.Username);
                DataCache.ClearCache();

 
New Post
5/12/2017 9:39 AM
 

Hi Michael,

I'm not a super DNN programmer, but this is a sample code that I have been using with success for many of my DNN sites:

               userInfo.FirstName = FirstName;
               userInfo.LastName = LastName;
               userInfo.DisplayName = Name;
               userInfo.Email = Email.ToLower().Trim();
               userInfo.PortalID = 0;

               userInfo.Profile.FirstName = FirstName;
               userInfo.Profile.LastName = LastName;
               userInfo.Profile.Country = Country;
               userInfo.Profile.Region = Region;

               UserController.UpdateUser(0, userInfo);

 

It works for me. Similar to yours but different in the sense that I use

               userInfo.Profile.FirstName = FirstName;
               userInfo.Profile.LastName = LastName;

Cheers,

Aderson

 
New Post
5/12/2017 9:55 AM
 

Thanks again for your reply Aderson. I ended up taking a different approach to finally get it working.

The issue is with the version of DNN I'm using (8.0.4). Apparently somewhere along the way the UpdateUserProfile method on the ProfileController was broken. I was able to track someone down with a similar issue to mine in this post. What fixed it for him did not 100% work for me. It would let me set a property once then it wouldn't function anymore. Same problem. However what I did was take his approach and go straight to the source code which I obtained from github. What I found was if you use a specific overload of the UpdateUserProfile after clearing both the portal cache and 'general' cache it works. The working method takes a UserInfo object and ProfilePropertyDefinitionCollection object. This is a known bug and will be fixed in DNN9. I'll post working code for anyone who may run across this issue:

// property is an object with a 'Name' and 'Value' property that contains the name of the property to update and the value to set it to.

var user = UserController.GetUserById(PortalSettings.PortalId, UserId);
var properties = new ProfilePropertyDefinitionCollection();
var userprofile = UserInfo.Profile;
var toUpdate = userprofile.GetProperty(property.Name);
// this will set the value on the user object
toUpdate.PropertyValue = property.Value;

foreach (ProfilePropertyDefinition profProperty in userprofile.ProfileProperties)
{
    properties.Add(profProperty);
}
                
// both caches must be cleared before you attempt to update
DataCache.ClearPortalCache(PortalSettings.PortalId, true);
DataCache.ClearCache();
// other overloads of this method were broken in Dnn8 so you must use this overload
DotNetNuke.Entities.Profile.ProfileController.UpdateUserProfile(user, properties);

Hope this stops someone. Thanks.

 
New Post
5/12/2017 10:20 AM
 

Thank you for sharing that with us Michael. How can someone figure this out! :)

That is why coding documentation is so important and a lot of us complain about that in DNN.

Cheers,

Aderson

 
Previous
 
Next
HomeHomePremiumPremiumDevelopmentDevelopmentCan't Update UserInfo.FirstName Can't Update UserInfo.FirstName



Try FREE
30 days money back guaranteed