How to set avatar in user profile?
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"
, ???);
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
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;
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
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
@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;
imgAvatar.ImageUrl = img.MediaUrl +
".tmb"
;
will get you the default thumbnail size of 120width
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
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)
as
Telerik.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)
as
Telerik.Sitefinity.Libraries.Model.Image;
42.
librariesManager.SaveChanges();
43.
44.
var bag =
new
Dictionary<
string
,
string
>();
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.
1.
var bag =
new
Dictionary<
string
,
string
>();
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);
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)
as
Telerik.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)
as
Telerik.Sitefinity.Libraries.Model.Image;
42.
librariesManager.SaveChanges();
43.
44.
var bag =
new
Dictionary<
string
,
string
>();
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.
1.
var bag =
new
Dictionary<
string
,
string
>();
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);
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.
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.
Have you tried suppressing security checks on the workflowmanager?
Yes tried that as well. Does not work.
anyone has an update to this? I'm running into the same problem.