How to set avatar in user profile?

Posted by Community Admin on 03-Aug-2018 03:45

How to set avatar in user profile?

All Replies

Posted by Community Admin on 22-Apr-2011 00:00

Hi,

I'm using the new Sitefinity 4.1 API to create user profile. I was able to set the FirstName and LastName but I would like to know how to set the Avatar field and what is the type of that field.

var profileManager = UserProfileManager.GetManager();
var profile = profileManager.CreateProfile(newUser, typeof(SitefinityProfile).FullName);
Telerik.Sitefinity.Model.DataExtensions.SetValue(profile, "FirstName", txtFirstName.Text);
Telerik.Sitefinity.Model.DataExtensions.SetValue(profile, "LastName", txtLastName.Text);
Telerik.Sitefinity.Model.DataExtensions.SetValue(profile, "Avatar", ???);

Thanks

Posted by Community Admin on 22-Apr-2011 00:00

Hi Raihan,

SitefinityProfile has a property Avatar that you can use directly. The filedName is "Avatar".
As a value you can pass Telerik.Sitefinity.Libraries.Model.Image - MediaUrl + .tmb

Regards,
Ivan Dimitrov
the Telerik team


Posted by Community Admin on 22-Apr-2011 00:00

Hi Ivan,

I have another question. How to retrieve an URL for avatar? I've implemented the following code:

protected void Page_Load(object sender, EventArgs e)
    UserManager userManager = UserManager.GetManager();
    User user = userManager.GetUser("admin");
    UserProfileManager userProfileManager = UserProfileManager.GetManager();
    UserProfile profile = userProfileManager.GetUserProfile(user.Id, typeof(SitefinityProfile).FullName);
    ContentLink v = Telerik.Sitefinity.Model.DataExtensions.GetValue(profile, "Avatar") as ContentLink;
     

It returns the object of Telerik.Sitefinity.Model.ContentLinks.ContentLink type. How to get an image URL from it?

Thank in advance,
Anton.

Posted by Community Admin on 23-Apr-2011 00:00

Can you please elaborate your code?

Telerik.Sitefinity.Libraries.Model.Image - MediaUrl + .tmb

Why the minus "-" sign?? And what is .tmb?

Please can you provide a complete code sample or a link to a tutorial?

Thanks

Posted by Community Admin on 25-Apr-2011 00:00

Hi,

In my code I am referring to a thumbnail of an images, because this is more convenient than scaling the image again. You can check the media modules API

Kind regards,
Ivan Dimitrov
the Telerik team


Posted by Community Admin on 28-Apr-2011 00:00

@Anton

Hi,

I had the same problem, this worked for me;

Regards,

  Peter

