Monday, April 28, 2008

Link to an item when running SPSiteDataQuery

Many a times we will need to run SPSiteDataQuery on a list to get the results from a sitecollection. Now once we have retreived the results from the query we need to get the url of each item. There is a hidden column "EncodedAbsUrl" which consists of complete URL to an Item. For some reason accessing this column doesn't give us the complete URL that we need (I don't know why). But there is another trick to get the URL of an item. There is another hidden column "FileRef" in each list. This column data has got the URL with the item ID concatenated to it, something like this:

row["FileRef"] = 1;#Popular Wiki/How To Use This Wiki Library.aspx

**row is from the datatable returned when you run web.GetSiteData(query) and query is the constructed SPSiteDataQuery.

We can retreive the URL part from it and construct the complete link from the root site. Something like this:

string url = row["FileRef"].ToString().Substring(row["FileRef"].ToString().IndexOf("#") + 1);

string finalUrl = site.MakeFullUrl(url);

You can read more about MakeFullUrl() method here msdn

2 comments:

Anonymous said...

Hi, looks like the only place to find help on this issue but this was not working for me, I had to write it from scratch, here it is:

string [] slipFileRef = row["FileRef"].ToString().Split('#');

string fullUrl = "Test this link";

Anonymous said...

looks like the only place for this issue. Your code was not working for me, I get a FileRef like 1;#Lists/MyList/1_.000 where 1 is the ID of the list's item.

here is the code I wrote for this:

string [] slipFileRef = row["FileRef"].ToString().Split('#');

string fullUrl = "Test this link";

cheers,
Damien

Dogpile
Powered By Blogger