Wednesday 17 February 2010

Reading & Writing SPFieldUrl field values Programmatically...

//
// Returns the value of an Url-Field.
//


public static string GetFieldValueUrl(this SPListItem item, string fieldName)
{
if (item != null)
{
SPFieldUrlValue urlValue = new SPFieldUrlValue(item[fieldName] as string);
return urlValue.Url;
}
else
{
return string.Empty;
}
}

Example:

// Get the url of a SPFieldUrl field
string url = item.GetFieldValueUrl("URL");



//
// Sets the value of an URL-Field.
//


public static void SetFieldValueUrl(this SPListItem item, string fieldName, string url, string description)
{
if (item != null)
{
item[fieldName] = new SPFieldUrlValue()
{
Description = description,
Url = url
};
}
}


Example:

// Set the url and description of a SPFieldUrl field
item.SetFieldValueUrl("URL", "http://www.TestSite.com", "Test' Blog");

No comments:

Post a Comment