Sunday, December 8, 2013

Get User's info using javascript client object model in SharePoint 2010

Hi everybody, here i am giving example how to retrieve user's info using java script client object model.

1- Create new Empty SharePoint Project.
2- Right click on project -> Add New Item -> click on Visual Web Part
3- Add below code in .ascx page
4- Create 

<script type="text/javascript">

var collUser;
var groupId;

    function retrieveAllUsersInGroup() {

        var clientContext = new SP.ClientContext('/');

        var collGroup = clientContext.get_web().get_siteGroups();

        var oGroup = collGroup.getById(groupId);

        this.collUser = oGroup.get_users();

        clientContext.load(collUser);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededUser), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceededUser() {

        var userInfo = ' ';

        var userEnumerator = collUser.getEnumerator();
        while (userEnumerator.moveNext()) 
        {
            var oUser = userEnumerator.get_current();
            userInfo += '\nUser: ' + oUser.get_title() +
            '\nID: ' + oUser.get_id();
        }

        alert(userInfo);
    }


    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

</script>


---
Thanks

No comments:

Post a Comment