none
Web method throwing an 'Internal Server Error' when instantiating a custom class

    질문

  • Hi,

    We have a web method implemented in the code-behind of an application page that works fine when invoked from jQuery, and native SharePoint and system objects are being used.

    We added a reference to a DLL of our own authoring and when instantiating an object of one of its classes an 'Internal Server Error' is thrown. Is obvious that the problem is the object instantiation, but don't understand why and didn't find any satisfactory answer on the Web.

    Two examples below.

    1. Without any reference to our custom DLL

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    public static string GetUserLocation() { var username = SPContext.Current.Web.CurrentUser.LoginName.Replace(@"i:0#.w|net1\", ""); return $"The user {username} is located at Brussels"; }

    It returns the message as expected.

    2. With the reference

    using UserInfo;

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static string GetUserLocation() { var username = SPContext.Current.Web.CurrentUser.LoginName.Replace(@"i:0#.w|net1\", ""); var user = new UserLocation() { Username = username; } return $"The user {username} is located at {user.location}"; }


    Does anyone have any idea why it doesn't work? As the method GetUserLocation is a web method should I do something specific on the classes used in the UserInformation project?

    Thanks in advance.

    Vitor

    2018년 6월 12일 화요일 오후 4:21

모든 응답

  • Hi,

    Please provide more information about the UserLocation class for further research.

    Or you can create a console application to test this class and make it works.

    If you want to get user profile information, we can use the code below to achieve it.

    UserProfileManager upm = new UserProfileManager(SPServiceContext.Current);
    UserProfile up = upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);

    More information: https://sharepoint.stackexchange.com/questions/151135/how-to-get-users-office-location-in-sharepoint-2010

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    2018년 6월 13일 수요일 오전 8:39
    중재자
  • Hi Dennis,

    Thanks a lot for your reply.

    The information you've passed is without question relevant for me.

    The class mentioned above is included in a class library that is added to projects where needed, and is working fine with console and ASP.NET projects. Its goal is to pass information from one system to another.

    I needed to include it in the SP project I'm currently working on. As most of the development in the project needs to be client-side I created an application page to make the methods from my class accessible by JS. As it seems everything is working fine with the web method if I don't use our own classes.

    An example is the class UIPreference.

        public class UIPreference
        {
            [JsonProperty(PropertyName = "titleEditable", NullValueHandling = NullValueHandling.Ignore)]
            public bool TitleEditable { get; set; }
            [JsonProperty(PropertyName = "commentsEditable", NullValueHandling = NullValueHandling.Ignore)]
            public bool CommentsEditable { get; set; }
            [JsonProperty(PropertyName = "attachButtonVisible", NullValueHandling = NullValueHandling.Ignore)]
            public bool AttachButtonVisible { get; set; }
            [JsonProperty(PropertyName = "searchButtonsEnabled", NullValueHandling = NullValueHandling.Ignore)]
            public bool SearchButtonsEnabled { get; set; }
        }

    This class is used only to pass the properties above in JSON format to the other system. The Newtonsoft.Json assembly is being used. The exception is thrown with the line var preferences = new UIPreference() in the code-behind of the application page.

    Do you need some more info?

    Thanks,

    Vitor

    2018년 6월 13일 수요일 오후 3:12
  • Hi Vitor,

    In your first post, I see you use UserLocation class.

    And in the second reply, you say the exception is thrown with the line var preferences = new UIPreference() , this is a new issue? I suggest you provide the detailed exception error message for further research.

    And try to create a custom class without using the JsonProperty and check if it works.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.


    2018년 6월 14일 목요일 오전 9:12
    중재자
  • Hi Dennis,

    sorry for the confusion. They are both in the class library we are using. The line I provided was just another example.

    The exception message is 'There was an error processing the request'. The result of the POST is HTTP 500 Internal Server Error. No StackTrace is being returned...

    The request and response headers are as follow:

    Request

    POST /Reports/_layouts/15/connector_sp/connectorpage.aspx/GetUserPermissions HTTP/1.1
    Accept: application/json, text/javascript, */*; q=0.01
    Content-Type: application/json; charset=utf-8
    X-Requested-With: XMLHttpRequest
    Referer: http://dev-sp-sites/Reports/Reports/Forms/My_Reports.aspx
    Accept-Language: en-GB,en;q=0.8,fr-BE;q=0.5,fr;q=0.3
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)
    Host: dev-sp-sites
    Content-Length: 0
    DNT: 1
    Connection: Keep-Alive
    Cache-Control: no-cache

    Response:

    Response: HTTP/1.1 500 Internal Server Error
    Content-Type: application/json; charset=utf-8
    Server: Microsoft-IIS/8.5
    jsonerror: true
    SPRequestGuid: dbff709e-8026-408b-52ba-eac1309d3786
    request-id: dbff709e-8026-408b-52ba-eac1309d3786
    X-FRAME-OPTIONS: SAMEORIGIN
    SPRequestDuration: 11
    SPIisLatency: 5
    X-Powered-By: ASP.NET
    MicrosoftSharePointTeamServices: 15.0.0.4893
    Access-Control-Allow-Origin: https://ws.isiknowledge.com/cps/xrpc
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Methods: GET, POST
    Access-Control-Allow-Credentials: true
    X-Content-Type-Options: nosniff
    X-MS-InvokeApp: 1; RequireReadOnly
    Date: Thu, 14 Jun 2018 10:21:53 GMT
    Content-Length: 91

    Response Body: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}

    I get no useful information from the logs also.

    I'm going to follow your suggestion, create a simple class without the Json attributes and see if it works. I'll come back after testing it.

    Thanks for your help.

    2018년 6월 14일 목요일 오전 10:53
  • Hi again,

    I created a new class library, added it to the project and received the same exception once I've tried to create the object.

    The class created goes like:

    namespace DummyCL
    {
        public class Dummy
        {
        }
    }

    Built it and added the DLL to the SP project (the previous DLL was removed).

    Created a new web method:

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public static string CallDummy()
            {
                // var dummyClass = new Dummy();
                return "Dummy is being called.";
            }

    The dummyClass line is commented to check if it works without creating the object. And it did. Taking the comment away will throw the same exception as before...

    2018년 6월 14일 목요일 오후 2:40
  • Hi,

    Please try to create the Dummy class in the SP Project directly, and check if it works.

    In SharePoint 2013, I suggest you create a custom REST service and consume the REST service from client-side using jQuery Ajax. The following article for your reference:

    https://social.technet.microsoft.com/wiki/contents/articles/24194.sharepoint-2013-create-a-custom-wcf-rest-service-hosted-in-sharepoint-and-deployed-in-a-wsp.aspx 

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    2018년 6월 15일 금요일 오전 2:33
    중재자
  • Hi,

    Please use the try catch block to track the exceptions being thrown at line of the code.

    I suspect your WSP solution is not correctly referenced the third party or external assembly. May be your external assembly might have using the unmanaged code block that could causes the error.

    Its better to use the SharePoint APIs instead of using the external assemblies in SharePoint.


    Murugesa Pandian MCSA,MCSE,MCPD

    Gear up for some solid action by doing. Slide,theory and blog won't useful much. 
    After all world likes only doers not sayers/speakers .

    2018년 6월 15일 금요일 오전 3:35