INTRODUCTION
I’m developing a Sitecore website for a client where using Angular and WebApi. One of the functionalities is to ensure that search keyword is tracked in Sitecore Analytics. All sounds simple and straight forward, except the Tracker code requires Session to run. And since the code that tracks the keyword is inside your WebApi, it makes it even trickier.
Session is by default not enabled in generic WebApi let alone Sitecore’s one. A great article has been written to allow you to do this:
http://blogs.perficient.com/microsoft/2016/11/sitecore-add-session-to-web-api/
It all works fine except every time you want to use this functionality on different projects, you have to copy/paste the code and redo all over again.
I’ve managed to wrap it into a Sitecore package which you can deploy to various Sitecore websites. A configuration file has also been created so that you can add multiple WebApi controllers without having to change the source code.
HOW TO USE
1. Go to Sitecore Desktop > Installation Wizard and install the SitecoreSessionApi-1.0.zip
2. Go to your Website folder > App_Config > Include > zzzzSitecoreSessionApi and open SitecoreSessionApiConfiguration.xml.
3. Add all your WebAPIs that require session enabled.
<Api>
<Name>AdvancedSearchSitecoreSessionApi</Name>
<RouteTemplate>sitecoresessionapi/advancedsearch</RouteTemplate>
<ControllerToLoad>Search</ControllerToLoad>
<ReadOnlySession>false</ReadOnlySession>
</Api>
<Api>
<Name>MyOtherApi</Name>
<RouteTemplate>sitecoresessionapi/myotherapi</RouteTemplate>
<ControllerToLoad>MyOtherApi</ControllerToLoad>
<ReadOnlySession>false</ReadOnlySession>
</Api>
4. Finally, in your Angular code, to load the WebApi, instead of using /api/[controllername] URL you would use the URL you specify in the RouteTemplate variable above eg. /sitecoresessionapi/myotherapi
DOWNLOAD
Click here to download the package.
That’s it! Hope this helps.
Tommy