在Microsoft.Office.Server.UserProfiles.UserProfileManager上抛出InvalidProgramException

时间:2016-02-17 09:38:19

标签: c# sharepoint-2013 web-parts

我正在制作一个需要使用以下代码从活动目录中获取当前用户信息的webpart:

protected void fetchUserInfo()
{
    System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
    ps.Assert();

    Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current;
    UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
    ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;
    string userName = SPContext.Current.Web.CurrentUser.LoginName;
    UserProfile profile = upm.GetUserProfile(userName);

    foreach (ProfileSubtypeProperty prop in pspm.PropertiesWithSection)
    {
    }
}

然而,在行ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;

上抛出InvalidProgramException

错误消息是:

  

Common Language Runtime检测到无效程序。

我试过谷歌搜索 Common Language Runtime detected an invalid program. sharepointUserProfileManager但是没有太多信息。

可能是什么问题?

刚才看到异常在UserProfileManager上流动,因此SPServiceContext无效,当我在serviceContext上查看属性SiteSubscriptionId时,我发现它是00000000-0000-0000-0000-000000000000

那是什么意思?有没有其他方法可以从sharepoint获取当前用户信息?

1 个答案:

答案 0 :(得分:0)

最后,我改变了主意,使用UserPrincipal来获取用户信息:

    PrincipalContext _principalcontext = GetPrincipalContext();
    UserPrincipal _user = UserPrincipal.FindByIdentity(_principalcontext, IdentityType.SamAccountName, UID);

    if (_user != null)
    {
        DirectoryEntry directoryEntry = _user.GetUnderlyingObject() as DirectoryEntry;
        //Property is here;
    }
相关问题