ContentLink v = p1.GetValue("Avatar"as ContentLink;
var images = App.WorkWith().Images().Where(w => w.Id == v.ChildItemId).Get();
Telerik.Sitefinity.Libraries.Model.Image img = images.FirstOrDefault();
if (img != null) 
  imgAvatar.ImageUrl = img.MediaUrl;




Posted by Community Admin on 03-May-2011 00:00

imgAvatar.ImageUrl = img.MediaUrl + ".tmb";

will get you the default thumbnail size of 120width

Posted by Community Admin on 04-May-2011 00:00

Hello Kristian,

This should return you the image in the same size which will be generated when you type

host/.../myimage.png.tmb

All the best,
Ivan Dimitrov
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 27-Jun-2012 00:00

Dear Community,

We have frontend users where they can update their profile Avatar along with their profile data. For this we have created a custom user control where users edit their profile data along with Avatar. Since users are not backend user and do not have enough rights, we are facing problems in updating this data. 

However we are able  to update user & profile data by setting SuppressSecurityChecks to true. Similarly we are also able to upload Avatar Image to a Album by setting SuppressSecurityChecks to true for librariesManager.Provider.

However when trying to publish this image using WorkflowManager.MessageWorkflow we are getting persmission issues. We are unable to suppress this security check. Kindly advise how can we resolve this issue.

Here is the code that we are using:

01.protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
02.        
03. 
04.            var manager = UserManager.GetManager();
05.            var user = manager.GetUser(HttpContext.Current.User.Identity.Name);
06.            Guid masterImageId = Guid.NewGuid();
07.            if (user != null)
08.            
09.                var profilemanager = UserProfileManager.GetManager();
10.                var uProfile = profilemanager.GetUserProfile(user,typeof(SitefinityProfile).FullName) as SitefinityProfile;
11.                if (uProfile != null)
12.                
13.                    var pAvatar = uProfile.Avatar;
14.                    if(pAvatar != null)
15.                        masterImageId = pAvatar.ChildItemId;
16. 
17.                    LibrariesManager librariesManager = LibrariesManager.GetManager();
18.                    Telerik.Sitefinity.Libraries.Model.Image temp = null;
19.                    Telerik.Sitefinity.Libraries.Model.Image master = librariesManager.GetImages().Where(i => i.Id == masterImageId).FirstOrDefault();
20. 
21.                    librariesManager.Provider.WithSuppressedSecurityChecks(() =>
22.                        
23.                            if (master != null)
24.                                temp = librariesManager.Lifecycle.CheckOut(master) asTelerik.Sitefinity.Libraries.Model.Image;
25.                            else
26.                            
27.                                temp = librariesManager.CreateImage(masterImageId);
28.                                temp.DateCreated = DateTime.UtcNow;
29.                                temp.PublicationDate = DateTime.UtcNow;
30.                            
31.                            Album album = librariesManager.GetAlbums().Where(i => i.Title == "UserProfile").SingleOrDefault();
32.                            temp.Parent = album;
33.                            temp.Title = user.UserName;
34.                            temp.LastModified = DateTime.UtcNow;
35.                            temp.UrlName = Regex.Replace(temp.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+""-");
36. 
37.                            //Upload the image file.
38.                            librariesManager.Upload(temp, e.File.InputStream, e.File.GetExtension());
39. 
40.                            if (master != null)
41.                                master = librariesManager.Lifecycle.CheckIn(temp) asTelerik.Sitefinity.Libraries.Model.Image;
42.                            librariesManager.SaveChanges();
43. 
44.                            var bag = new Dictionary<stringstring>();
45.                            bag.Add("ContentType",typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
46.                            WorkflowManager.MessageWorkflow(masterImageId,typeof(Telerik.Sitefinity.Libraries.Model.Image), null"Publish"false, bag);
47. 
48.                            if (pAvatar == null)
49.                            
50.                                ContentLink avatarLink = ContentLinksExtensions.CreateContentLink(uProfile, temp);
51.                                uProfile.Avatar = avatarLink;
52.                                profilemanager.SaveChanges();
53.                            
54.                            Thumbnail.ImageUrl = common.GetImageUrl(masterImageId,true);
55.                        );
56.                
57.            
58.        

Following is the area which causes this error
1.var bag = new Dictionary<stringstring>();
2.                           bag.Add("ContentType",typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
3.                           WorkflowManager.MessageWorkflow(masterImageId,typeof(Telerik.Sitefinity.Libraries.Model.Image), null"Publish"false, bag);

Error is as follows:
You are not authorized to 'Modify library and manage images' ('Image').  

Thanks in advance.

Regards,
Raj

Posted by Community Admin on 27-Jun-2012 00:00

Dear Community,

We have frontend users where they can update their profile Avatar along with their profile data. For this we have created a custom user control where users edit their profile data along with Avatar. Since users are not backend user and do not have enough rights, we are facing problems in updating this data. 

However we are able  to update user & profile data by setting SuppressSecurityChecks to true. Similarly we are also able to upload Avatar Image to a Album by setting SuppressSecurityChecks to true for librariesManager.Provider.

However when trying to publish this image using WorkflowManager.MessageWorkflow we are getting persmission issues. We are unable to suppress this security check. Kindly advise how can we resolve this issue.

Here is the code that we are using:

01.protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
02.        
03. 
04.            var manager = UserManager.GetManager();
05.            var user = manager.GetUser(HttpContext.Current.User.Identity.Name);
06.            Guid masterImageId = Guid.NewGuid();
07.            if (user != null)
08.            
09.                var profilemanager = UserProfileManager.GetManager();
10.                var uProfile = profilemanager.GetUserProfile(user,typeof(SitefinityProfile).FullName) as SitefinityProfile;
11.                if (uProfile != null)
12.                
13.                    var pAvatar = uProfile.Avatar;
14.                    if(pAvatar != null)
15.                        masterImageId = pAvatar.ChildItemId;
16. 
17.                    LibrariesManager librariesManager = LibrariesManager.GetManager();
18.                    Telerik.Sitefinity.Libraries.Model.Image temp = null;
19.                    Telerik.Sitefinity.Libraries.Model.Image master = librariesManager.GetImages().Where(i => i.Id == masterImageId).FirstOrDefault();
20. 
21.                    librariesManager.Provider.WithSuppressedSecurityChecks(() =>
22.                        
23.                            if (master != null)
24.                                temp = librariesManager.Lifecycle.CheckOut(master) asTelerik.Sitefinity.Libraries.Model.Image;
25.                            else
26.                            
27.                                temp = librariesManager.CreateImage(masterImageId);
28.                                temp.DateCreated = DateTime.UtcNow;
29.                                temp.PublicationDate = DateTime.UtcNow;
30.                            
31.                            Album album = librariesManager.GetAlbums().Where(i => i.Title == "UserProfile").SingleOrDefault();
32.                            temp.Parent = album;
33.                            temp.Title = user.UserName;
34.                            temp.LastModified = DateTime.UtcNow;
35.                            temp.UrlName = Regex.Replace(temp.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+""-");
36. 
37.                            //Upload the image file.
38.                            librariesManager.Upload(temp, e.File.InputStream, e.File.GetExtension());
39. 
40.                            if (master != null)
41.                                master = librariesManager.Lifecycle.CheckIn(temp) asTelerik.Sitefinity.Libraries.Model.Image;
42.                            librariesManager.SaveChanges();
43. 
44.                            var bag = new Dictionary<stringstring>();
45.                            bag.Add("ContentType",typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
46.                            WorkflowManager.MessageWorkflow(masterImageId,typeof(Telerik.Sitefinity.Libraries.Model.Image), null"Publish"false, bag);
47. 
48.                            if (pAvatar == null)
49.                            
50.                                ContentLink avatarLink = ContentLinksExtensions.CreateContentLink(uProfile, temp);
51.                                uProfile.Avatar = avatarLink;
52.                                profilemanager.SaveChanges();
53.                            
54.                            Thumbnail.ImageUrl = common.GetImageUrl(masterImageId,true);
55.                        );
56.                
57.            
58.        

Following is the area which causes this error
1.var bag = new Dictionary<stringstring>();
2.                           bag.Add("ContentType",typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
3.                           WorkflowManager.MessageWorkflow(masterImageId,typeof(Telerik.Sitefinity.Libraries.Model.Image), null"Publish"false, bag);

Error is as follows:
You are not authorized to 'Modify library and manage images' ('Image').  

Thanks in advance.

Regards,
Raj

Posted by Community Admin on 27-Jun-2012 00:00

Hmm, i don't see any reason why you can't use the default profile widget Sitefinity provides (Users > Profile). Then just edit the template to the fields you want to display/edit. Also the avatars are stored in the Default Image library. You should set permissions to that library to allow modifications by authenticated users. I just dedicated that library for user profiles and make another one for images I want to use elsewhere.

Posted by Community Admin on 27-Jun-2012 00:00

Hi Kristian,
Thanks for replying back. We have certain custom logic to be applied and requires us to use custom control and not profile widget provided by Sitefinity.

We have a separate role created for the frontend site users and have given rights to modify images to that role for that library but while publishing using WorkflowManager it still gives the authorization error.

Do you have any idea to suppress security checks for publishing images just like we do using SuppressSecurityChecks for usermanager, profilemanager & Librarymanager.

Thanks for all your help. Really appreciate it.

Regards,
Raj

Posted by Community Admin on 27-Jun-2012 00:00

Have you tried suppressing security checks on the workflowmanager?

Posted by Community Admin on 27-Jun-2012 00:00

Yes tried that as well. Does not work.

Posted by Community Admin on 12-Mar-2013 00:00

anyone has an update to this? I'm running into the same problem.

This thread is closed