Tuesday, May 22, 2007

Using tokens in your site definitions

When working with site definitions, or onet.xml, as the feature also has been called from time to time, you can now use different tokens to refer to the site collection you're working in or the site you are creating itself.

For instance, if you are to refer to a file on your root web, say a picture in an image library, you can insert the url for image as Url="~sitecollection/myimagelibrary/myimage.jpg".

You can also refer to the site you are creating with the site definition. If you want to refer to a newly created image library in your new site, you would write Url="~site/myimagelibrary/myimage.jpg".

So, the two new tokens are "~sitecollection" and "~site". These are also referred to when setting custom master pages. For more information, see this article on MSDN.

For my case, I wanted to place a web part on my new site when it is created. This web part was going to have 3 links, all pointing to new lists or document libraries created when the site was created. So, for each of the links, I entered the url using the token "~site", like this:


This url will most likely correspond to the actual url you have given your list or document library, i.e. if your list further up in the onet.xml file is created like this:

<List FeatureId="00BFEA71-2062-426C-90BF-714C59600103" Type="103" Title="My Cool Links" Url="Lists/MyCoolLinks" />

you would insert a link to this like this: Url="~site/Lists/MyCoolLinks".

No comments: