SPServices return not working

May 30, 2013 at 2:25 AM
I created a function using spservices to get the profile picture but when I alert the function it returned undefined. Please help.

$(document).ready(function(){
var getUsername = $().SPServices.SPGetCurrentUser();
alert(getProfPic(getUsername));
});

function getProfPic(username) {
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "UserInfo",
        CAMLViewFields: "<ViewFields><FieldRef Name='ID'/><FieldRef Name='Account'/><FieldRef Name='Picture'/><FieldRef Name='Name'/></ViewFields>",
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='Name'/><Value Type='Text'>" + username + "</Value></Eq></Where></Query>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                var p = ($(this).attr("ows_Picture")).split(",")[1];
                //var a = ($(this).attr("ows_Account"));
                return p;
            });
        }
    });
}
Coordinator
Jun 4, 2013 at 3:00 PM
Picture isn't one of the columns in the UserInfo list in my environment. I think you need to call the User Profile Service for this.

M.
Jun 5, 2013 at 6:58 AM
Edited Jun 5, 2013 at 7:04 AM
I already solved the Picture worked it turned out that I misplaced the return p.

This is the complete code.

function getProfPic(username) {
    var p1 = "";
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "UserInfo",
        CAMLViewFields: "<ViewFields><FieldRef Name='ID'/><FieldRef Name='Account'/><FieldRef Name='Picture'/><FieldRef Name='Name'/></ViewFields>",
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='Name'/><Value Type='Text'>" + username + "</Value></Eq></Where></Query>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                p1 = ($(this).attr("ows_Picture"));
                p = "";
                if (p1 == undefined) {
                    var webprotocol = window.location.protocol;
                    var webhost = window.location.host;
                    p = webprotocol +"//" + webhost + "/_layouts/images/person.gif";
                } else {
                    p = p1.split(",")[1];
                }
            });
        }
    });
    return p;
}
then I just add this to <img src='" + getProfPic(user) + "' /> and this is the result

Image

Sir btw I saw your showing spinning icon example I tested it and it worked in Firefox but it doesn't work in IE and chrome. I don't know what's causing this problem.
Coordinator
Jun 5, 2013 at 12:47 PM
IE totally freezes up when a synchronous call is happening. If you move to using jQuery deferred objects (promises), it will work.

M.