Devday 2016

Vi har haft vår årliga Devday, en dag i kunskapens tecken där syftet är att testa nya tekniker under ett paraply av vägledande och smått märkliga teman. Filosofin bakom Devday är att praktisera den pågående interna kunskapsspridningen, exempelvis i form av teknikkvällar, genom att erbjuda en dag för att förverkliga idéer och skapa lagkänsla. Då vi alla är kodfantaster är det ytterligare en bonus att kunna ha lite friare tyglar kring vilka tekniker man kan använda i utvecklandet. Årets övergripande tema var ”MasterDev” där varje lag med givna grundrecept, ett skafferi fyllt av ramverk och sin egen personliga kryddning fick en dag på sig att implementera något ur kategorierna:

– Intranet of things
– Adventures in the shell
– George Booles matkasse-webappar

Lagen tävlade mot varandra i två priskategorier: folkets val-priset, samt teknikpriset. Det sistnämnda gick till laget som plockade flest poäng från en lista med bonusobjektiv, där det fanns allt från dokumentation till unit-tester och användandet av specifika ramverk.

Så vad kunde våra lag koka ihop under en dag?

– En konsolbaserat tinder som använder SharePoints user profile service och visar ASCII art av dina potentiella matchningar. Högerpil för like!

– Recept-appar i varierande utformning som gav menyförslag baserat på valda kategorier och/eller sökord, förslag till restauranger som serverar maten, en ingredienslista om man föredrar att laga den själv, samt passande middagsmusik.

– En flic-integration som triggar text-to-speech över ett Sonos-system.

devday_1

devday_2

devday_3

devday_4

Update Field.JSLink using JSOM or REST

Today I have just a little code snippet to share. This code snippet shows how to update the JSLink property for an existing field using JSOM and REST. For REST I use sharepoint-utilities.

A couple of notes, to update a field we need:

  • A POST request
  • with a header ‘X-HTTP-Method’: ‘MERGE’
  • with __metadata: { type: SP.Field } and JSLink property in the payload
  • and X-RequestDigest‘ header (but it is nicely handled by sharepoint-utilities)

Trigger SP2010 Workflows using JSOM

Today I found out how to start workflows in JSOM (JavaScript Object Model in SharePoint). Nothing special, but since it is not documented, it took me a while to find a solution. Here is the code which I want to keep as simple as possible.

What you need to start a SP2010 Workflow for a list item or a document in JSOM, you need to load SP.WorkflowServices.js and you need to create the manager and get the service, then you can trigger a workflow using the workflow name, the list guid and the guid of the list item:

Here is the code to trigger a workflow for multiple items:

 

The code above is inspired from this gist and sharepoint stackexchange. It is a simplified version that only works for list item workflows and SharePoint 2010 workflows.
Here is an example how you can get multiple items and batch start a workflow:

Why I needed it

I created a simple workflow that is triggered on Item Added and Item Updated. Unfortunately there are already thousands of items in the document library. To trigger them manually is nothing good. But a simple javascript solution did exactly what I wanted.

Create and download a file in javascript

Phew, I spent a lot of time to get this to work:

  • Create a text based file in javascript – a simple csv or txt
  • Download a file in Chrome and Internet Explorer
  • Make Excel understand Unicode characters (å ä ö and many more) in csv directly.

I found many solutions on the Internet that I used to find out a solution that works for me

A deep diving jsfiddle about unicode encodings, bom, line endings: http://jsfiddle.net/kimiliini/HM4rW (unfortunately, does not work in IE). There I learned one important thing: we need a BOM in order to make Excel understand that it is not just ASCII. The BOM (byte order mark) for utf-8 is %ef%bb%bf for utf-8. Without this bom you’ll see the right characters in a text editor (except Notepad of course), but if you open the csv file directly in Excel, you’ll see wrong letters.
Other good resources are FileSaver.js: https://github.com/eligrey/FileSaver.js and download.js: http://danml.com/download.html

This is my solution that is tested in Chrome and IE10:

Safari

Unfortunately in Safari, the link is opened directly in the web browser. You have to press Cmd – S to trigger Save As manually.

Part of SharePoint Utilities

I submitted this code to sputils (SharePoint Utilities) on github.