Call stored procedure with parameters using Web Api Odata - stored-procedures

Is it possible to call Stored Procedure with parameters using Web Api Odata?. How can I achieve that?. My intention is call Web APi Odata function with two parameters to get results I'am expecting. I was thinking about using "Views", but unfortunately i have to pass parameters to get more specific results. Thanks for advise!

Expose your stored procedure as an unbound OData function. An OData function is invoked using GET with the parameters specified in the URI. For example:
GET http://host/SomeFunction(Param1=123,AnotherParam='a string')
You will need to declare the function as part of your OData configuration, and write a controller action method to implement the stored procedure invocation. Here's a tutorial to get you started: Actions and Functions in OData v4 Using ASP.NET Web API 2.2

Related

Expose stored procedure using oData service

I need to expose my stored procedure using oData. I have done it using EclipseLink JPA but without odata. Any link to the tutorial or an example will be appreciated.
The best way to expose a stored procedure, will be to create a corresponding FunctionImport for it.
You can create a complex type, that corresponds to the return structure of your procedure, and map the input variables of the procedure to the FunctionImport input parameters.
Note: OData 4 defines something similar called ActionsImports too. The the only difference is, that ActionImports are "side effecting"
If you are using OData 4 and your procedure is changing some data you should use an ActionImport else, if its just reading the data and returning some results use FunctionImport.
If you are using OData 2 and you have a side effecting procedure, You should use a Function Import with IsSideEffecting flag set to true;
Links to implementation documents
Olingo 2: Adding Function Imports to OData Services with the JPA Processor
Olingo 4: How to build an OData Service with Olingo V4
Part 6: Action Imports and Function Imports

How to call SAP GW create_deep_entity method() in SAP WebIDE?

I have been using Web IDE trial version to try out one application.
The application needs to pass data to Gateway method Create Deep Entity.
While using oModel.create() getting an error which says method not implemented (i.e. Plain create method is called with oModel.create(), which is not implemented on SAP Gateway. but what I need to call is create_deep_entity method )
Just need to know is there any syntax that will tell Web IDE that Deep Create is to be called.
there is not a create_deep entity method as such in SAPUI5. If your deep insert is correctly implemented on SAP Gateway, you just need to use a standard create or post mehod on sapui5 with the difference that the data you are sending will not have a flat structure. See the example of #serban Petrscu.
oModel.create({field1: '', field2: 0, childEntity: {field3: 'ABC'}})

Retrieve xml childnode using Odata in dynamics CRM

I've a problem when i try to pass a childnode attribute as a parameter using Odata in dynamics crm 2015!
for example:
when i pass a parentnode it works fine!
but when i pass a child node it does not work :
https://ServerURL/XRMServices/2011/OrganizationData.svc/InvoiceSet?$select=Name,InvoiceNumber,TotalAmount,TransactionCurrencyId&$filter=CustomerId/Name eq 'AttributeValue'
i also tried the "$expand" function but it doesn't work!
https://ServerURL/XRMServices/2011/OrganizationData.svc/InvoiceSet?$expand=Account_Invoice&$filter=Account/Name eq 'AttributeValue'
Your help will be greatly appreciated
Did you try using the OData Query designer?
You could build the query using the UI to make sure it works without wasting time messing with the OData query uri.
Also, please do not use the ServerUrl (assuming you are using .serverUrl()) on client side, always use the clientUrl() property. CRM might be deployed on a different machine name / url than the Url exposed via IIS.

Passing parameters to an OData (GET) method which returns a collection

I am using OData v3. How do I pass a parameter to an OData controller and return a collection? Example of what I am trying to do:
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<ServiceInfoResult> Get([FromODataUri] int instanceId)
{
//etc
}
When I test this, I get this error:
No HTTP resource was found that matches the request URI 'http://localhost:30863/odata/ServiceInfoResultApi(1)'.
No action was found on the controller 'ServiceInfoResultApi' that matches the request.
I am aware of OData actions, but they are not a viable option on this case, because I need to get odata.count returned in the response, but no matter what I do, I cannot get that property returned when using OData actions (even when trying what was suggested here: Web API OData Inlinecount not working and here: Webapi odata expand with entity framework functions). So it would seem my only alternative was to create a new OData API controller for the ServiceInfoResult entity, so that I can avoid OData actions. However, as you can see above, passing in a parameter to a method that returns a collection seems to cause other errors.
Any solutions? And no, I can't update to OData v4, since that presents a whole host of other issues (it would take more time than I have and moreover, it doesn't support DateTime)
UPDATE
Thanks to #Fan Ouyang, I've discovered the reason for odata.count missing in the JSON payload is that I am not returning an entity type. If ServiceInfoResult were an entity in my database, odata.count would be returned. A bit silly that it's not being returned just because of that, but that's just how it is. I'm wondering if there's any workaround. For example, can I download the source code, change 1 line of code and use that? Otherwise, maybe it's time I started looking at OData v4. The project I have is quite big, so that's not a nice thought with the short amount of time I have. So, if there's any alternatives, I'd like to hear them. Thanks!
Try this sample, CheckoutMany method take parameter and return a collection https://github.com/OData/ODataSamples/tree/master/WebApi/v3/ODataActionsSample
Add [EnableQuery] attribute in the CheckOutMany action method, and add $inlinecount queryoption in requset url, you can see odata.count in payload
By the way, V4 support datetime now: http://odata.github.io/WebApi/#04-01-datetime-support
This was recently fixed with the release of version 5.7 (#odata.count is now returned for collections of complex types). More info here:
https://github.com/OData/WebApi/issues/484#issuecomment-153929767

Method with Parameters on Upload MVC Web API 4 Beta

I created a method in order to upload an image to my server by using web api. However I need to send additional parameters while sending multipart form data. What I want to do is to create a methods which gets required parameters and uploads to desired place by looking that parameters. The method is as like as below
public void AddPhotoItemData( int UserID, int QuestID, int QuestTemplateItemID, double Latitude, double Longitude)
What I COULD NOT do is to trig this method while sending multipart form data and these parameters at the same time. Is there anyway to upload a file with a method that has parameters?
Regards,
KEMAL
The method in your question uses an RPC style signature which runs against the grain of the uniform interfaces that an HTTP based API provides. Look at this good blog post to see what are the "philosophical" differences between RPC and HTTP based APIs.
The Web API facilitates creating HTTP based APIs. This implies you'll be sending "representations" to specific URIs using the standard GET/POST/DELETE & other HTTP methods. That's why multiple "parameters" in a "call" aren't really supported without wrapper of some sort.

Resources