空白SharePoint站点中获取用户团队负责人并填充至指定输入框的方法咨询
Got it, let's figure out how to pull the current user's team leader (manager) info and populate that teamLeader input field in your SharePoint site!
Since you're already using jQuery to populate the email field, we can extend that workflow to fetch the manager details using SharePoint's REST API (or the client-side object model, if you prefer). Here's how to do it:
方法1:使用SharePoint REST API(推荐,和你现有代码风格一致)
We'll first fetch the current user's info, then use their ID to retrieve their manager's details from SharePoint's user profile.
// 获取当前用户并填充邮箱,同时获取经理信息 $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser", method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function(userData) { // 填充用户邮箱到输入框 const userEmail = userData.d.Email; $("#emailaddress").val(userEmail); // 用当前用户ID请求经理信息 const userId = userData.d.Id; $.ajax({ url: `${_spPageContextInfo.webAbsoluteUrl}/_api/web/siteusers/getbyid(${userId})/manager`, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function(managerData) { // 这里可以选择填充经理的姓名(Title)或者邮箱(Email) // 示例:填充经理姓名 $("#teamLeader").val(managerData.d.Title); // 如果需要填充邮箱,替换成下面这句: // $("#teamLeader").val(managerData.d.Email); }, error: function(error) { console.error("获取经理信息失败:", error); // 处理无经理的情况,比如设置默认提示 $("#teamLeader").val("未找到团队负责人信息"); } }); }, error: function(error) { console.error("获取当前用户信息失败:", error); } });
方法2:使用SharePoint客户端对象模型(SP.js)
If you prefer using SharePoint's native client-side API instead of jQuery for the manager lookup, here's an alternative:
// 确保SP.js加载完成后执行 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { const context = SP.ClientContext.get_current(); const web = context.get_web(); const currentUser = web.get_currentUser(); // 加载当前用户信息 context.load(currentUser); context.executeQueryAsync( function() { // 填充用户邮箱 $("#emailaddress").val(currentUser.get_email()); // 获取并加载经理信息 const manager = currentUser.get_manager(); context.load(manager); context.executeQueryAsync( function() { if (manager) { // 填充经理姓名 $("#teamLeader").val(manager.get_title()); // 或者填充邮箱:manager.get_email() } else { $("#teamLeader").val("未找到团队负责人信息"); } }, function(sender, args) { console.error("获取经理信息失败:", args.get_message()); $("#teamLeader").val("获取团队负责人信息失败"); } ); }, function(sender, args) { console.error("获取当前用户信息失败:", args.get_message()); } ); });
关键注意事项
- 权限要求: 当前用户需要有读取用户配置文件数据的权限,否则API会返回403错误。
- 经理字段配置: 确保当前用户的"Manager"属性已经在Active Directory或SharePoint用户配置文件中设置,否则API会返回空值或错误。
- 错误处理: 我在代码里加了基础的错误处理,你可以根据需求调整提示内容或逻辑。
内容的提问来源于stack exchange,提问作者Goja
相关产品推荐
相关产品推荐

