1

I have custom claims provider which uses user profile service to get some data when resolving claims for user. I'm creating UserProfileManager this way:

private UserProfileManager UserProfileManager
        {
            get
            {
                if (_upm == null)
                {
                    SPServiceContext serviceContext = SPServiceContext.Current;                    
                    _upm = new UserProfileManager(serviceContext);
                }
                return _upm;
            }
        }

It works when I'm using gui for user management, because SPServiceContext.Current is present. I also have a timer job which performs some operation on user permissions, so it also uses my custom claim provider. The problem is, that there is no SPServiceContext.Current in OWSTIMER process and I can't create UserProfileManager instance.

How can I obtain service context for UserProfileManager in timer job (OWSTIMER process)?

1 Answer 1

1

Here is how you get SPServiceContext:

public void UserProfileSample()
{
    //get current service context
    string websiteUrl = "http://SampleName";
    SPSite site = new SPSite(strUrl);
    SPServiceContext serviceContext = SPServiceContext.GetContext(site);

   //initialize user profile config manager object
   UserProfileManager upm = new UserProfileManager(serviceContext);

}

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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