views:

3748

answers:

4

When I try to retrieve a column which is a hyperlink I get two items that are comma delimited instead of one.

When I pull item["ColumnName"] I get its value:

http://www.google.com/article/583,%20title%20gets%20stars

Why is it showing the link, and title?

A: 

That is how SharePoint stores links. First the URL and then the Title that's actually shown on the page.

From the SharePoint documentation:

"The URL field uniquely consists of two strings separated by a comma and space. One string contains the URL path and the other contains the description used as hyperlinked text."

You have to split the string to get two parts.

string url = field["URL"].Split(',')[0];
string title = field["URL"].Split(',')[1];

Code is not optimal, but just to show you exactly what I mean.

Oliver, you didn't specify SharePoint version. My answer is for 2003 version. If you have MOSS, take a look at SPFieldUrl and SPFieldUrlValue classes.

muerte
+5  A: 

Because at the lowest level, all Sharepoint fields are stored as strings. The GetFieldValue method of an SPField accepts a string, and it is up to the logic of that field class to read that string and convert it into a meaningful value object.

item["FieldName"] returns a generic object that represents the field value. By itself the object is usually useless, except as the raw string representation of the data.

If you use the GetFieldValueAsHtml() method, it will return <a href="url">title</a>:

//if field is of type Hyperlink, returns <a href="url">title</a>
item.Fields["FieldName"].GetFieldValueAsHtml(item["FieldName"])

Or

//if field is of type Hyperlink, returns Url, Title
item.Fields["FieldName"].GetFieldValueAsText(item["FieldName"])

Or

//if field is of type Hyperlink, returns Url
item.Fields["FieldName"].GetValidatedString(item["FieldName"])
Rex M
That will return HTML code for rendering the field value directly on the page
muerte
Thanks meurte, wasn't done answering.
Rex M
Thats WSS 3.0 right?
muerte
+7  A: 

You can extract the actual url and the description from the column value this way:

SPFieldUrlValue fieldValue = new SPFieldUrlValue(myItem["URL"].ToString());

string linkTitle = fieldValue.Description;

string linkUrl = fieldValue.Url;
Magnus Johansson
A: 

The SPListItem URL property does return the URL for the SPListItem including the List/Documetn Library name, but it doesn't return the full URL including the server and site names.

To get the full URL, you can either concatenate the SPLIstItem.Web.Url and the SPListItem.URL, or extract the full URL from the SPListItem.XML data like this:

foreach (SPListItem item in list.Items)
{
  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.LoadXml(item.Xml);

  XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
  nsm.AddNamespace("z", "#RowsetSchema");

  string fullURL = xmlDoc.SelectSingleNode("z:row", nsm).Attributes["ows_EncodedAbsUrl"].Value;
}

http://insomniacgeek.com/code/how-to-get-the-full-url-from-splistitem-in-sharepoint/

Mo