Friday, March 29, 2013

Display User Image With User Name on ListViews webpart in CreatedBy And ModifiedBy field.

We all know SharePoint supports to store images along with user in the userprofile database. End user can user MySite to modify the content of their profile through Out Of Box screens that is very handy. but the question is How to use the image / data on the screens where SharePoint left for customization? Like we have a general requirement to show user's image along with User Name on the ListView webpart.
or on the Document Library. or on the Listing page where use form field to bind data so users can view items.


In such cases we generally use custom webparts and access user profile manager to access user profile and fetch the image url of the user in item data bound, which is not a scalable option as development effort and testing effort is very high for such simple requirement.

How ever we fulfill such requirements on client side only that is by using JavaScript Client Object Model to fetch image and use Client Scripts to Render the image in the corresponding cell. as shown below.

here is my JavaScript

you can also put this JavaScript on the page by adding it to a content editor WebPart that is a simplest option but its scope is limited to a single page. otherwise you can refer it in MasterPage that will apply this solution on all the page as required in our case.


$(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(RenderUserImage, "sp.js");
});
var userProfile;
function RenderUserImage() {

    $(".ms-vb-user").each(function () {
        userProfile = null;
        var userProfileUrl = $(this).find('a').attr('href');
        var clientContext = new SP.ClientContext.get_current();
        var web = clientContext.get_web();
        var userId = getParameterByName(userProfileUrl, "ID"); ;
        // Get the user information list for the current site
        var userInfoList = web.get_siteUserInfoList();
        // Define the query to get the user by the display name of the user.
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\'ID\'/><Value Type=\'Counter\'>" + userId + "</Value> </Eq></Where></Query><RowLimit>1</RowLimit></View>");
        // Load the items into a variable
        userProfile = userInfoList.getItems(camlQuery);
        clientContext.load(userProfile);
        // Execute the query and set up the profile information
        clientContext.executeQueryAsync(Function.createDelegate(this, function () { SetImageWithUserName(this); }), Function.createDelegate(this, this.onQueryFailed));
    });
}

function SetImageWithUserName(cell) {
    var userImgUrl = '/_layouts/images/o14_person_placeholder_32.png';
    var item = userProfile.itemAt(0);
    var imgUrl = '';
    var picture = item.get_item('Picture');
    if (picture != null && picture != '') {
        imgUrl = picture.get_url();
    }
    if (imgUrl == null || imgUrl == '') {
        imgUrl = userImgUrl;
    }
    $(cell).find('img').attr('src', imgUrl);
    $(cell).find('img').attr('height', 32);
    $(cell).find('img').attr('width', 32);
 
}
function getParameterByName(url, name) {
    if (url == '') {
        url = window.location.search;
    }
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

1 comments:

Like plenty of the best free slots websites, Ignition accepts cryptocurrencies. Here, find a way to|you probably can} fund your casino account via Bitcoin, Litecoin, and Ethereum. Unfortunately, US gamers will not be able to register on any on-line casino via Slotozilla web site. The casino have to have} bonuses that can be be} wagered rapidly and can withdraw cash to the account inside a number of} days. Slot machines 1xbet are legal in the country, which allows gamers to play any game.

Post a Comment