I'm trying to get the value of a field of type "Hyperlink or Image" (currently set to "Hyperlink" in the column settings that is being returned as part of a Sharepoint 2010 ListItem. I'm using the Client Object Model to do this, as I'm building a custom website that is remote to the server where Sharepoint is running. Unfortunately, what I've tried results in a NullReferenceException.
Here's some code to get you going:
ClientContext clientContext = new ClientContext("http://site");
List list = clientContext.Web.Lists.GetByTitle("SP List Name");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View/>"; // simplest query for now
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems,
Items => Items.Include(
item => item["Custom_x0020_URL_x0020_Field_x0020_"]
)
);
clientContext.ExecuteQuery();
string liveUrl = string.Empty;
foreach (ListItem listItem in listItems)
{
liveUrl = ((FieldUrlValue)(listItem["Custom_x0020_URL_x0020_Field_x0020_"])).Url.ToString();
}
The "liveUrl =" line has been modified a bunch, trying different ways of getting the value out, but none work. This is just the one that seems to make the most sense, despite still not working. This shouldn't be this hard, right? :P