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.