标签: sharepoint-userprofile

使用反射设置索引属性的值

我尝试使用反射复制以下 C# 代码:

UserProfileManager userProfileManager = new UserProfileManager(ServerContextGoesHere);
UserProfile userProfile = null;

userProfile = userProfileManager.GetUserProfile(@"somedomain\someuser");

userProfile["PictureUrl"].Value = "This is where I want to update the value using reflection!";

userProfile.Commit();
Run Code Online (Sandbox Code Playgroud)

使用反射,除了我尝试在 UserProfile 对象上设置“PictureUrl”索引属性的行之外,我可以让所有内容正常工作。使用反编译器时,索引属性如下所示:

public UserProfileValueCollection this[string strPropName]
Run Code Online (Sandbox Code Playgroud)

这是我的代码,使用反射来实现与上面相同的效果,请注意 TODO 注释,我需要在其中设置 PictureUrl 索引属性的值:

   Assembly userProfileAssembly;

    var windowsFolderPath = Environment.GetEnvironmentVariable("windir");
    var pathToServerAssembly = string.Format(@"{0}\assembly\GAC_MSIL\Microsoft.Office.Server.UserProfiles\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Server.UserProfiles.dll", windowsFolderPath);

    try
    {
        userProfileAssembly = Assembly.LoadFrom(pathToServerAssembly);
    }
    catch (FileNotFoundException)
    {
        // Assembly wasn't found, so eject.
        return;
    }

    var userProfileManagerClass = userProfileAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfileManager");
    if (userProfileManagerClass == null) return;

    var userExistsMethod …
Run Code Online (Sandbox Code Playgroud)

c# reflection sharepoint indexed-properties sharepoint-userprofile

3
推荐指数
1
解决办法
3677
查看次数