Important notice

Please note that this blog is discontinued here. All the posts are also coppied to my new blog at http://boris.gomiunik.net.
Important: Due to large amount of comment spam further commenting has been disabled here. If you wish to send me a comment, plase look up the same post on my new blog and leave comment there. Thanks!

11/18/2007

Using Cookies with SharePoint's Data View

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();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else
    var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

Delete cookie:

function eraseCookie(name) {
    createCookie(name,"",-1);
}

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:

<script type="text/javascript">createCookie('ShoeSize','M')</script>

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

<script type="text/javascript">createCookie('ShoeSize','S',3)</script>

When I don't need the cookie anymore, I just use <script type="text/javascript">eraseCookie('ShoeSize')</script>.

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.

1 comment:

Jose Antonio said...

Hi Boris, I see that sites like Amazon are using 2 levels of authentication, one that is based on cookies but that doesn't expose any important thing... maybe just some preferences. and other that is the real one using a user name and password.
That would sound like a solution for many of our own web sites, like the E-sejem for sending messages to the "regijski nabiralnik".