Thursday, February 9, 2012

Reading / Getting and Updating SharePoint Hyperlink column Value (SPFieldUrlValue)

This is pretty simple implementation, but you have reached to this post means may be you are having some problem with reading or updating values for the SharePoint Hyperlink column.


using (SPSite site = new SPSite("http://SPsiteURL"))
{
    using (SPWeb web = site.OpenWeb("WebName"))
    {// Get the list
        SPList list = web.Lists["ListName"];       // Create a link field value.
        SPFieldUrlValue HypLinkField= new SPFieldUrlValue();
        HypLinkField.Description = "SharePoint Developer Center";
        HypLinkField.Url = "http://sharepoint.com";// Create a new list item and set the URL field value.
        SPListItem sListItem = list.Items.Add();
        sListItem[SPBuiltInFieldId.URL] = HypLinkField;
        sListItem.Update();
    }
}

First I will show you code which will do update to the existing hyperlink column value. In above code sample, SPFieldUrlValue represent SharePoint Hyperlink column. Hyperlink column has two properties, one contains description and other contains actual web url. above example shows how to assign both values.

Now,to read URL property of the Hyperlink column, follow below steps.

Item["SP-HyperLinkColumn"] = "http://www.msdn.com, MSDN Microsoft";
Item.Update(); 

Don’t forget to introduce space after comma, which actually indicates that it is description of the Hyperlink Column.

Or Alternate approach is,

SPFieldUrlValue MyURL = new SPFieldUrlValue();
MyURL.Description = "MSDN Microsoft";
MyURL.Url = "http://www.msdn.com";
Item["SP-HyperLinkColumn"] = MyURL;

Please leave your comments/feedback.
 
Share:

11 comments: