Seiten

Donnerstag, 7. Januar 2016

Sharepoint 2013 Online Workflow - Get current Time and Date

Hi,

at the moment I'm working very much with Sharepoint 2013 Online and - thank god - I have access to the SharepointDesigner.

Background

I was trying to create a workflow which had the purpose to check a list of treatys. If they run out of time, they may need to be quit. This workflow runs once a day forever (simple infinity loop). 

It wasn't very beautiful, that the workflow has started every day on the same time I started the workflow the first time (9 o'clock in the morning). Looking in the future, the amount of treatys will raise. Therefor using the resources of sharepoint during the daybusiness, is not very helpful. Yes of course, I could wake up in the middle of the night an start the workflow once but this solution is too simple and not cool.

So I was looking for a possibility to trigger the workflow by time. Specify the starting time isn't complicated. You can specify it using a sharepint list and capture the entry by the workflow or just set a workflow variable with the specific time.

But, the workflow still not knows the current time and date.


Solutions of the Web

I found some solutions on the web but they weren't very helpful for my situation. Why? Well, the workflow willl never ends. he will always run. Therefor it is necessary it requires less ressources and produces less amount of data or using less memory. 
  • One solution was to create or modify a dummy file and capture the datetime of creation/ modification. 
  • the second solution suggests to create a history log entry with an unique description. Then the workflow has to capture the creation date of the log entry. Well this would work but considiring my problem, the system would create one log entry each day. That means 365 - btw 366 in this year 2016 - entries each year would be stored in the system. I haven't figured out how to delete them. Maybe it is very easy.

My Solution

I found the following blog entry of Chris Wellons. He describes his problem that he doesn't trust the local system time and wanted to get the time and date from a different source.
He describes some services which offered an API to get time and date as well as some  services using JSONP but they are alredy down.

Then Mr. Wellsons had an "heureka": 
"This is when the lightbulb went off: web servers hand out date strings all the time! It’s a standard HTTP header: Date!"
Now we are in business. 
Building Workflows using the SharePoint Designer, it offeres the action "Call HTTP Webservice".
So all we need is calling a GET-Method to a random website, filtering the response for "date" and voilá, we have the current date and time (GMT).

Step by step: 
  1. Reading the specification RFC 2616 Chapter 14.18 (Wikipedia), the respond as well as the request of an HTTP Request contains the date and time of the request as well as of the respond.To check the specification, use Web-Sniffer to see which content your request will have.
    The request to google will respond with the following information:

    The field "Date" ist interesting, because it rovides the information about the current time (GMT) and date.
  2. Open SharePoint Designer and create a new Workflow 2013, not 2010.
  3. Create a new dictionary
  4.  Add the following elements:
    I'm not sure, if this is necessary but it will work.
  5. Store the Output in a new variable of the type "dictionary" and name it "dictionary"
  6. Create "Call HTTP-WebService"-Action
  7. Click on "This" and add the webservice. To be mentioned: I don't want to get the content of the webservice. I'm only interested in the meta information. Therfore i used the follwoing third party service to transform the respons in propper JSON style: http://www.firstamong.com/json/index.php?q=
    This is a suggestion by HyongGuk Kim. We add http://www.google.de to the adress and write it to the HTTP Call. The HTTP-Method is set to "HTTP Get".
  8.  The respond has to be stored somewhere. Therefor we store it in a variable "Header" of the type "dictionary".
  9. Furthermore we have to set a variable of the type "dictionary" to tore the respond output for "RespnseStatusCode". But we will not need it. The Software will create an error, if no variable is set.
  10. Click with your right moue button on the "Call HTTP-Webservice"-Action and choose "properties". Set the dictionary we created in step 3 to "Request Header" like shown in the following picture:
  11. Now first test it. Add "Send Mail"-Action and send it to your self with the content of the variable "Header". Save and publish your workflow and start it.
  12. The variable Header has the following content:



    Using the WebService JSON Parser Online by Olivier Cuenot, you can transform the output of the header in a much better human readable style:



    Now you can see, that "COnnecntion", "Date", "Server" and "X-Powered-By" are arrays containing one field only. Now we can build our parser to get the information from the first field of the array "Date".
  13. Go to your workflow again. Add "Get Item from Dictionary"-Action and write for "item by name or path":
    Date/(0)


    Why? Well Date, Server, Connection,... are all root elements of the type array. Each of them only consits of one element. The first element will be assigned by using the index 0 in brackets. Of course you can get the information about the Server or the Connection on the same way like writing: Server/(0) or Connection/(0) .
  14. The infomration are stored in the variable "Header". Store the output in the variable "Date" of the type "Date and Time".

    Add the variable "Date" to the "Send a Mail"-Action. Looking Up the wrkflow variable, you can retrive the infomration tored in the date variable of the type "date and time" in different format, e.g. long/ short time, long/ short date, string or iso
  15. Finally save and publish yur workflow and start it.
  16. The mail looks like this:

  17. Now we are able to use the current date and time working with a Sharepoint 2013 workflow.
    To be mentioned: You will see that the respond time is in GMT +-0 timezone and the exact time and date stored in the variable date is in GMT +1. Maybe a nice function of Sharepoint? Don't know.
I know that this isn't a very clean solution. Some points arent exactly clear to my self but it works. Therefor I invite you to add explanations and improvements.

Thanks for reading.


//Edit 17.02.2016:
Meanwhile I have extracted the solution to an seperate website workflow which starts every day at 00:01 and waits 24h until it starts again. The website workflow determines the actual date and write it to a seperate list. In this list i use calculated columns to determine the date of yesterday, of tomorrow as well as the last and the next year.

  

References

  • http://nullprogram.com/blog/2013/05/11/ , 07.01.2016 
  • http://www.wonderlaura.com/Lists/Posts/Post.aspx?ID=224 , 07.01.2016
  • http://blogs.msdn.com/b/sridhara/archive/2014/08/21/fix-sharepoint-2013-workflow-recursion-prevention-part-2.aspx ,  07.01.2016
  • https://de.wikipedia.org/wiki/Liste_der_HTTP-Headerfelder#Antwort-Headerfelder , 07.01.2016
  • http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 , 07.01.2016
  • https://blogs.msdn.microsoft.com/sharepointdesigner/2012/09/04/how-to-work-with-web-service-using-call-http-web-service-action-in-sharepoint-designer-2013/ , 07.01.2016
  • http://blog.appliedis.com/2014/10/09/sharepoint-designer-2013-workflow-working-with-web-services/ , 07.01.2016
  • http://blogs.evocom.de/af/archive/2013/08/06/sharepoint-2013-workflows-teil-1-call-http-web-service.aspx , 07.01.2016
  • https://sergeluca.wordpress.com/2013/04/09/calling-the-sharepoint-2013-rest-api-from-a-sharepoint-designer-workflow/ , 07.01.2016
  • http://json.parser.online.fr/ , 07.01.2016 

Keine Kommentare:

Kommentar veröffentlichen