Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

The context: I have this web service hosted in SharePoint.

[ServiceContract]
public interface IUserService
{
  [WebInvoke(UriTemplate = "/GetCurrentUser", Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
  [OperationContract]
  string GetCurrentUser();
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserService: IUserService
{
  public string GetCurrentUser()
  {
    return SPContext.Current.Web.CurrentUser.LoginName;
  }
}

The corresponding service file UserService.svc is mapped to the ISAPI folder and looks like this:

<%@ ServiceHost Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
                Language="C#" Debug="true"
                Service="UserService, $SharePoint.Project.AssemblyFullName$" %>

I am calling the web service from a SharePoint page through JavaScript code.

function getCurrentUser() {
  $.ajax({
    type: 'GET',
    url: 'http://server/sites/rootweb/_vti_bin/UserService.svc/GetCurrentUser',
    success: function (data) {
      printToDiv(data);
    }
  });
}

The URL is hard coded for demo purposes only.

The problem: Suppose User A is the one who has first invoked the JavaScript function. Then the service returns his username. If afterwards anyone else signs in to SharePoint and calls the web service, then he will get User A's username. If IE is closed and started new and if the User C is the one who has first called the web service then his username is always returned. It doesn't matter whether User A or User B signs in to SharPoint. It is somwhow that the user who has first called the web service is cached by either the web service or the JavaScript piece of code.

What do I have to do so that the web service returns the right username?

share|improve this question
    
Have you tried setting cache:false as an argument on the ajax call? –  Robert Lindgren Sep 2 '13 at 8:27
    
Damn that was pretty easy :-) Having the right feeling with the chaching but not checking that $.ajax() supports a caching option is poor. Thank You Robert! –  Yavuz Sep 2 '13 at 8:41
    
I'll add it as an answer for you to accept :) –  Robert Lindgren Sep 2 '13 at 8:45

1 Answer 1

up vote 0 down vote accepted

It could be due to the browser caching the response of your AJAX request, IE loves this for example. Try adding: cache:false to the AJAX request:

function getCurrentUser() {
  $.ajax({
    type: 'GET',
    cache: false,
    url: 'http://server/sites/rootweb/_vti_bin/UserService.svc/GetCurrentUser',
    success: function (data) {
      printToDiv(data);
    }
  });
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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