Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to make some manipulation with the user profiles from a console application.

I'm trying to enumerate the profiles, with no success :

        var mysite = "http://my";

        SPSecurity.RunWithElevatedPrivileges(() =>{
            var site = new SPSite(mysite);
            var ctx = SPServiceContext.GetContext(site);
            var upm = new UserProfileManager(ctx);

            foreach (UserProfile profile in upm)
            {
                var login = profile["AccountName"].Value;
                Console.WriteLine(login);
            }

        });

This code throw an exception at the creation of the UserProfileManager instance :

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.Office.Server.UserProfiles
  StackTrace:
       at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
       at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.CheckAdministrationAccess(UserProfileApplicationAdminRights rights, Boolean requireAllRights)
       at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.CheckAdministrationAccess(UserProfileApplicationAdminRights rights)
       at Microsoft.Office.Server.UserProfiles.ProfileManagerBase.CanManagePeople(UserProfileApplicationProxy userProfileApplicationProxy)
       at Microsoft.Office.Server.UserProfiles.ProfileManagerBase.get_IsProfileAdmin()
       at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext, Boolean IgnoreUserPrivacy, Boolean backwardCompatible)
       at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext)
       at MyApp.Program.<>c__DisplayClass6.<Main>b__3() in C:\Projects\MyApp\MyApp\Program.cs:line 25
       at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
       at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
  InnerException: 

What can be the cause of the error ?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Most likely, this is a permission issue. If you are administrator, make sure the conole app runs as administrator. Check link below:

http://sureshannamalai.blogspot.com/2011/08/get-user-profile-using-console-app.html

share|improve this answer
1  
Actually, the error come because my user, even if he is farm administrator, was not explicitly granted full control on the service. Thanks for putting me on the right way. Non-thanks to Microsoft for such awful error message –  Steve B Jan 17 '12 at 8:25
    
hell yeah u right, about error message. –  Jalali Shakib Apr 26 '12 at 4:53
add comment

It seems like the userprofile object is returning null. This can happen due to permission rights for the user, even though it is running with SPSecurity.RunWithElevatedPrivileges().

Can you try out the solution provided in this post? - http://socialsp.com/2010/11/25/access-denied-messages-when-retrieving-sharepoint-user-profiles-count-even-running-in-elevated-code/

Finally, your code would look like this -

var mysite = "http://my";
HttpContext tempCtx = HttpContext.Current;
        try
       {
        SPSecurity.RunWithElevatedPrivileges(() =>{
            var site = new SPSite(mysite);
            var ctx = SPServiceContext.GetContext(site);
            HttpContext.Current = null;
            var upm = new UserProfileManager(ctx);
            foreach (UserProfile profile in upm)
            {
                var login = profile["AccountName"].Value;
                Console.WriteLine(login);
            }
        });
        }
        catch (Exception ex){ LogException(ex);}
        finally{ HttpContext.Current = tempCtx;}
share|improve this answer
    
I work with a console application. This workaround does not apply as HttpContext is already null. –  Steve B Jan 17 '12 at 8:14
1  
I guess I missed that. It should be permission issues. Make sure the app is running under Admin privileges. Did you try the User Profile Service in Central Admin? Click Permisions in the ribbon and add the user with Full Control permissions. –  Deepu Nair Jan 17 '12 at 8:26
    
it is the solution, as Ashish Patel mentionned in its answer. thx for the help anyway –  Steve B Jan 17 '12 at 8:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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