I've followed this guide to create and deploy a custom WCF service in Sharepoint 2010 to be consumed in a Visual Web Part using jQuery.

The WCF service's method does several things:

  • Make use of SPContext.Current
  • Calls some stored procedures using LINQ-to-SQL data context
  • Executes some code wrapped in SPSecurity.RunWithElevatedPrivileges{}
  • Returns a List<SPFile>

I've deployed the solution using Visual Studio 2010 Professional in a Sharepoint site that's valid and working.

The problem is when I make the jQuery ajax request I get the login dialog asking for credentials (doesn't like any) and also see a 401 Unauthorized response in Firebug. Also, accessing the same URL through the browser does the same, keeps asking for credentials and if you cancel you get a blank page and 401 Unauthorized in Firebug.

If I change the implementation of the web service's method to return an empty List<SPFile>, then it works.

I've debugged the whole method step-by-step and it finished without throwing any exceptions. But still getting the Unathorized response.

I'm new at Sharepoint development. I have no idea what the problem is or where to look for it.

I'm using Visual Studio 2010 Professional, Sharepoint 2010 Server, Windows Server 2008 R2 x64

1 Answer 1


You should not return a list of SPFile from your web sevice, the SPFile has a reference to SPListItem, which has a reference to SPWeb, ... All of the probably refering to a Site which you accessed (and hopefully disposed) inside RunWithElevatedPrivileges, so serializing the SPFiles are going revitalize these sites, which you aren't allowed to and certainly don't want.

Create your own serializable class with the properties you need on the client and return a list of those instead.

  • +1 Seems to make sense, because the problem is on the return, not in the middle of the code. I'll try that and get back to you later. Thanks!
    – empz
    Commented Mar 30, 2012 at 6:38
  • That was exactly the problem. I didn't even think of that. I've been struggling with this all day. Thank you again!
    – empz
    Commented Mar 30, 2012 at 6:50

Your Answer

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

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