In my previous post I've described several approaches available to developers who builds apps for anonymous users in SharePoint 2013 and in this post I'd like to describe in details how to make it using a Workflow.
- Create a new App and specify the necessary permissions for your logic
- Add a site workflow
- Make an infinite loop
- Make a call to the REST API to get data in JSON format
- Save results inside a library in the App Web again using REST API
- Use delay activity to postpone execution to (1, 5, 30... minutes)
- Make an AJAX call to get the saved file
- Display data for user as required by App logic
First of all, we need to build a request url combining current host web (app web depending on your request) with a relative path ("_api/web/lists" in my case). Then we need to create a dictionary for request headers with one value (Accept = "application/json; odata=verbose") to get data in JSON instead of XML. Then I pass these variables to HttpSend activity as Uri and RequestHeaders properties, select Get as a Method and map RequestContent to a new variable (data) for later usage. Also I log ResponseStatusCode in Workflow History.
Save to library
The save logic is almost the same as get with a few changes: now the request relative path is "_api/web/getfolderbyserverrelativeurl('Data')/files/add(overwrite=true,url='data.js')", HttpSend activity method is POST and RequestContent reuses the data variable from above "var appData = " + data.ToString() + ";" .
Implementing Timer Job using Workflow 2013
As shown in the 1st workflow picture I'm using an infinite DoWhile activity (with always true end condition), the last activity in the Body area is Delay activity that is configured for 1 minute duration of delay.
WF start logic
For SharePoint 2013 style workflow there is a new API that can be used from a JS code using CSOM. For regular App users I've created a button that checks whether an instance of the workflow is started and (if not) starts a new instance. I had to create 2 functions due to async request execution. It's required to get a list of App Web workflow instances to check whether my workflow is already running and to get a subscription by id to start a new workflow instance. And it's possible to get these 2 things in one request using CSOM.
Get data using jQuery getScript function
jQuery allow dynamic loading of a script file. By default, it has caching disabled thus I'm always getting actual data, but caching can be enabled in the getScript options to improve performance if your data is suitable for caching.
The script file has a very simple format:
and can be used as a JS variable after the script is loaded.
Live demo available here.
You can download the App and its code on https://spsitepro.codeplex.com.