How to pass an entity via a Web Service - vb.net

Is there a way to pass the data as an entity within another entity? What I'm trying to get to is this: I'm passing customer details to my database, via the webmethod : PostData. What I want is to pass details such as name, surname, etc as string, but I want to group some details, like vehicle details to be passed as : vehicle.Manufacturer vehicle.Model... etc The other data that are not group-able will be passed as an ordinary string. Hope that makes sense. Is there a way to do this?

You can do this either by XML or JSON, so the object that in the part of request will be like in XML :
<VehicleEntity>
<attribute1>value</attribute1>
<attribute2>value</attribute2>
</VehicleEntity>
or Similarly using JSON
VehilceEntity{
attribute1 :value;
attribute2 :value;
}
Basically it is similar to how you are passing parameter, here it is custom parameter VehicleEntity

Related

How to expand(odata-webapi) all the properties without passing $expand in query string in vb.net

I am using odata v5.7.0 for webapi(vb.net).I need all the properties of object should be expanded without using $expand property on the query string.
Ex: http://localhost:26209/ProductList?$expand=customers/products
into
http://localhost:26209/ProductList
Not sure why you would want to do that and there might not be a clear cut way to achive it.Still if you want to get all the products for a customer you will have to go one at a time by using something like this.
http://localhost:26209/ProductList/customers('customer_id')/products
Else if you are using OData 2 there is an EntitySet for every association, so you an directly query the products EntitySet and use $filter instead. The URL will look similar to
http://localhost:26209/Products?$filter=CustomerId eq *
You can check $filter conventions here

Should we always validate resource id in url and body in HTTP PUT request?

Suppose I am updating a employee record
url - /api/employees/10
body -
{
id : 10,
name : xyz
}
Should I validate for the employee id in url is same as in response? Because one employee can hit the url himself but update the data of another employee by sending another value in the PUT body.
If you have to validate, it's likely that you want to use POST. A POST is not idempotent and you are supposed to manage the change.
PUT is idempotent, and it just creates a resource. It implies that you don't actually care what id 10 is and whether it is a new id or an existing id. You just replace id 10 with the resource you supply. You only use PUT when you know what the uri should be.
Yes, if the representation of the object in the body contains its own key, you should validate that it matches the key from the URL. It's an error for the client to try to PUT an object at /api/employees/10 that isn't a valid value for employee #10's record, so you should check for that and report it as an error just as you would check that the object has correct syntax.
I believe that the best error code to return in this case is 422 Unprocessable Entity, but I might be wrong about that.
Another thing you can do instead is don't include the key at all in the body. However I find that keeping the key in makes sense for consistency with the way the same type of object is represented in other parts of the API (possibly embedded inside other objects). This is especially true when using XML (although it looks like you are using JSON here).

Restlet using Get and Post Methods

Hiiii,
I am developing a small app using Restlet 2.0 API..
I want just to know how to create a URI which accepts more than one parameter for insert query.
Ex:
router.attach("/{patient}/insertpatient", insertpatient);
I want to insert all the info about the patient using POST.
Or Search by ID and address for instance using GET.
thanks,
For the "insert patient" operation, I'd suggest something like a POST to /patients (if the server generates the patient ID) or a PUT to /patients/{patient-id} (if the client generates the patient ID). Either way, the body of the message would contain all of the input data needed to create a new patient entity. Two simple options for serializing the data would be as a URL-encoded form or as JSON.
Lookup by by ID could be a simple as a GET on /patients/{patient-id}. Lookup by address could also be a GET, possibly using a URL-encoded query parameter. For example, you could fetch the patient at address "123 Main Street, apt 4, Anytown, USA, 98765" with a GET on /patients?addr=%20Main%20Street%2C%20apt%204%2C%20Anytown%2C%20USA%2C%2098765

OData / WCF Service Operation - property access through URI?

I use a Service Operation in WCF data service to get a object.
[WebGet]
public IQueryable<sample> GetSamples(int Id)
I can retrieve data by
http://localhost:xx/GetSamples?Id=9
Is it possible to get property of the returned object similar to
http://localhost:xx/samples(x)/property
I've tried http://localhost:xx/GetSamples?Id=9/property, and http://localhost:xx/GetSamples/property?Id=9 etc. Nothing works.
If Sample is a complex type then this won't work.
If Sample is an entity type, then it will work with a small modification. Property access is only possible on a singleton result. WCF DS doesn't know that your service operation always returns a single entity, to tell is to, add an attribute SingleResult to your service operation method. Then the first URL should work: service/GetSample/PropertyName?id=2
If the Sample is an entity type and you know the key property value (or values) then service/Samples(keypropertyvalue)/PropertyName should also work.
What about using a select?
http://localhost:xx/GetSamples?Id=9&$select=property

Retrieving Attribute names of an Entity in MS CRM 4.0

I am trying to retrieve the attribute name and type that exist in an entity, Dynamic Entity to be precise. I have the following code.
DynamicEntity contactEntity = new DynamicEntity();
contactEntity.Name = EntityName.contact.ToString();
Property t = null;
foreach (Property prop_Test in contactEntity.Properties)
{
Response.Write("<br/>Name : " + prop_Test.Name.ToString());
}
I am getting the properties count as 0.
Is it mandatory for me to pass an id to the contact entity. Because i am trying to map attributes from the entity to the attributes i get from an excel file. The end user themselves would be doing the mapping so all i need are the attribute name and type and nothing else. For instance in SQL we have the query
SELECT * FROM TABLE_NAME WHERE 1 <> 1
This query basically returns an empty resultset with only the fieldnames. That is what i am looking for here. Is it even possible ?
In your example above, the dynamic entity does not have any properties set on it. The dynamic entity is a special type in MS CRM that is used when you do not know the CRM type until runtime. If you add properties to the dynamic entity and run your example, you will get however many properties returned that you define.
In order to get the contact attributes, you will need to reference the CRM Metadata Service as explained in the SDK.
There is an example within this download in the HowTo section that shows how to get out the entity and attribute metadata.