none
Problem in accessing REST API through javascript in sp2013

    Question

  • I am trying to access list items the list items through REST in javascript. I use following code:

     

    <input onclick="Call()" name="Button1" type="button" value="button" />
    <script type="text/javascript" src="../SiteAssets/Scripts/jquery.js"></script><script type="text/javascript" src="../SiteAssets/Scripts/jquery-1.7.2.js"></script><script type="text/javascript" src="../SiteAssets/Scripts/jquery-1.7.2.min.js"></script><script  type="text/javascript"> 
                    
    function test()
    {
    
    }                
    ExecuteOrDelayUntilScriptLoaded(test,'sp.js');
    </script><script >
    function Call()
    {
    var appweburl = 'https://microsoft.sharepoint.com/teams/CCG_Tier2_Support';
    
    var executor = new SP.RequestExecutor(appweburl);
                    executor.executeAsync(
                        {
                            url:
                                appweburl +
                                "/_api/SP.AppContextSite(@target)/web/lists?@target='" +
                                'RESTDemo' + "'",
                            method: "GET"}
                    );
    
    
    
    }
    jQuery.ajax({
    url: 'https://microsoft.sharepoint.com/teams/CCG_Tier2_Support',
    type: 'GET'
    })

    I have inherited the page from default master page.

    Here I am getting an error that object does not support thi property when try to access SP.RequestExecutor().

    Can someone tell me the problem? 

    

     
    Wednesday, April 17, 2013 6:10 AM

Answers

  • Hi All

    Following code solves the problem:

    $(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 file and continue to the  
        //   success event handler 
        $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); 
    }); 
     
    // Function to prepare and issue the request to get 
    //  SharePoint data 
    function execCrossDomainRequest() { 
        var executor; 
     
        // Initialize the RequestExecutor with the app web URL. 
        executor = new SP.RequestExecutor(appweburl); 
     
        // Issue the call against the host web. 
        // To get the title using REST we can hit the endpoint: 
        //      app_web_url/_api/SP.AppContextSite(@target)/web/title?@target='siteUrl' 
        // The response formats the data in the JSON format. 
        // The functions successHandler and errorHandler attend the 
        //      success and error events respectively. 
        executor.executeAsync( 
            { 
    //write your code
    });
    }

    Thursday, April 18, 2013 12:26 PM

All replies

  • Hi

    I think in your page  sp.RequestExecutor.js is not getting loaded. If not add a scipt tag entry to load this js in your page and it will work fine

    Wednesday, April 17, 2013 8:50 AM
  • Thanks agarwal for your reply.

    What you are saying is true. My page don't render  SP.RequestExecutor.js file. but for that i made following changes in the code and then tried though its behaving the same way as before. Is there any other way i can try to make SP.RequestExecutor.js to render before SP.RequestExecutor() get called. 

    Following are the code changes:

    <script type="text/javascript" src="../SiteAssets/Scripts/jquery.js"></script><script type="text/javascript" src="../SiteAssets/Scripts/jquery-1.2.1.js"></script><script type="text/javascript" src="../SiteAssets/Scripts/jquery.min.js"></script><script  type="text/javascript"> 
                    
                   
    ExecuteOrDelayUntilScriptLoaded('Call','SP.RequestExecutor.js');
    </script><script >
    function Call()
    {
    
    var appweburl = 'https://microsoft.sharepoint.com/teams/CCG_Tier2_Support';
    
    var executor = new SP.RequestExecutor(appweburl);
                    executor.executeAsync(
                        {
                            url:
                                appweburl +
                                "/_api/SP.AppContextSite(@target)/web/lists?@target='" +
                                'RESTDemo' + "'",
                            method: "GET"}
                    );
    
    
    
    }
    jQuery.ajax({
    url: 'https://microsoft.sharepoint.com/teams/CCG_Tier2_Support',
    type: 'GET'
    })
    </script>

    Wednesday, April 17, 2013 9:20 AM
  • The file is present in layouts so load the file explicitly

    <SharePoint:ScriptLink ID="ScriptLink5" Language="javascript" Name="SP.RequestExecutor.js" OnDemand="true" runat="server" Localizable="false"></SharePoint:ScriptLink>

    Wednesday, April 17, 2013 10:05 AM
  • call  ExecuteOrDelayUntilScriptLoaded(test,'sp.js') method inside the document.ready function.

    $(document).ready(function() 
    {
        ExecuteOrDelayUntilScriptLoaded(test,'sp.js');
    });


    MCTS,MCPD Sharepoint 2010. My Blog- http://www.sharepoint-journey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful

    Wednesday, April 17, 2013 10:47 AM
    Moderator
  • I have tried this Devendra. But Unfortunately not working. when i tried with: ExecuteOrDelayUntilScriptLoaded(Call,'SP.RequestExecutor.js');

    The function won't get executed.

    Wednesday, April 17, 2013 11:58 AM
  • SP.RequestExecutor.js file do not get loaded even after adding this line of code.
    Wednesday, April 17, 2013 11:59 AM
  • hi

    Which line of code did you add and you didn't get the file

    Wednesday, April 17, 2013 12:31 PM
  • hi 

    I tried the following line of code which u suggested and did not succeed:

    <SharePoint:ScriptLink ID="ScriptLink5" Language="javascript" Name="SP.RequestExecutor.js" OnDemand="true" runat="server" Localizable="false"></SharePoint:ScriptLink>

    Thursday, April 18, 2013 5:28 AM
  • did you try to debug the code ?

    document.ready function is not firing?


    MCTS,MCPD Sharepoint 2010. My Blog- http://www.sharepoint-journey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful

    Thursday, April 18, 2013 6:04 AM
    Moderator
  • yes i did debug the code. Document.ready() fires.

    As explained in the question itself the problem is when it reaches the line-  SP.RequestExecutor();  throws an error that object does not support this property .

    Thursday, April 18, 2013 6:10 AM
  • Hi All

    Following code solves the problem:

    $(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 file and continue to the  
        //   success event handler 
        $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); 
    }); 
     
    // Function to prepare and issue the request to get 
    //  SharePoint data 
    function execCrossDomainRequest() { 
        var executor; 
     
        // Initialize the RequestExecutor with the app web URL. 
        executor = new SP.RequestExecutor(appweburl); 
     
        // Issue the call against the host web. 
        // To get the title using REST we can hit the endpoint: 
        //      app_web_url/_api/SP.AppContextSite(@target)/web/title?@target='siteUrl' 
        // The response formats the data in the JSON format. 
        // The functions successHandler and errorHandler attend the 
        //      success and error events respectively. 
        executor.executeAsync( 
            { 
    //write your code
    });
    }

    Thursday, April 18, 2013 12:26 PM