You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SharePoint 2013 JSOM获取当前用户Title报错:CLR检测到无效程序

SharePoint JSOM获取当前用户Title时抛出“Common Language Runtime detected an invalid program”错误

我编写了一段JavaScript代码,本意是获取SharePoint中当前用户的“Title”信息,但每次运行都会弹出错误提示:Common Language Runtime detected an invalid program。代码如下:

<script type="text/javascript"> 
var personProperties; 

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs. 
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js'); 

function getUserProperties() { 
    // Replace the placeholder value with the target user's credentials. 
    // var targetUser = "domainName\\userName"; 

    // Get the current client context and PeopleManager instance. 
    var clientContext = new SP.ClientContext.get_current(); 
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext); 

    // Get user properties for the target user. 
    // To get the PersonProperties object for the current user, use the 
    // getMyProperties method. 
    personProperties = peopleManager.getMyProperties(); 

    // Load the PersonProperties object and send the request. 
    clientContext.load(personProperties); 
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail); 
} 

// This function runs if the executeQueryAsync call succeeds. 
function onRequestSuccess() { 
    // Get a property directly from the PersonProperties object. 
    var messageText = personProperties.get_userProfileProperties()['Title']; 
    alert(messageText); 
} 

// This function runs if the executeQueryAsync call fails. 
function onRequestFail(sender, args) { 
    alert(args.get_message()); 
} 
</script>

请问各位有什么排查思路?感谢各位的建议。


以下是几个可以尝试的排查方向,帮你定位问题:

  • 确认脚本加载顺序的可靠性
    有时候SP.SOD.executeOrDelayUntilScriptLoaded依赖的SP.UserProfiles.js可能需要先确保SP.js已经加载完成。你可以调整加载逻辑,先加载核心的SP.js,再加载用户配置文件脚本:

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
        SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
    });
    

    这样能避免因核心上下文未初始化导致的异常。

  • 验证用户权限与身份
    这个错误有时候会在用户权限不足时出现——比如匿名用户,或者当前用户没有访问自己用户配置文件的权限。你可以先用管理员账号测试这段代码,如果能正常运行,就说明是权限问题,需要检查用户配置文件服务的权限设置。

  • 检查用户属性的实际键名
    不同的SharePoint环境中,用户配置文件的属性键名可能有差异。比如有些环境里“职位”对应的键名是JobTitle而非Title。你可以先在成功回调里打印所有属性,看看实际的键值对:

    function onRequestSuccess() {
        console.log(personProperties.get_userProfileProperties());
        // 从控制台输出里找到对应的职位属性键名
    }
    

    这样能确认你是否用对了属性名称。

  • 排除页面脚本冲突
    如果页面上还有其他自定义脚本,可能会和JSOM的对象产生冲突。你可以把这段代码放到一个空白的经典Web部件页里测试,如果能正常运行,就说明是原页面的脚本冲突问题,需要逐一排查其他脚本。

  • 查看服务器端日志获取详细错误
    这个CLR错误通常是服务器端抛出的,前端只收到了简化的提示。你可以查看SharePoint服务器的U日志(Unified Logging Service),找到对应的错误条目,里面会有更详细的异常信息,比如是否是程序集加载失败、用户配置文件服务异常等,这能帮你定位根本问题。


内容的提问来源于stack exchange,提问作者Tomáš Vítů

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:42:27