locked
Web API Issue RRS feed

  • Question

  • Hello All,

    I am working on Web API functionality. I have included "jquery_1.9.1.min", "SDK.REST" files in entity Web Resources section and executed the below code. While executing the script, getting the below error message.

    Code:

    function WebAPIExample()
    {
    	debugger;
    	var getAccounts = getAPIData();
    	debugger;
    }
    
    function getAPIData()
    {
    	GET https://mylearning123.crm5.dynamics.com/api/data/v8.0/accounts?$select=name,revenue&$top=3&$filter=revenue gt 100000 HTTP/1.1
    	Accept: application/json
    	OData-MaxVersion: 4.0
    	OData-Version: 4.0
    	Prefer: odata.maxpagesize=3
    }
    Error Message
    Microsoft Dynamics CRM Error Report Contents
    
    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>Expected ';'</Message>
       <Line>11</Line>
       <URL>/%7B635913124710002091%7D/WebResources/crm_WebApi?ver=718141953</URL>
       <PageURL>/form/page.aspx?lcid=1033&themeId=f499443d-2082-4938-8842-e7ee62de9a23&tstamp=700056&updateTimeStamp=635883907106564876&userts=131001900299507616&ver=718141953#_gridType=10007&etc=10007&id=%7b854E5822-9FD1-E511-80E1-3863BB34FDE8%7d&pagemode=iframe&rskey=%7bAEA491F3-553D-48F3-94AF-46747F3F915C%7d&theme=Outlook15White&counter=1455716436735</PageURL>
       <Function></Function>
       <FunctionRaw></FunctionRaw>
       <CallStack>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; AskTbORJ/5.15.15.36191; MS-RTC LM 8; BRI/2; .NET4.0C; .NET4.0E; InfoPath.3; GWX:DOWNLOADED; rv:11.0) like Gecko</BrowserUserAgent>
       <BrowserLanguage>en-US</BrowserLanguage>
       <SystemLanguage>en-US</SystemLanguage>
       <UserLanguage>en-US</UserLanguage>
       <ScreenResolution>1360x768</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClienState>Online</ClienState>
       <ClientTime>2016-02-17T19:12:49</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>1033</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>1033</UserCulture>
        <OrgID>{F2E3612E-BD82-45C2-8122-C03AB37FD6F5}</OrgID>
        <UserID>{4D4AE231-50F0-42E4-95EC-9684C95D5E2E}</UserID>
        <CRMVersion>8.0.0.2091</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>

    If i run the Web API directly, i am able to capture the Result Set and getting error while calling from script.

    Help me out to fix the issue.

    Regards,

    Kumar R

    Wednesday, February 17, 2016 2:27 PM

Answers

  • Yes. I haven't included that methods in the above code. 
    I found the issue which is related to one extra space in the URI in code level. Because of that its giving error. Corrected it and now its working fine.

    Thank you dear.

    • Marked as answer by Kumar_R Wednesday, February 17, 2016 5:05 PM
    Wednesday, February 17, 2016 5:05 PM

All replies

  • function getAPIData()
    {
    	GET https://mylearning123.crm5.dynamics.com/api/data/v8.0/accounts?$select=name,revenue&$top=3&$filter=revenue gt 100000 HTTP/1.1
    	Accept: application/json
    	OData-MaxVersion: 4.0
    	OData-Version: 4.0
    	Prefer: odata.maxpagesize=3
    }
    what you wrote inside getAPIData is not JavaScript code, that's why you get the error

    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, February 17, 2016 2:43 PM
  • Hello,

    You have this error because you don't build code in the right way. Check this sample that shows how to build webapi requests in right way - https://code.msdn.microsoft.com/SdkWebAPIPreviewjs-Sample-2ed65c1b/sourcecode?fileId=138606&pathId=1811022220


    Dynamics CRM MVP
    My blog

    Wednesday, February 17, 2016 2:46 PM
    Moderator
  • Thanks for your reply. Can you help me to get the set of records from one entity based on query using javascript in MS CRM online?

    As i come to know OData is no more in MS CRM 2016 and its replaced with Web API. As i googled, i found this aproach. 

    Reference Link: https://msdn.microsoft.com/en-us/library/gg334767.aspx

    Regards,

    Kumar R

    Wednesday, February 17, 2016 2:48 PM
  • Thanks for your reply. Can you help me to get the set of records from one entity based on query using javascript in MS CRM online?

    As i come to know OData is no more in MS CRM 2016 and its replaced with Web API. As i googled, i found this aproach. 

    Reference Link: https://msdn.microsoft.com/en-us/library/gg334767.aspx

    Regards,

    Kumar R

    This is not correct - support of 2011 Odata endpoint is still supported so you can use it without any issue.

    Dynamics CRM MVP
    My blog


    Wednesday, February 17, 2016 2:52 PM
    Moderator
  • Hi,

    As suggested tried with XMLHttpRequest method. Its giving undefined message after execution.

    function WebAPICall()
    {
    	debugger;
    	var getAccounts = getAPIData();
    	debugger;
    }
    
    function getAPIData()
    {
    	var apiURI = "https://mylearning123.crm5.dynamics.com/api/data/v8.0/accounts?$select=name,revenue&$top=3&$filter=revenue gt 100000";
    	var req = new XMLHttpRequest(); 
    	req.open("GET", encodeURI(apiURI), true); 
    	req.setRequestHeader("Accept", "application/json"); 
    	req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    	req.setRequestHeader("OData-MaxVersion", "4.0"); 
    	req.setRequestHeader("OData-Version", "4.0"); 
    	req.onreadystatechange = function () 
    	{ 
    		if (this.readyState == 4 /* complete */) 
    		{ 
    			req.onreadystatechange = null; 
    			if (this.status == 200) 
    			{ 
    				if (successCallback) 
    					successCallback(JSON.parse(this.response).value); 
    			} 
    			else 
    			{ 
    				if (errorCallback) 
    					errorCallback(Sdk.WebAPIPreview.errorHandler(this)); 
    			} 
    		} 
    	}; 
    	req.send(); 
    }

    Correct me please where i am wrong?


    • Edited by Kumar_R Wednesday, February 17, 2016 4:12 PM
    Wednesday, February 17, 2016 4:11 PM
  • What is successCallback, errorCallback in your case because I don't see it in your code?

    Have you included JSON2 and Sdk.WebAPIPreview JS files to operate with that namespaces?


    Dynamics CRM MVP
    My blog

    Wednesday, February 17, 2016 4:29 PM
    Moderator
  • Hi

    These are system defined functions right.

    successCallback - The function to call when the actions are completed. The results of the operation will be passed to this function.

    errorCallback  - The function to call when there is an error. The error will be passed to this function.

    Wednesday, February 17, 2016 4:39 PM
  • No, it is not. This is functions that you have to define.

    Dynamics CRM MVP
    My blog

    Wednesday, February 17, 2016 4:47 PM
    Moderator
  • Yes. I haven't included that methods in the above code. 
    I found the issue which is related to one extra space in the URI in code level. Because of that its giving error. Corrected it and now its working fine.

    Thank you dear.

    • Marked as answer by Kumar_R Wednesday, February 17, 2016 5:05 PM
    Wednesday, February 17, 2016 5:05 PM
  • So may be it is correct to mark my replies as answer instead of yours? :)

    Dynamics CRM MVP
    My blog

    Wednesday, February 17, 2016 7:08 PM
    Moderator