locked
error: "An error was encountered while retrieving the user profile" in mysites RRS feed

  • Question

  • Hi,

    I have some user profiles configured with my sites in the user profile service application.
    Now, when i created a new tab in my sites and writing a custom code to display profile details of the user, i am getting this error:
    "An error was encountered while retrieving the user profile".

    my code is as follows:

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
    SPSite site = SPContext.Current.Site;
    SPServiceContext svcContext = null;
    svcContext = SPServiceContext.GetContext(site);
                    
    UserProfileManager uspManager = new UserProfileManager(SPServiceContext.GetContext(site), false);
    SPWeb web = site.OpenWeb();
    site.AllowUnsafeUpdates = true;
    web.AllowUnsafeUpdates = true;
    web.ValidateFormDigest();
    String accountName=domainName + "\\" + fname + "." + lname ;

    UserProfile userProfile = uspManager.GetUserProfile(accountName); //error occuring at this line.
                    
    lblName.Text = "<br/>"+userProfile[PropertyConstants.PreferredName].Value.ToString()+"<br/>";

    });


    when i am trying to pass the login name directly(like

    UserProfile userProfile = uspManager.GetUserProfile("SPDOM\\PVSAV.PSG");

    it is working. Is it a problem with my sites or any thing else?

    Please suggest me a solution for this.



    Thanks,

    PVSAVSG.


    Thanks, PVSAVSG.


    • Edited by pvsavsg Tuesday, March 13, 2012 6:56 AM modified code
    Tuesday, March 13, 2012 6:52 AM

Answers

  • Hi all,

    i got it atlast, by using the mysite url like this :

    string[] urlContainer = this.Request.Url.ToString().Split('=');
    UserProfile userProfile = upmgr.GetUserProfile(urlContainer[1]);

    Thanks all, for your valuable suggestions.



    Thanks, PVSAVSG.

    • Marked as answer by pvsavsg Tuesday, March 13, 2012 12:19 PM
    Tuesday, March 13, 2012 12:19 PM

All replies

  • Hi,

    Maybe this thread is helpful to you: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/62f953b5-214a-40b1-9280-ccb554ed68c6/


    Regards, Sjoukje

    Web: http://sjoukjezaal.com | LinkedIn: http://www.linkedin.com/in/sjoukjezaal | Twitter: @SjoukjeZaal

    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.

    Tuesday, March 13, 2012 6:59 AM
  • accountname  always in this format  "domain\\username"

    to get the userprofile  getuserProfile method you need to pass the accountname("domain\\username").

    http://msdn.microsoft.com/en-us/library/ms544918.aspx


    MCTS,MCPD Sharepoint 2010. My Blog- http://sharepoint-journey.com


    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful


    Tuesday, March 13, 2012 7:06 AM
  • Hi!

    Thanks for the reply.
    It works in the context of current user, but the problem is to get the details of the user profile, we are seeing.
    Is there any way we can get the rawsid of the user profile, we are seeing in mysites?


    Thanks, PVSAVSG.


    • Edited by pvsavsg Tuesday, March 13, 2012 7:33 AM
    Tuesday, March 13, 2012 7:28 AM
  • yeah, my user name is like firstname.lastname .

    doesn't it accepts the username with dots or any other symbols?
    If not, how is it taking the same when passing directly?

    Thanks, PVSAVSG.

    Tuesday, March 13, 2012 7:31 AM
  • HI,

    Username will always will something like domain\sAMAccountName.

    When you say the below works, then there shuldnt be an issue in manipulating string, the way you have done. But make sure before you pass the string to the GetUserProfile Method if it is in the same way as expected.

    UserProfile userProfile = uspManager.GetUserProfile("SPDOM\\PVSAV.PSG");

    But a point to note: usually the accountname is not likely to be fname+lname. Check if this is how it has been configured for all the users or only for PVSAV.PSG.

    Hope that gives you a hint.


    Sreeharsha Alagani | MCTS Sharepoint 2010 | Linkedin | Blog

    Tuesday, March 13, 2012 8:42 AM
  • yeah.. i checked it...infact i am saving the data from my external content source to user profile service application with the  specified fname.lname format....and now that they were in the domain SPDOM, i have to login with the samAccountName, which is "SPDOM\PVSAV.PSG". and, for information, i have logged into different my site profile with the specified formatted credentials and everything seems to be working great.

    i even the checked whether my string is resulting in proper samAccountName or not, using javascript alerts. everything is fine.....but i dont understand y is it not accepting when i m trying to pass it as a string....

    Can anyone check with this code in your environments and test it?

    Thanks, PVSAVSG.

    Tuesday, March 13, 2012 9:02 AM
  • So you say that,

    1. You are able to access the Sites using the account you specifed. But you are not able to get the user profile for the user pvsav.psg??

    I hope you have the user profile service started and profiles created.

    I am using the same code in my environment and it works. Here is how it is...

     public static string GetUserPicture(string siteUrl, string userAccount)
            {
                string pictureUrl = string.Empty;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(site);
                        UserProfileManager profileManager = new UserProfileManager(context, false);
                        UserProfile userProfile = profileManager.GetUserProfile(userAccount);
                        if (userProfile != null)
                        {
                            if (userProfile["PictureUrl"].Value != null)
                            {
                                pictureUrl = userProfile["PictureUrl"].Value.ToString();
                            }
                            else 
                            {
                                pictureUrl = "/_layouts/images/no_pic.gif";
                            }
                        }
                       
    
                    }
                });
                return pictureUrl;
            }


    Sreeharsha Alagani | MCTS Sharepoint 2010 | Linkedin | Blog

    Tuesday, March 13, 2012 9:39 AM
  • hi harsha,

    It is working for me in my sites when i am calling it like this:

    string picurl=GetUserPicture("SPDOM\\PVSAV.PSG");

    but not working(and throwing the same error "An error was encountered while retrieving the user profile.") when i m using it like this:
    string url=GetUserPicture(domainName+"\\"+fname+"."+lname);

    can u explain how can i dynamically pass the samAccountName to that method?


    Thanks, PVSAVSG.


    • Edited by pvsavsg Tuesday, March 13, 2012 10:08 AM error details mentioned
    Tuesday, March 13, 2012 10:04 AM
  • Hi all,

    i got it atlast, by using the mysite url like this :

    string[] urlContainer = this.Request.Url.ToString().Split('=');
    UserProfile userProfile = upmgr.GetUserProfile(urlContainer[1]);

    Thanks all, for your valuable suggestions.



    Thanks, PVSAVSG.

    • Marked as answer by pvsavsg Tuesday, March 13, 2012 12:19 PM
    Tuesday, March 13, 2012 12:19 PM