Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

Working solution from StackEchange link

var context = SP.ClientContext.get_current();
var myListItem = context.get_web().get_lists().getByTitle("ListName").getItemById(1);
context.load(myListItem);

context.executeQueryAsync(function(){

alert(myListItem.get_item("HyperLinkFieldName").get_description());
alert(myListItem.get_item("HyperLinkFieldName").get_url());

},function(sender,args){alert(args.get_message());});
share|improve this answer
add comment

try:

    liveUrl = new SPFieldUrlValue(listItem["Custom_x0020_URL_x0020_Field_x0020_"].ToString()).Url.ToString();
share|improve this answer
add comment

You could try using SPServices instead.

$().SPServices({
operation: "GetListItems",
async: false,
listName: "SP List Name",
WebURL: "List URL", //not needed if list is on the same site level
CAMLViewFields: "<ViewFields><FieldRef Name='Custom_x0020_URL_x0020_Field_x0020_' /></ViewFields>",
completefunc: function (xData, Status) {

//alert(xData.responseText);  //see if it returns you a value

  $(xData.responseXML).SPFilterNode("z:row").each(function() {

                  var xitem = $(this);
                  var url = xitem.attr('ows_Custom_x0020_URL_x0020_Field_x0020_');

                  alert(url);
       });
    }
});
share|improve this answer
add comment
liveUrl = 
  ((FieldUrlValue)(listItem["Custom_x0020_URL_x0020_Field_x0020_"])).Url.ToString();

... should work.

Have you tried actually instantiating a new FieldUrlValue object rather than casting? I know you can cast a FieldLookupValue in that manner, but the FieldUrlValue appears to require instantiation (I don't know if that's necessarily accurate)???

Also, see Adding description to sharepoint link URL using the C# CSOM

He had to instantiate rather than cast.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.