web api odata service - return complex type - asp.net-web-api

i want to create web api odata service that return reault type which consist of collection data member and additional members like this service return:
http://services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
as you can see the result type is consist of collection data member and additional members
can anyone send me a sample how to do it?
i can't succeed to create this kind of complex type and to be able filter the collection items by there values
as yuo can see in this query it return all the result without filter the items
services.odata.org/OData/OData.svc/Suppliers
i want to be able filter this type like this:
services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
in this query i managed to filter the collection member items and still returning the other data members.

If you just want to implement filters like: services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
Please check the sample at http://aspnet.codeplex.com/SourceControl/changeset/view/903afc4e11df#Samples%2fNet4%2fCS%2fWebApi%2fODataServiceSample%2fReadMe.txt
It has a supplier and address model with queryable attribute. It should work with the same $filter query.

Odata support was implicit in asp.net webapi RC.
You just had to return a IQueryable from you Actions and mark it with [QueryableAttribute].
Only this much supported querystring based data filtering.
I was a bit disappointed when I saw the [QueryableAttribute] doesnt build in RTM.
In RTM it’s available as a separate package, Microsoft.AspNet.WebApi.OData on Nuget in a preview/alpha form. Full release is coming later this fall. You can grab it from here(http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData). There is a nice overview post available (http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx)

Related

Microsoft Dynamics API - Getting Fields from Entity with Navigation

I am trying to get the field list from an entity, in example contact, I have successfully done it doing a Request to:
EntityDefinitions(LogicalName='contact')/Attributes/Microsoft.Dynamics.CRM.AttributeMetadata
But now I need to get the Navigation Properties defined on this entity, I have found information on how to get Navigation Properties when querying a specific record by id, but in this case I need it when getting the field list.
Thanks in advance for your help
Referring the docs: https://learn.microsoft.com/en-us/powerapps/developer/data-platform/webapi/query-metadata-web-api
You already have the Lookup attributes, AttributeMetadata has everything.
If you want to only read the metadata of lookups, change the type to LookupAttributeMetadata.
EntityDefinitions(LogicalName='contact')/Attributes/Microsoft.Dynamics.CRM.LookupAttributeMetadata
To obtain relationships info alongside the columns list then an additional query is required, RelationshipDefinitions:
/RelationshipDefinitions/Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata
/RelationshipDefinitions/Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata
filter on ReferencedEntity / ReferencingEntity to limit the results
/RelationshipDefinitions/Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata
filter on Entity1LogicalName / Entity2LogicalName / IntersectEntityName

How to distinguish entities in Dynamics CRM using the web API?

