How to use selected User-profile service in SharePoint

1.8k views Asked by At

Basically I am making a timer job to send a birthdayWish email fetched from SharePoint userprofile service. But problem is I have multiple userprofile services on server.

like 1). userprofile service1
2). userprofile service2
3). userprofile service3
4). userprofile service4

So how to use 2nd user-profile service.

Here's some code, that I did:

    SPServiceContext oServiceContext = SPServiceContext.GetContext(SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);

     UserProfileManager oProfileManager = new UserProfileManager(oServiceContext);

So here in SPServiceApplicationProxyGroup.Default its getting 1st user-profile.

Among them I want to use 2nd user-profile service. but by default when trying to access its getting 1st user-profile service and iterate through it.

So how to set it to use 2nd user-profile service.

1

There are 1 answers

1
Vedran Rasol On

My guess is that you have one User Profile Service Instance (UPS) with multiple User Profile Service Applications (UPA) on it (and not multiple UPS):

var userAppName = "userprofile service2";
SPFarm farm = SPFarm.Local;

SPServiceApplication application;
foreach (SPService s in farm.Services)
{
    if(s.TypeName.Contains("User Profile Service"))
    {
        //We found UPS
        foreach(SPServiceApplication app in s.Applications)
        {
            if(app.Name == userAppName)
                application = app; //Here is UPA
        }
    }
}

//Or if you like oneliner (not 100% safe)
var x = farm.Services.First(s => s.TypeName.Contains("User Profile Service"))
          .Applications.FirstOrDefault(app => app.Name == userAppName);