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/30/2007

Add Content Editor Web Parts a bit faster

Here's a quick tip: If you want to add more Content Editor Web Parts faster to a homepage or a webpart page (to move and edit them afterwards)

1. Switch to edit mode
2. Add a Content Editor Web Part
3. When the page reloads with a CEWP added, hit Refresh (F5) and click "Retry" when Internet Explorer asks you if to resubmit information.

I use this technique to add multiple CEWPs to the page, when I need to add some explanations above some webparts. When I have enough of them, I drag them to different zones and positions and edit the titles and contents.

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.

Using parameters from URL as variables in XSLT Data View

This is a reply to PsychedEric in my post about QueryString. To use params from URL in an XSLT data view is quite simple. Once you have the data view inserted, display the options of the Data View and click on Parameters.

Next click the button "New Parameter" and enter a name for it. Then in a Parameter Source dropdown, select Query String.

 

Under the dropdown enter the variable name - which parameter in URL to read. For example if it will be reading parameter "p" in url webborg.blogspot.com?p=1234 just enter letter p. You can also set a default value if there is no p parameter in the URL.Next click OK.

And that's it. Now you can use this parameter for filtering, or in the Data View Web part as you desire. If you want to use the parameter in the DataView just enter its name with a $ (dollar) sign before it. SharePoint Designer's Intellisense will recognize it.