3

I am new to MVC development, however, I current have a requirement to build an application for a SharePoint app. It is currently hosted on our Azure server and is up and running fine. My problem lies in interacting with SharePoint via the Controller.

I can retrieve data when the page is initially loaded, the problem is when I interact with the page, the Controller cannot seem to validate its connection with SharePoint. I have a button on the page that when clicked, goes off to SharePoint to retrieve some data and then populates a textbox on the page. To do this, I am making an AJAX request, in which I pass the Controller and the procedure (along with the SPHostUrl and SPAppWebUrl). It executes the procedure fine, but consistently returns a null value on the line:

spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext)

(i.e. spContext is always null) I cannot seem to retrieve the clientcontext from here.

Here is my AJAX request is that is of any help, but could someone point me in the right direction with this? Is there some level of authentication I need from a HttpGet request that I also need, or is there something I am simply missing?

 $.ajax({
    url: '@Url.Action("CalculateHolidayTime", "Holidays", new { @SPHostUrl = @ViewBag.SPHostUrl, @SPAppWebUrl = @ViewBag.SPAppWebUrl }, null)',
    type: 'GET',
    dataType: 'json',
    cache: false,
    data:  { intEmployeeId: empId, strHolidayFrom: dateFrom, strHolidayTo: dateTo },          
    success: function(oTime) {
        console.log("data call succeeded...");
        $('#txtTotalTime').val(oTime.TotalTime);
        alert(oTime.Output);
        alert(oTime.Validation);
    },
    error: function(request) {
        console.log("data call failed...");
        alert(request.responseText);
    }
});

EDIT: I have narrowed it down a little more, the HttpContext.Session doesn't contain any keys on the button click, specifically the "SPContext" key, which is used to hold the SharePoint context information. Any reason as to why the session loses its keys?

2
+50

Have you added the SharePointContextFilter attribute to your CalculateHolidayTime action?

You are correctly passing the SPHostUrl as a parameter - the filter is the bit that takes that SPHostUrl and adds a sharepoint context to the session (or reads an existing one).

This is all assuming you've used the Visual Studio 'app for SharePoint' template which includes the SharePointContextFilter - if you haven't, it will help!

0

Sounds more like a pure "MVC and lost sessions" problem. You may check:

In all cases, you should first check cookies sent by the browser when the Ajax call fires (with F12 tools or Fiddler or else): are cookies present in the Ajax request?

  • Hi, thanks for the response. The cookies are present in the Ajax request (SPCacheKey etc). It is just the Session that the code loses when the button is clicked. – Tom Jul 27 '15 at 14:07
0

Not sure whether this will solve your problem , but i normally find it easy to pass the Querystring paramaters using below code:

url:'@Url.ActionWithQS("CalculateHolidayTime","Holidays")',

This will make sure all the tokens in the current page querystring will be added to the GET request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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