Tech Space

The Technical Solution Space for SharePoint 2010, MOSS 2007, and .Net (C#, VB.Net)

//


// 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");

0 comments:

Post a Comment