I am using the request to GET "/api/data/v9.0/EntityDefinitions" to list all the entities that are present in Dynamics and get basic information regarding them (EntityMetadata EntityType).
I know that some of the entities are standard, custom, non-createable, non-updateable, etc.
To check if an entity is a custom I can use a field named IsCustom from EntityDefinition and if it is true then the entity is custom. But for non-createable entities and others (I don't know how to call them properly) this approach is not applicable, because there is no such field.
For example, 'activitypointer' supports only GET operation (activitypointer EntityType). Records of this type cannot be created. But from the request above, I cannot get the correct information about that.
Can entities be distinguished?
P.S.: maybe I did something wrong and need to look elsewhere?

dynamics crm 365 plugin apply QueryExpression to custom list of entities

In my plugin, I have a list of entities. I would like to apply the query expression that is passed in the input parameters
QueryExpression query = PluginExecutionContext.InputParameters["Query"];
to a custom list of type entity.
List<Entity> myList;
The entities in the list match the same attributes as the entities used in the plugin context. Is there a way to apply a QueryExpression to a list of entities, or convert a QueryExpression to linq?
QueryExpression is really just a wrapper around FetchXML, which is just an XML schema for queries in Dynamics CRM. If you want to pass in a query as a parameter to a plugin, you could set up an custom entity call "query" or something to that effect and add a field of type textarea to that custom entity called "fetchxml". Then set up the input parameter of your plugin to accept a record of that custom entity instead of a text parameter. this has the added benefit of allowing you to more easily edit the input parameters of the plugin.
Of course, you could always just put the raw fetchXML into the parameter as text, but I can tell you from experience that this will come back to bite you as it is extremely hard to maintain because any changes elsewhere in the system could completely trash your plugin.
If you want to know more about how to get the fetchXML for a certain query or have any other questions, just shoot me a comment.
Is there a way to apply a QueryExpression to a list of entities
Answer is No.
QueryExpression & FetchXML is native of Dynamics CRM, it can be used against CRM service (Database) only. I assume this plugin is on Retrieve message and you are trying to use the system's query expression from that Retrieve service call against your own dataset of entity (List<Entity>). But why?
, or convert a QueryExpression to linq?
No. I know the reverse is possible.

WebAPI - odata service adding ForeignKey

i am building my the model using ODataModelBuilder, i am trying to create navigation property however in the metadata i dont see any foreginkey indication, in my solution i am not using EF, so there is no foreignKey attribute, is it possible to add it by code?
As you clarified in your comment, the reason you want to add foreign key information is because your client application is not including related entities when you query the main entity. I don't think foreign keys are the problem here.
As an example, I'll use two entity types: Customer and Order. Every Customer has some number of associated Orders, so I have a navigation property on Customer called Orders that points to a collection of Orders. If I issue a GET request to /MyService.svc/Customers(1), the server will respond with all of the Customer's information as well as URLs that point to the related Order entities*. I won't, by default, get the data of each related Order within the same payload.
If you want a request to Customers(1) to include all of the data of its associated Orders, you would add the $expand query option to the request URI: /MyService.svc/Customers(1)?$expand=Orders. Using the WCF Data Services client (DataServiceContext), you can do this with .Expand():
DataServiceQuery<Customer> query = context.Customers.Expand("Orders");
However, WebAPI OData doesn't currently support $expand (the latest nightly builds do though, so this will change soon).
The other approach would be to make a separate request to fill in the missing Order data. You can use the LoadProperty() method to do this:
context.LoadProperty(customer, "Orders");
The LoadProperty approach should work with WebAPI as it stands today.
I know this doesn't answer your original question, but I hope addresses your intent.
*In JSON, which is the default format for WebAPI OData services, no links will show up on the wire, but they are still there "in spirit". The client is expected to be able to compute them on its own, which the WCF Data Services Client does.

Dynamic query with WCF RIA Services

I use Silverlight 4 with WCF RIA Services (domain services with EF4). Now I'd like to add a functionality, which allow an user to query data based on the criteria user selected (ad-hoc query). I've found that:
-WCF RIA Services doesn't allow anonymous types, so linq projection isn't possible.
-Exposing OData doesn't help (much), because you can't filter data at client-side.
Searching Internet, it seems I can use dynamic linq library described in the following link:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
In short, the above link shows how to pass search predicate to server, and execute query at the server-side.
But how about returning arbitrary data? Anonymous types can't be passed, and I don't want user to retrieve all data, but only those fields user chose. Maybe I should serialize my entity data in domain service and pass it as raw xml? Is it possible? If so, how can I do that?
For one of our scenarios we have a DomainService operation which returns xml strings, it looks something like this:
public IQueryable<WidgetInfo> GetWidgetList()
{
IList<WidgetInfo> widgets = WidgetDatabase.GetWidgets(userId);
return widgets.AsQueryable();
}
where WidgetInfo looks like this:
public class WidgetInfo
{
[Key]
public int Id;
public string Title;
public string WidgetData; // Contains XML description of data
}
I'm going to respond to returning arbitrary data. Take a look at the discussion here: https://stackoverflow.com/a/10018119/178620
I have achieved returning arbitrary data by creating a new POCO Entity, which contains IEnumerable. And I do serialization and deserialization using Json.Net Bson, which is much faster than XML.
Update: There's also the Dynamic Linq Library (https://stackoverflow.com/a/848435/178620)

Resources