I try to get SPContext in javascript but I have an error. I added links for javascript libraries to my Client Web part page:

<script type="text/javascript" src="../Scripts/jquery-1.8.2.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="../Scripts/testing.js"></script>

In testing.js I wrote that code:

// The allAnnouncements variable is used by more than one 
//  function to retrieve and process the results.
var allAnnouncements;
var hostweburl;
var appweburl;

// Load the required SharePoint libraries
$(document).ready(function () {
    //Get the URI decoded URLs.
    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl =  decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    // resources are in URLs in the form:
    // web_url/_layouts/15/resource
    var scriptbase = hostweburl + "/_layouts/15/";
   //   Load the js files and continue to the successHandler
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js",            
                function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); }
// Function to prepare and issue the request to get
//  SharePoint data
function execCrossDomainRequest() {
    // context: The ClientContext object provides access to
    //      the web and lists objects.
    // factory: Initialize the factory object with the
    //      app web URL.
    try {

        var context = new SP.ClientContext(hostweburl);

        //var context = new SP.ClientContext(appweburl);
    var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
    var app = new SP.AppContextSite(context, hostweburl);
    var web = app.get_web();

    //var context = new SP.ClientContext(appweburl);

    //var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);

    //Get the web and list objects
    //  and prepare the query
    var web = context.get_web();
    var list = web.get_lists().getByTitle("List1");
    var camlString =
        "<View><ViewFields>" +
            "<FieldRef Name='Title' />" +
            "<FieldRef Name='Body1' />" +

    var camlQuery = new SP.CamlQuery();
    allAnnouncements = list.getItems(camlQuery);

    context.load(allAnnouncements, "Include(Title, Body1)");

    //Execute the query with all the previous 
    //  options and parameters
        successHandler, errorHandler

// Function to handle the success event.
// Prints the data to the page.
function successHandler(data, req) {
    var announcementsHTML = "";
    var enumerator = allAnnouncements.getEnumerator();

    while (enumerator.moveNext()) {
        var announcement = enumerator.get_current();
        announcementsHTML = announcementsHTML +
            "<p><h1>" + announcement.get_item("Title") +
            "</h1>" + announcement.get_item("Body1") +

    document.getElementById("renderAnnouncements").innerHTML =

// Function to handle the error event.
// Prints the error message to the page.
function errorHandler(data, error, errorMessage) {
    document.getElementById("renderAnnouncements").innerText =
        "Could not complete cross-domain call: " +

// Function to retrieve a query string value.
// For production purposes you may want to use
//  a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
    var params =
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];

but in debugger I see Object doesn't support this action. What is wrong? and it working on O365.


  • On what row are you getting the error? Commented Feb 17, 2014 at 10:12
  • Yeah can we see the error?
    – Stephen
    Commented Feb 17, 2014 at 11:30
  • 1
    on this line try { var context = new SP.ClientContext(hostweburl); }catch(eee) { alert(eee); } Error message(Object doesn't support this action)
    – tola
    Commented Feb 18, 2014 at 1:29

3 Answers 3


The error is because you are trying the create an App Context from the Host webs URL.

To get the correct contexts for app web and host web use this code:

var context = new SP.ClientContext(appweburl);
var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
var context.set_webRequestExecutorFactory(factory);
var appContextSite = new SP.AppContextSite(context, hostweburl);

You will have the Appweb Context in context and the host web context in appContextSite and hence can get the host web by doing

this.web = appContextSite.get_web();

(modified code from http://www.mavention.com/blog/sharePoint-app-reading-data-from-host-web)

  • I try with script but I still same. noted that I am using sharepoint hosted. and this script put in on Client Web Part. any more idea for me to achieve this..kindly please...
    – tola
    Commented Feb 18, 2014 at 8:06
  • Have you given your app read permissions on the host web, from the manifest? The code is correct and working in my environments Commented Feb 18, 2014 at 8:18
  • I shared my project here drive.google.com/… kindly please help me
    – tola
    Commented Feb 18, 2014 at 8:57
  • Sorry but I do not have the time to hlep you out on that level. Commented Feb 18, 2014 at 8:58

I got the issue why.. it because of this line of script is not load correctly..

<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>

and I replace with this line.

<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>

remember that.. and now every things is working fine..


I had the same issue when trying to set up the SP.ClientContext.

For me I was trying to change the items in the QuickLaunch using JS and DOM within a SharePoint site.

I was able to set up a SP.ClientContext object using a content editor webpart and embedded the following code into it.


<script src="/sites/yourSite/Style Library/scripts/yourscript.js"></script>
<a id="contextBtn" href="javascript:btn1Click()">+ ​Add a New Topic to this Subject     </a>​​​​​​​​​​​​​​​​

The above src attribute is a little confusing so I'll explain.

/sites - SP sites /yourSite - would be your site name i.e. /style library/scripts - where I stored it in SP, can be anywhere but good practice to store in style library, it works there /yourscipt.js - your file name

NOTE: if your using SP designer can navigate to the JS file you want and view the properties and gran the location ( has path with http://) and content editor automatically makes it relative)

Here is the JS waiting in yourfile.js

function btn1Click() {
         var clientContext = new SP.ClientContext.get_current();


NOTE: what made this work was attaching the script to the click event of the button. when trying to call directly from yourfile.js give the error you were seeing.

Object doesn't support this action

Not sure why it works this way, could be that client context needs to be called be an DOM element? it could also have to do with waiting for SP.js to load, I just couldn't get it to work using ExecuteOrDelayUntilScriptLoaded() function, which how most people recommend doing it.

Anyway, not sure if this is what your looking for, but that's how I was able to set up the clientContext in my situation.

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.