Search code examples
asp.netdotnetnukedotnetnuke-module

How to update dotnetnuke userprofile image in codebehind?


I am building my own "user profile" module where one of the options, the user can change his default dnn profile image. I am having problems doing this "in the code behind". I am using c#.

This is what I have so far:

UserInfo myDnnUser = this.UserInfo;
myDnnUser.Profile.InitialiseProfile(PortalId);

myDnnUser.Profile.SetProfileProperty("Photo", "new filename");
myDnnUser.Profile.SetProfileProperty("PhotoURL", "new url");

ProfileController.UpdateUserProfile(myDnnUser);

But its not working, and when I view the "File" table that dnn uses, its still the same (old) filename.

Any ideas?


Solution

  • There are three tables involved: UserProfile, ProfilePropertyDefinition and Files.

    UserProfile stores PropertyValues for ProfilePropertyDefinitions.

    Expected PropertyValue for a "Photo" PropertyName is a FileID reference to the Files table, not a file name. Before setting the Photo, you need to get the FileID:

        var objFiles = new FileController();
        FileInfo objFile = objFiles.GetFile("filepath", PortalID);
        myDnnUser.Profile.Photo = objFile.FileId;
        ProfileController.UpdateUserProfile(myDnnUser);
    

    PhotoURL is a read-only property that retrieves the url for the UserProfile's Photo property.