Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SP.ClientContext().get_current throws uncaught typeError: undefined is not a function

I´m following this conference workshop http://video.ch9.ms/sessions/spc/2014/SPC261.mp4 where they teach you, how to create a sharepoint App

At some point I have this script

$(document).ready(function () {

    //get context and then username
    context = new SP.ClientContext.get_Current();
    web = context.get_web();

    //get the tasks for the user
    getTaskCountForUser();


});

It will simply count how many tasks are remaining in a Task list.

I have in the app part aspx file the next script included

<%--<script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>--%>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>


<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.runtime.debug.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.debug.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>
<script src="../Scripts/QuickTask.js" type="text/javascript"></script>

Everything seems to work just fine. App part is shown in SharePoint Online, I can include it in the home but the script just won't work and throws Uncaught TypeError: undefined is not a function right in the line

context = new SP.ClientContext.get_Current();

I tried enclosing that segment like this:

ExecuteOrDelayUntilScriptLoaded(function () {
   //my code above in here

}, "sp.js");

But in that case the same error is thrown in the first line.

What am i missing? I´m using a Sharepoint E3 online account from my MSDN subscription, Vsual Studio 2013 and in the video, the guy just go seamless through it.

Thanks

like image 691
Ricker Silva Avatar asked Dec 12 '22 05:12

Ricker Silva


1 Answers

You have some typos in your code, links to debug js files (not that this is incorrect, but why do you need them?) and error in getting ClientContext object.

To fix script include problems use following script tags instead of yours:

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

But the main problem is in this line:

context = new SP.ClientContext.get_Current();

You do not need to use new keyword in this case, you are getting context object using getter. It should be:

context = SP.ClientContext.get_current();

Also notice case of get_current() function - current word should start from lower case character.

like image 125
Yevgeniy.Chernobrivets Avatar answered Dec 25 '22 17:12

Yevgeniy.Chernobrivets