Using Cookies with SharePoint’s Data View

November 18th, 2007 | Categories: SharePoint, Web design

In my previous post I’ve explained how to pass parameters from URL to use in a Data View. There is another very useful way: – using Cookies. The process is very similar, using SharePoint Designer:

1. To read the cookie value and use it as a parameter:

Open the properties pane of the XSLT Data View and select Parameters

Add a parameter, name it as you wish, and in the Parameter Source select Cookie. In the next field enter the name of the cookie which to read and again you can enter a default value.

And you can use the parameter value as you wish – for filtering, conditional formatting, as content in the dataview, etc.

2. But how to set or manipulate Cookies?

The easiest way to set a cookie value is with functions I’ve found on QuirksMode. I’ve contacted the author but got no reply so I’ll dare to publish the JavaScripts here. I repeat again: these are the property of QuirksMode. I use the functions below to set, change value or delete cookies 

Set or change value of a Cookie:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        var expires = “; expires=”+date.toGMTString();
    var expires = “”;
    document.cookie = name+”=”+value+expires+”; path=/”;

Delete cookie:

function eraseCookie(name) {

To put the above example into practice. In a DataView I’d be reading a cookie named ShoeSize and use it as parameter $Size in a data view. To set the value of a cookie for example to M I’d use a function:

If the third parameter is not supplied the cookie is active only as long as the browser is open. To change a cookie value to S and to be valid for 3 days, I’d use

When I don’t need the cookie anymore, I just use .

Using cookies to pass parameters between pages has its benefits, like – you don’t complicate with codepages, long URLs, you can pass the parameter between more pages than one, etc. But you can’t pass the parameter with cookie between sites. For this I’d recomend QueryString.