2

How to get current login user department using java script or spservices? Any idea?

1

Please try below mentioned code. You will get the current logged in user's username & department using SPServices

<script src="/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>  
<script src="/Scripts/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>  
<script language="javascript" type="text/javascript" src="/Scripts/jquery.SPServices-0.7.2.min.js"></script>

<script language="javascript" type="text/javascript" src="/Scripts/sputility.min.js"></script>


<script type="text/javascript">  

$(document).ready(function() {  
    // get the title of current user  
    var UserName= $().SPServices.SPGetCurrentUser({  
    fieldName: "Title",  
   debug: false  
});  
// get department of current user
var userDepartment = $().SPServices.SPGetCurrentUser({
   fieldName: "Department"
   });
     // alert(userDepartment);


//alert("Hi");
//populate the field ‘Requestor’ with the current account  
$('textarea[title="Requestor"]').val(UserName);  
$('input[title="Requestor"]').val(UserName);  

$('textarea[title="Department"]').val(userDepartment); 
$('input[title="Department"]').val(userDepartment); 

});  

</script>
  • Thank U hardik it's working fine. – chakri May 20 '16 at 9:57
  • Mark it so others can get benefit of the answer. – Hardik May 20 '16 at 10:01
  • Using javascript any idea? – chakri May 20 '16 at 10:10
  • using javascript for email am using oUser.get_email() for department any option is there – chakri May 20 '16 at 10:13
  • Please refer this ....msdn.microsoft.com/en-us/library/office/jj920104.aspx – Hardik May 20 '16 at 10:21
0

Just copy - paste and run . You dont need import everything library :D Best performance solution :)

function populateDataFields() {
    var data = {};
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
        headers: {
            Accept: "application/json;odata=verbose"
        },
        success: function(data) {
            try {
                //Get properties from user profile Json data  
                data.userDisplayName = data.d.DisplayName;
                data.AccountName = data.d.AccountName;
                data.Title = data.d.Title;
                var properties = data.d.UserProfileProperties.results;
                for (var i = 0; i < properties.length; i++) {
                    var property = properties[i];
                    if (property.Key == "SPS-Department") {
                        data.Department = property.Value;
                    }

                }

                var newLine = "\r\n"
                var details = "Name:" + data.d.DisplayName;
                details += newLine;
                details += "Department:" + data.Department;
                console.log(details);
            } catch (err2) {
                console.error(JSON.stringify(err2));
            }
        },
        error: function(jQxhr, errorCode, errorThrown) {
            console.error(errorThrown);
        }
    });
}
SP.SOD.executeOrDelayUntilScriptLoaded(populateDataFields, 'SP.js');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.