How to use selected User-profile service in SharePoint

时光毁灭记忆、已成空白 提交于 2019-12-04 22:43:15

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!