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 ?