Sitecore 7 is buzzing!
While unsure about the release data, MVP's around the world are taking the deep dive.
So am I, diving in the new Sitecore version, that Sitecore call a "developer release".
What's good about oData!
oData is the "open data protocol" that microsoft came up with. It's like Rest, but with some additions.
Several products can connect with oData data sources, some examples are Excell, Lightswitch, Sharepoint...
Why now?
Sitecore 7 brings new API's that make this possible with far less effort than before!
The new search API is responsible: The searchmanager exposes an IQueryable.
Why do we need this? oData converts query arguments to Linq expressions, thus enabling us to automagically, using oData, query Sitecore indexes! how neat.
Show me, Show me!
Was this intro to long for you :-) Time for some code!
I'll explain more, after the code.
Sitecore 7 brings new API's that make this possible with far less effort than before!
The new search API is responsible: The searchmanager exposes an IQueryable.
Why do we need this? oData converts query arguments to Linq expressions, thus enabling us to automagically, using oData, query Sitecore indexes! how neat.
Show me, Show me!
Was this intro to long for you :-) Time for some code!
I'll explain more, after the code.
- [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerSession)]
- public class WcfDataService : DataService<ReturnItemContext>
- {
- public static void InitializeService(DataServiceConfiguration pConfig)
- {
- pConfig.DataServiceBehavior.AcceptAnyAllRequests = true;
- pConfig.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
- pConfig.UseVerboseErrors = true;
- pConfig.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
- pConfig.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead);
- }
- protected override ReturnItemContext CreateDataSource()
- {
- }
- }
- public class ReturnItemContext
- {
- public IQueryable<ContentItem> ContentItems
- {
- [WebGet]
- get
- {
- using (IProviderSearchContext lContext = SearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
- {
- WebOperationContext lOp = WebOperationContext.Current;
- NameValueCollection lParameters =
- (lOp == null) ? null :
- (lOp.IncomingRequest.UriTemplateMatch == null) ? null : lOp.IncomingRequest.UriTemplateMatch.QueryParameters;
- IModelFilter<ContentItem> lParsed = lParser.Parse(lParameters);
- IQueryable<ContentItem> lSearchQuery = lContext.GetQueryable<ContentItem>();
- IQueryable<object> lFiltered = lParsed.Filter(lSearchQuery);
- return lFiltered.Cast<ContentItem>().ToList().AsQueryable();
- }
- }
- }
- }
The InitializeService sets things up for this. You can read more on the WCF data service here.
As you can see, the main thing is the ReturnItemContext and it's property called ContentItems.
The ContentItems property provides a IQueryable<ContentItem>.
The ContentItems property provides a IQueryable<ContentItem>.
When an oData consumer accesses this property, the search engine is called (line #34).
The oData query that the client generates, is applied to the Linq query (line #35) by using an open source project called Linq2Rest.
When finally the Linq query is executed (line #36), the magic happens and the query is run.
The oData query that the client generates, is applied to the Linq query (line #35) by using an open source project called Linq2Rest.
Example oData
The following call to the WCFDataService using the following URL: http://.../WcfDataService.svc/ContentItems?$filter=ParentId eq '11111111111111111111111111111111'
Trigger the query where the oData part [$filter=ParentId eq '11111111111111111111111111111111'] is parsed by Linq2Rest and added to the linq statement. oData clients know to create these URL arguments and will do this.
Using Excell as oData Client
Excell becomes an oData client, when a free business intelligence product from Microsoft called PowerPivot is installed. It adds a new Ribbon that enables you to use a oData datasource.
Powerpivot |
1. button to start Table import wizard
2. location for oData service URL.
Using Lightswitch as oData Client
Lightswitch is a tool from microsofts that (so they say) "...is a simplified, self-service development tool that enables you to build business applications quickly and easily for the desktop and cloud.".
Lightswitch provides an wizard to connect to an oData service and generates CRUD screens for this.
To test Lightswitch as a client for the service, I followed some simple steps.
1. start a lightswitch (windows) project
2. add a datasource
After this wizard, the tool inspects metadata, and generates a datamodel, in our case with one table.
3. Next, I added a screen
In this screen I configured a filter, "where parentid=111111111111111111":
Pressed F5 and voila, a windows client for sitecore (prototyped):
Conclusion
Of course, you can use oData for self written clients. This makes perfect sense!
And know, with all the new stuff in sitecore 7, you can do this!
The code above is written to get it working, understanding how it works -> Don't use it!
Sharepoint should be able to connect, but I did not test it. For a complete list of oData clients, check http://www.odata.org/ecosystem/.
Resources
http://www.odata.org/
http://linq2rest.codeplex.com/
http://sdn.sitecore.net/
http://msdn.microsoft.com/en-us/data/odata.aspx
http://www.microsoft.com/en-us/bi/powerpivot.aspx
http://msdn.microsoft.com/en-us/vstudio/ff796201.aspx
http://blogs.msdn.com/b/officeapps/archive/2012/10/24/using-an-odata-service-in-apps-for-sharepoint.aspx