I am trying to run a piece code to figure out if a user has a MySite or not. Following is the code:

UserProfileManager profManager = new UserProfileManager();
UserProfile profile = profManager.GetUserProfile(Constants.MembershipProvider + ":" + username);
if (profile == null || profile.PersonalSite == null) {
      valueToReturn = false;

So the code works just fine when accessed under the context of a logged in user, but gets an access denied when trying to run anonymously. I tried wrapping the code with SPSecruity.RunWithElevatedPrevilege but to no avail.

Any idea how I can access the data for anonymous users?



let me see if i understand you correctly: if user is anonymous, does it make sense to check if it has a my site at all? It sure would have to share that site with alot of users :-)

hence you should avoid the call all together if user is anonymous.

several ways to see is user is anonymous. SPContext.Current.Site.Web.CurrentUser is one, another could be to wrap your call in an SPSecurityTrimmedControl and set target on SPBasePermissions or AuthenticationRestrictions.

hth Anders

  • Our site is not available anonymously, but we have a requirement to check if the user is has created their mysite from the login page. FYI, another way to check if a user is authenticated or not is through HttpContext.Current.User.Identity.IsAuthenticated property. – Emon Nov 24 '09 at 19:40
  • You need to clarify what you mean with "check if user has created their my site from login page". If the user havent logged in yet, how could you determine that? – Anders Rask Nov 25 '09 at 10:21

The problem is that even when running using RunWithElevated is that the constructor of the UserProfileManager, internally is using the SPContext. Therefore it's causing to fail when executing your code.

  • 1
    I think this could be the possible issue. I am trying the following code block: using(SPSite site = new SPSite(siteGuid)){ UserProfileManager upm = new UserProfileManager(ServerContext.GetContext(site)); } But keep getting a "The best overloaded method match for 'Microsoft.SharePoint.Portal.UserProfiles.UserProfileManager.UserProfileManager(Microsoft.SharePoint.Portal.PortalContext)'" How else can I initiate UserProfileManager specifying the context? – Emon Nov 24 '09 at 19:48

Unless SharePoint is setup to run anonymously you cannot execute the web services anonymously. What you can do though is set the code to run under a specified account before you run the GetUserProfile statement.

  • This is not web service. I am running this code from a code-behind. You would think wrapping the code with SPSecurity.RunWithElevatedPrevilege would work because at that point you are running under system account context. – Emon Nov 24 '09 at 1:54

Another thought is you could create a webservice that is your above code and then call that webservice and specify credentials from your anonymous page in the code behind.

In my experience anonymous and sharepoint can get very weird and unexpected sometimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.