none
set the column value to current UserID if check box is checked RRS feed

  • Question

  • In editform.aspx page, there are several columns (type:user or group). I'd like to set the columns' value to current userid if corresponding checkbox is checked. 

    I'd appreciate if someone can help me. 

    Wednesday, August 27, 2014 6:01 AM

Answers

  • Use the following sample code.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    
    //Bind the Checkbox click event
    $('#checkboxid').click(function() 
    {
    	if (!$(this).is(':checked')) 
    	{
    		GetCurrentUser();        
            }
    });
    
    
    
    
    function GetCurrentUser() {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });
    }
    function onSuccess(data, request){
     var loginName = data.d.Title;
     SetUserFieldValue("people",loginName);
    }
    function onError(error) {
      //alert(error);
    }
    function SetUserFieldValue(fieldName, userName) {
     var _PeoplePicker = $("div[title='" + fieldName + "']");
     var _PeoplePickerTopId = _PeoplePicker.attr('id');
     var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
     _PeoplePickerEditer.val(userName);
     var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
     _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
    }
    
    });
    </script>
    Please mark it answered, if your problem resolved.

    • Marked as answer by Denis xu Thursday, August 28, 2014 10:52 PM
    Thursday, August 28, 2014 10:18 AM

All replies

  • Hi,

    Add the Script editor web part to the edit form and use the following sample code to get and set the current user.

    You need to modify the code meet your requirement.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    function GetCurrentUser() {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });
    }
    function onSuccess(data, request){
     var loginName = data.d.Title;
     SetUserFieldValue("people",loginName);
    }
    function onError(error) {
      //alert(error);
    }
    function SetUserFieldValue(fieldName, userName) {
     var _PeoplePicker = $("div[title='" + fieldName + "']");
     var _PeoplePickerTopId = _PeoplePicker.attr('id');
     var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
     _PeoplePickerEditer.val(userName);
     var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
     _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
    }
    GetCurrentUser();
    });
    </script>
    Please mark it answered, if your problem resolved.


    Wednesday, August 27, 2014 6:29 AM
  • Hi,

    Add the Script editor web part to the edit form and use the following sample code to get and set the current user.

    You need to modify the code meet your requirement.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    function GetCurrentUser() {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });
    }
    function onSuccess(data, request){
     var loginName = data.d.Title;
     SetUserFieldValue("people",loginName);
    }
    function onError(error) {
      //alert(error);
    }
    function SetUserFieldValue(fieldName, userName) {
     var _PeoplePicker = $("div[title='" + fieldName + "']");
     var _PeoplePickerTopId = _PeoplePicker.attr('id');
     var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
     _PeoplePickerEditer.val(userName);
     var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
     _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
    }
    GetCurrentUser();
    });
    </script>
    Please mark it answered, if your problem resolved.


    Thank you for the reply. I am new to coding. Could you please let me know how to bind the script with the checkbox?

    below is the concering column and checkbox:

    <td width="400px" valign="top" class="ms-formbody">
    
           <SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="Display" FieldName="_x786e__x8ba4__x002d__x65b9__x68" __designer:bind="{ddwrt:DataBind('u',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@_x786e__x8ba4__x002d__x65b9__x68')}"/>
    
           <SharePoint:FieldDescription runat="server" id="ff3description{$Pos}" FieldName="_x786e__x8ba4__x002d__x65b9__x68" ControlMode="Display"/>
    
           <asp:CheckBox runat="server" id="chkName{generate-id()}" ></asp:CheckBox>
    
    </td>
    

    Thursday, August 28, 2014 9:38 AM
  • Use the following sample code.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var userid = _spPageContextInfo.userId;
    
    //Bind the Checkbox click event
    $('#checkboxid').click(function() 
    {
    	if (!$(this).is(':checked')) 
    	{
    		GetCurrentUser();        
            }
    });
    
    
    
    
    function GetCurrentUser() {
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });
    }
    function onSuccess(data, request){
     var loginName = data.d.Title;
     SetUserFieldValue("people",loginName);
    }
    function onError(error) {
      //alert(error);
    }
    function SetUserFieldValue(fieldName, userName) {
     var _PeoplePicker = $("div[title='" + fieldName + "']");
     var _PeoplePickerTopId = _PeoplePicker.attr('id');
     var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
     _PeoplePickerEditer.val(userName);
     var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
     _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
    }
    
    });
    </script>
    Please mark it answered, if your problem resolved.

    • Marked as answer by Denis xu Thursday, August 28, 2014 10:52 PM
    Thursday, August 28, 2014 10:18 AM
  • Finally, It works. Thank you.
    Thursday, August 28, 2014 10:53 PM