2

I'm currently building an application page for a SharePoint 2010 site. One of the methods on this application page retrieves the User Profile information from the server and displays it to the client. This function is to return a Json object containing each of the property fields I need to display (currently testing with three).

My C# backend method:

[WebMethod]
public static string getProfileProperties()
{
    ProfileProperties profileProps = new ProfileProperties();

    using (SPSite site = SPContext.Current.Site)
    {
        SPServiceContext context = SPServiceContext.GetContext(site);
        UserProfileManager profileManager = new UserProfileManager(context);

        AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
        AudienceManager AudMgr = new AudienceManager(context);
        AudienceCollection Audiences = AudMgr.Audiences;

        using (SPWeb web = SPContext.Current.Web)
        {
            UserProfile profile = profileManager.GetUserProfile(web.CurrentUser.LoginName);
            profileProps.email = profile[PropertyConstants.WorkEmail].Value.ToString();
            profileProps.name = profile[PropertyConstants.AccountName].Value.ToString();
            profileProps.username = profile[PropertyConstants.UserName].Value.ToString();
            return JsonConvert.SerializeObject(profileProps);
        }
    }
}

My AJAX call:

(function ($) {
    $.ajax({
        type: "POST",
        url: "getProfileProperties",
        contentType: 'application/json',
        dataType: 'json',
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(XMLHttpRequest.responseJSON);
            console.log("Response Code: " + XMLHttpRequest.status + "\nStatus: " + textStatus + "\nError: " + errorThrown);
        },
        success: function (result) {
            console.log(result);
            renderProfileProperties(result.d);
        }
    });

    function renderProfileProperties(data) {
        $('.profile-name').text(data.name);
        $('.profile-email').text(data.email);
    }
})(jQuery);

The Ajax function returns a 500 Internal Server Error on execution, which I have attributed to the contentType and dataType possibly not matching to what the server is expecting and responding with.

Does anybody know, or could help me figure out, why my Ajax returns a 500 error? Thank you.

1
  • Is there anything related to the error in the log files? One thing that I see is that you are wrapping the SPContext's Site and Web objects in the using block which you should not do. You should only dispose SPWeb/SPSite objects that you create. Try removing the using blocks. Jan 30, 2017 at 23:08

1 Answer 1

0

[Updated] Benjamin, i'm wondering what made you write a WebMethod to access Profile information, you could leverage the JSOM/REST to fetch details in SharePoint 2013 https://msdn.microsoft.com/en-us/library/office/dn790354.aspx

You can just try out those basic REST call's in Postman (chrome plugin), and then use it with Script WebPart.

-- Correcting

You can use SPServices (which internally uses the WebService) to fetch profile information

http://sympmarc.com/2012/02/15/spservices-example-userprofileservice-getuserprofilebyname/

3
  • I'm using SharePoint 2010, and I can't use JSOM in that regard in 2010, as far as I am aware. I also chose to use a WebMethod as it's part of an Application Page, with custom HTML and includes. Jan 30, 2017 at 21:37
  • you mentioned 2013 in your description. Jan 30, 2017 at 21:46
  • My bad, it's for 2010, not 2013. I'll update the question. Jan 30, 2017 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.