Home > SharePoint > User properties in SharePoint 2010

User properties in SharePoint 2010

2011/03/22

In SharePoint when we add a Windows user to a site collection is possible to define properties as the email and the department:


When is needed to retrieve the properties for a user of a site collection we can user a code as this (that you can see on many blogs):

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.min.js"></script>
<script type="text/javascript">
    var objWeb17843 ;
	
    $().ready(function () {
        ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js"); 
    });
    
    function getWebUserData() {  
         var objContext = new SP.ClientContext.get_current();  
         objWeb17843 = objContext.get_web();  
         var objCurrentUser = objWeb17843.get_currentUser();  
         objCurrentUser.retrieve();  
         objContext.load(objWeb17843);  
         objContext.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));  
    }  
     
    function onSuccessMethod(sender, args) {  
         var objUser = objWeb17843.get_currentUser();  
         window.alert('User name:' + objUser.get_title() + '\n Login Name:' + objUser.get_loginName());  
    }  
     
    function onFailureMethod(sender, args) {  
         window.alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());  
    }
</script>

But the problem is that the user object has methods as get_title and get_login_name , there is not a simple method for obtain other properties as the Department.

It could be used in this case the userprofileservice.asmx , but there is a more simple approach.

I have discovered the existence of the SPServices, a jquery library for SharePoint ; it is recommended to download the latest version (at this time 0.6.0) because it works correctly with the latest jquery library (1.5).

With this library the code for read a user property is very simple:

var userDepartment = $().SPServices.SPGetCurrentUser({ fieldName: "Department" });

Advertisements
Categories: SharePoint
%d bloggers like this: