The Autocomplains - Sitefinity OData
The beginning (Autocomplete contains issue)
Along with other improvements and features, Sitefinity 9.0 introduces a new “Content as a service” API based on OData 4.0 protocol. This is a huge step and a great benefit for developers, because till then there was no other way to expose content via AJAX services on the frontend, except writing your own. With the release of Sitefinity 9.0, Progress gave the developers the opportunity to better focus on the business layer and leaving more of the trivial work to Sitefinity.
Sitefinity team has provided us with a public client sdk for working with Sitefinity OData services. You can get it from Sitefinity's public NuGet. Included is a basic javascript API, a new Kendo datasource type "sitefinity" and a documentation.
Of course there will always be limitations and more complex scenarios which developers should take care of. I’ll try to show you some of the issues we had using OData services of Sitefinity, their limitations and what solutions we’ve chosen to handle the client’s request.
Assumptions
If you don’t feel comfortable with OData you can check the office OData website and the Getting started section
Recently
I came across a question in internet how to use Sitefinity OData API to filter content items by a given string (using autocomplete in the particular case). Fortunately I had exactly the same issue few weeks ago and was able to quickly help and gave real world example of this.
Then I realised that I had more common cases which made me feel uncomfortable as a developer, because it should happen out of the box with ease, but more or less it wasn’t that enjoyable.
The case
An intranet portal where an employee should be able to create reports where the title of the report could be the same as of some of his tasks.
The issue
Piece of cake, right? Just need to have an autocomplete which will show all the tasks of the give employee which title contains the given string.
First I started with easiest one - Kendo autocomplete with Kendo datasource of type "sitefinity" and using sitefnity webservices sdk.
HTML
JavaScript
This works like a charm ;)
... but with "startswith" operator. I wanted to use the "contains" filter and so I change the "filter" property of the autocomplete to "contains".
Aaand booom!
We get "Unknown operator type" exception. Ok, so "contains" operator is not supported. Not very cool. What we can do here is to not use the "sitefinity" type datasource and instead use "odata-v4" type and directly call the the service.
The solution
I used custom read function of the kendo datasource instance where I get the input string from the “filter” object and manually request the OData service. Sample code I won’t get deeper in the project, instead I will give you a sample code (snippet) to demonstrate the solution.
HTML
JavaScript
Conclusion
Sitefinity OData services and its Client SDK is a good starting point, but there is a lot of work on it. Just keep in mind that there could be issues with some fairly simple and straightforward tasks.
Moreover with the above solution you have the flexibility to $expand some navigation properties, add additional filters and so on.
Along with other improvements and features, Sitefinity 9.0 introduces a new “Content as a service” API based on OData 4.0 protocol. This is a huge step and a great benefit for developers, because till then there was no other way to expose content via AJAX services on the frontend, except writing your own. With the release of Sitefinity 9.0, Progress gave the developers the opportunity to better focus on the business layer and leaving more of the trivial work to Sitefinity.
Sitefinity team has provided us with a public client sdk for working with Sitefinity OData services. You can get it from Sitefinity's public NuGet. Included is a basic javascript API, a new Kendo datasource type "sitefinity" and a documentation.
Of course there will always be limitations and more complex scenarios which developers should take care of. I’ll try to show you some of the issues we had using OData services of Sitefinity, their limitations and what solutions we’ve chosen to handle the client’s request.
Assumptions
- You already know what is Progress Sitefinity CSM and know how to use it to create great online business presence and user experiences with it.
- If you are still not sure, you can benefit from the official Sitefinity website and its Developer network
- You have basic (at least) knowledge of OData 4.0 protocol.
If you don’t feel comfortable with OData you can check the office OData website and the Getting started section
Recently
I came across a question in internet how to use Sitefinity OData API to filter content items by a given string (using autocomplete in the particular case). Fortunately I had exactly the same issue few weeks ago and was able to quickly help and gave real world example of this.
Then I realised that I had more common cases which made me feel uncomfortable as a developer, because it should happen out of the box with ease, but more or less it wasn’t that enjoyable.
The case
An intranet portal where an employee should be able to create reports where the title of the report could be the same as of some of his tasks.
The issue
Piece of cake, right? Just need to have an autocomplete which will show all the tasks of the give employee which title contains the given string.
First I started with easiest one - Kendo autocomplete with Kendo datasource of type "sitefinity" and using sitefnity webservices sdk.
HTML
JavaScript
This works like a charm ;)
... but with "startswith" operator. I wanted to use the "contains" filter and so I change the "filter" property of the autocomplete to "contains".
Aaand booom!
We get "Unknown operator type" exception. Ok, so "contains" operator is not supported. Not very cool. What we can do here is to not use the "sitefinity" type datasource and instead use "odata-v4" type and directly call the the service.
The solution
I used custom read function of the kendo datasource instance where I get the input string from the “filter” object and manually request the OData service. Sample code I won’t get deeper in the project, instead I will give you a sample code (snippet) to demonstrate the solution.
HTML
JavaScript
Conclusion
Sitefinity OData services and its Client SDK is a good starting point, but there is a lot of work on it. Just keep in mind that there could be issues with some fairly simple and straightforward tasks.
Moreover with the above solution you have the flexibility to $expand some navigation properties, add additional filters and so on.