Sharepoint-2013使用活动用户部门填写字段

时间:2017-05-04 22:24:02

标签: javascript sharepoint sharepoint-2010 peoplepicker

我有一些代码会在SP PeoplePicker中查找用户并返回用户的部门。

我想要实现的是自动填充当前用户部门的字段,因为我不想要peoplepicker字段。当前用户数据应该是可检索的,或者我应该能够在没有peoplepicker字段的情况下通过peoplepicker进行搜索。

因此,我将以下脚本添加到内容编辑器中:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>

<script> 
    $(document).ready(function()
    { 
        var PeoplePicker = $("div[title='test_column']"); 
        var PeoplePickerTopId = PeoplePicker.attr('id'); 
        var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[PeoplePickerTopId]; 

        ppobject.OnValueChangedClientScript = function(elementId, userInfo){getUserData()};       
    });

    function getUserData()
    { 
        //Set variables
        var UserDept;
        var i;
        var _PeoplePicker = $("div[title='test_column']"); 
        var _PeoplePickerTopId = _PeoplePicker.attr('id'); 
        var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId]; 

        // Call GetAllUserInfo to read PeoplePicker entries
        editorsInfo = ppobject.GetAllUserInfo(); 

        // Set Customer Info
        if(editorNames!= null)
            {UserDept = editorsInfo[0].EntityData.Department;} 

        $("input[title='Department']").val(UserDept)
    }
</script>

注意,我无法访问Sharepoint Designer。我试图仅从网络视图开始工作。

此外,如果答案需要AJAX或从httpget帖子返回值,您是否可以彻底解答答案,因为我之前从未使用过AJAX,并且从Web服务器接收响应的经验有限。

提前感谢!

1 个答案:

答案 0 :(得分:1)

您可以使用SPServices Jquery。只需下载最新的一个。 您正在使用Sharepoint 2013,因此请使用脚本编辑器而不是内容编辑器。 下面是我经常使用的脚本,工作正常。

&#13;
&#13;
<script type="text/javascript" src="/your path to JS/SiteAssets/JS/jquery-3.1.1.js"></script>  
<script type="text/jscript" src="/your path to JS/SiteAssets/JS/jquery.SPServices.min.js"></script>  

<script type="text/javascript">  

$(document).ready(function() {  	

	var UserDept = $().SPServices.SPGetCurrentUser({  
	fieldName: "Department",  	
	debug: false  
	});	  
	 

	$("input[title*='Department']").val(UserDept); // For assigning to SharePoint column named 'Department'  

	$("#txtDept").val(UserDept); // For assigning to textbox  


});  

</script>  
&#13;
&#13;
&#13;