0

I created a very very simple MVC view to show how many task are remaining in my todo list. Just as many tutorials out there but this time as an MVC auto hosted app.

I created the projects and played with the design a bit and everything was fun. But now I include the next code:

var context;
var web;
var user;
var list;
var listItems;
var listName = "QuickTasks";

$(document).ready(function () {
    alert("starting ready function");

    //get context and then username
    context = new SP.ClientContext.get_current();
    alert("context instantiated");
    web = context.get_web();

    alert("end of ready function");
    //get the tasks for the user
    //getTaskCountForUser();

});

The first alert is displayed, as a website triggered from SharePoint as soon as I start debugging and even when I start my developers subsite and it is shown as an app part. But the rest is not.

My main objective is to be able to get current user information from SharePoint context so I can then do stuff in my app. For example, print it on screen. I started via javascript but now I see there is a SharePointContext class in the MVC, but no idea how to do that.

Additionally I have this script added in my view, besides those bundled by layout view.

<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js" type="text/javascript"></script>
0

Is this code in the app web or in the auto-hosted MVC app? If it is in the auto-hosted part, you need to authenticate and also you will have no SharePoint context available as current context, since you technically are not in SharePoint when you calls your code (you are in your MVC app).

Take a look here for how to create a context from your MVC app. The link is for Provider-Hosted apps, which is what you should use for your MVC app now, since the annoucement of Auto-hosted being discontinued.

  • I´m trying that now since I was assigned a mission away from ofice. I checed and switched the app from auto to provder hosted. Looks like there is no great difference. However, I hardly see something useful in the link. Can you guide me a bit further? – Ricker Silva Jun 11 '14 at 16:39

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.