Sitecore CMS and everything related RSS 2.0
 Thursday, April 16, 2009

UrlString provides a simple wrapper around URLs, both relative and absolute. Many Sitecore UI components receive data from querystring parameters, and UrlString provides convenient  method for constructing URLs with querystring parameters.

Assembling an URL:

UrlString url = new UrlString("/path/to/mypage.aspx");

url["id"] = "{some-id}";
url["db"] = "master";

string result = url.ToString(); // "/path/to/mypage.aspx?id=some-id&db=master"

Tweaking existing url:

UrlString url = new UrlString("/path/to/mypage.aspx?id=some-id&db=master");

url["db"] = "web";
url["mode"] = "new";

string result = url.ToString(); // "/path/to/mypage.aspx?id=some-id&db=web&mode=new"

Some methods in Sitecore API accept UrlString to add more data. ItemUri class can embed itself in a UrlString, so that you can pass information uniquely identifying a Sitecore item in a URL:

UrlString url = new UrlString("page.aspx");

Sitecore.Context.Item.Uri.AddToUrlString(url);

string result = url.ToString(); // "page.aspx?id={id}&la=en&v=1&db=master"

Compared to System.Uri, Sitecore's UrlString is very lax in accepting any kinds of input and giving it back.

Thursday, April 16, 2009 12:30:48 PM (FLE Standard Time, UTC+02:00)  #    Comments [0]
Sitecore | API
 Monday, April 13, 2009

Most of the data in Sitecore ends up stored and passed around as a string, hence we have quite a few helper classes to deal with string values. ListString class helps maintaining a list of strings that is serialized as a string itself. Like this:

“Fred|John|Derek”

What you see above is three strings, “Fred”, “John” and “Derek” serialized to a single string, each string being separated by the pipe ‘|’ symbol. Here’s how to use the ListString to create similar list:

ListString list = new ListString();

list.Add("Fred");
list.Add("John");
list.Add("Derek");

string result = list.ToString(); // -> "Fred|John|Derek"

Now you need to parse it somewhere else:

string rawValue = “Fred|John|Derek”;

ListString list = new ListString(rawValue);

int count = list.Count; // -> 3
string first = list[0]; // -> "Fred"

foreach(string s in list)
{
  // supports enumeration too
}

list.Add("Mary");
string result = list.ToString(); // -> "Fred|John|Derek|Mary"

Note that the class doesn’t check and escape incoming strings, so it’s your responsibility to make sure the values you pass do not contain a separator symbol.

Sitecore uses ListString alot internally, but it’s always better to use higher level API designed for each specific case, if such exists. A good example of that is MultilistField. So when else ListString can be handy? Whenever you need to pass multiple values as one, such as passing parameters, implementing a new field type that stores multiple values, etc.

Monday, April 13, 2009 4:54:23 PM (FLE Standard Time, UTC+02:00)  #    Comments [0]
Sitecore | API
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
 Alex de Groot
Few words about SiteCore from Holland
 Alexander Shyba
Sitecore Support
 Anders Dreyer
Anders Dreyer on Sitecore Development
 Jakob Christensen
Sitecore Core Development
 Lars Fløe Nielsen
Lars's ramblings about development and business processes
 Ole Thrane
Sitecore API
 Runi Thomsen
Runi Thomsen Sitecore Toughts
 The Sitecore Experience
The Sitecore Experience
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010
Alexey Rusakov
Sign In
Statistics
Total Posts: 211
This Year: 0
This Month: 0
This Week: 0
Comments: 0
Themes
Pick a theme:
All Content © 2010, Alexey Rusakov
DasBlog theme 'Business' created by Christoph De Baene (delarou)