Wednesday, September 9, 2015

Read user profile Information in sharepoint 2013 and JSOM

Requirement : Read user profile Information in  sharepoint 2013 and JSOM?


Steps to do it :
1.  Create a new page in sharepoint 2013 "UserProfiletest.aspx"
2. Add a script editor webpart to it.
3. Add references to jquery library and sp.js  in the script tag
4. Paste the following code in the script editor webpart .


<script>
 $(document).ready(function () {
            siteUrl = _spPageContextInfo.webServerRelativeUrl;
            GetLoadVision();
        });




        function GetLoadVision() {
           //load sp.js
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
        }




function retrieveListItems(){
var userID="domain/username";
getUserProfile(userID);
}

function getUserProfile(userID)
{
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var userInfoList = web.get_siteUserInfoList();
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/>' +'<Value Type=\'Number\'>' + userID + '</Value></Eq' +'</Where></Query><RowLimit>1</RowLimit></View');
this.collListItem = userInfoList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args)
{
var item = collListItem.itemAt(0);
var profile = item.get_item(‘Notes’);
var pictureUrl = item.get_item(‘Picture’).get_url();
var userImage = document.getElementById(‘myImageContainer’); -> Image object
userImage.src = pictureUrl;
var profileDiv = document.getElementById(‘userProfileContainer’);
profileDiv.innerHTML = profile;
}


</script>


<div id="userProfileContainer’"></div>


That's it... :)

No comments:

Post a Comment

Rename a Web Application in Sharepoint : A solution using powershell

Here is a small script that will enable you all to change a SharePoint Web Application name . We can use the following SharePoint Pow...