Thursday, January 28, 2010

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.

How to bind SharePoint event receivers to a custom list definition

In most cases you want to create your own list definition with your own event receiver that sould only react on events of your custom list.

SharePoint ships with alot of list definitions with unique list type numbers:

100: Generic list
101: Document library
104: Announcements list
and many more.

You can create event receivers that react on events of these list types.

To create a custom list definition you should use the visual studio template "List Definition" shipped with the VSeWSS 1.1. If you create a custom list with base type "document library" its id will also be 101. It's just a copy of the template you selected.

First line of schema.xml after creating the custom list definition project:

PLAIN TEXT

XML:

  1. <List xmlns:ows="Microsoft SharePoint"
  2.   Title="List Definition1"
  3.   Direction="$Resources:Direction;"
  4.   Url="MyDocumentLibrary"
  5.   BaseType="1"
  6.   Name="MyDocumentLibrary"
  7.   Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
  8.   Type="101" 
  9.   xmlns="http://schemas.microsoft.com/sharepoint/">

So what you should do ist change the Type value to a non-used number:

PLAIN TEXT

XML:

  1. <List xmlns:ows="Microsoft SharePoint"
  2.   Title="List Definition1"
  3.   Direction="$Resources:Direction;"
  4.   Url="MyDocumentLibrary"
  5.   BaseType="1"
  6.   Name="MyDocumentLibrary"
  7.   Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
  8.   Type="999" 
  9.   xmlns="http://schemas.microsoft.com/sharepoint/">

You also have to modify the ListDefinition.xml

PLAIN TEXT

XML:

  1. <Elements
    Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
    xmlns="http://schemas.microsoft.com/sharepoint/">
  2.   <ListTemplate
    Name="BskhDocumentLibrary"

  3.                 DisplayName="My Dokument Library"
  4.                 Description="Custom document library with custom event receiver doing cool things."
  5.                 BaseType="1"
  6.                 Type="999"
  7.                 OnQuickLaunch="TRUE"
  8.                 SecurityBits="11"
  9.                 Sequence="110"
  10.                 Image="/_layouts/images/itdl.gif"
  11.                 DocumentTemplate="101"
    />
  12. </Elements>

and the ItemEvntReceiver.xml

PLAIN TEXT

XML:

  1. <Elements
    Id="faca01fe-8478-4116-8b72-1e243bda6268"
    xmlns="http://schemas.microsoft.com/sharepoint/">
  2.   <Receivers
    ListTemplateOwner="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
    ListTemplateId="999">
  3.     <Receiver>
  4.       <Name>ItemDeleted</Name>
  5.       <Type>ItemDeleted</Type>
  6.       <SequenceNumber>1</SequenceNumber>
  7.       <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>     
  8.     </Receiver>
  9.     <Receiver>
  10.       <Name>ItemAdded</Name>
  11.       <Type>ItemAdded</Type>
  12.       <SequenceNumber>1</SequenceNumber>
  13.       <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
  14.     </Receiver>
  15.     <Receiver>
  16.       <Name>ItemUpdated</Name>
  17.       <Type>ItemUpdated</Type>
  18.       <SequenceNumber>1</SequenceNumber>
  19.       <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
  20.     </Receiver>
  21.   </Receivers>
  22. </Elements>

Now your event receiver will be called only by MyDocumentLibrary lists and no longer by all document libraries.

How SharePoint stores field names

In my one of my previous posts I've received a good comment from Mike that got me talking quite a lot about SharePoint List / Document Library Field Names. So it's better to dedicate a post to it. First I'll explain some exceptions and then show how to quickly get a sharepoint field name.

You can't change SharePoint field name

Once you create a field, you can't change the "SharePoint" name for it. Im using this sometimes when creating lists. I'll create a field called FirstName and then rename it to First Name. Like that I have no complications with spaces or any unstandard characters. So now (compared to previous post) I can filter with FilterField1=FirstName. 

 
 

Title field

No matter which list you create (except survey), there will always be one Title field no matter how we have it named. We can recognize this title field as the one that is linked to the item (with edit menu)


No matter what you rename this field to, this field will always be "SharePoint" named Title.

Field names for Out-of-the-box lists

Lists that are already included in the templates (like for example Contacts) have different "SharePoint" names than "Display" names. For example: In a Contacts list we can see a field called Fax Number, but "SharePoint" name for it is WorkFax. How to discover real names? Skip to the end of this post and read more.

Spaces and special characters in field names

Spaces in field names get converted to _x0020_. 0020 represents the Unicode character code. If you'd like to know any other, I've found this very good web character convertion tool. Just enter character into Characters field and you'll find the code in Hexadecimal code points but make it a 4 digit number (for example 20 for space should be 0020).

When using those special characters in URL, the underscore (_) gets converted to %5F

So how the heck to know SharePoint field names?

You COULD mess around with all the specialities and translations and conversions that I've mentioned untill now, but there's an easier way:(two actually):

1. Sort the List / Document Library and check in URL for SortField parameter value.
 



2. Go to List / Document Library Settings, click on the name of the column to modify it and in URL find the last parameter value
 

 

And ofcourse if you need to use the field name in other places than URL (like in SharePoint designer), don't forget to convert %5F to underscore.

P.S. - Yes, I know there is also a way to see SharePoint Field Names in SharePoint Designer (mouseover a field name in Data Source Details), but in this post I wanted to focus on work without it.

Navigating through SharePoint Site using URL

I like to access lists, document libraries directly instead of visiting site or subsite and then clicking my way around. SharePoint has very logically structured URLs. Here are some of the most common:

To access:

Type url http://[URL]+

Lists

/lists/[List name]*

List New Item form

[list URL] + NewForm.aspx

List item details (display)

[list URL] + DispForm.aspx?ID=[item ID]

List item edit

[list URL] + EditForm.aspx?ID=[item ID]

Document Libraries

/[Document Library Name]*

Document library upload

[DocLib url] + /Forms/Upload.aspx

Document library item details

[DocLib url] + /Forms/DispForm.aspx?ID=[item ID]

Document library item edit properties

[DocLib url] + /Forms/EditForm.aspx?ID=[item ID]

Special SharePoint pages

http://[URL]+/_layouts+

View all site content

viewlsts.aspx

Recycle Bin...

recyclebin.aspx

Site Collection's recycle bin

adminrecyclebin.aspx

Create

create.aspx

Site Settings

settings.aspx

New Subweb

newsbweb.aspx

* in cases of Lists and document libraries only spaces get preserved (and translated in %20 in url). Special characters are excluded (for example: list Special Čheck url would be http://[sute url]/lists/Special%20heck/

These things are quite obvious. What I'm trying to point out is if you're accessing certain lists/document libraries/settings often, it's not a bad idea to start paying attention to URLs.

Oznake ponudnika Technorati:

Wednesday, January 6, 2010

Cascading Drop Down Lists

Below find a few screenshots of configuring and using the custom field -

I have a list called 'Continents':


A second list 'Countries':


And a third list 'Cities':


In my fourth list 'My Location' in which I would want to have the three dropdowns displayed, I create three columns : Continent, Country and City using my custom field 'Cascading Drop Down List (With Filter)'.



The three columns created are -


Configuring the columns and using the filter -








When adding a 'New Item' to the 'My Location' list, the first dropdown will have "Please select an Item' (which is also something I added) as the first item in the dropdown.

Cascading Drop Down List (With Filter) in action:



According to the filter set, the list of continents that appear are only the ones that contain 'America'.