SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using CSOM (JavaScript) how do you get the URL and description fields from a Hyperlink field?

share|improve this question
    
Did you check the SPFieldUrlValue class? msdn.microsoft.com/en-us/library/ee552494(v=office.14).aspx – Vardhaman Deshpande Nov 17 '12 at 18:43
    
Yes - there's no complete example on there of how to use it. – Ryan Nov 17 '12 at 18:45
up vote 5 down vote accepted

Here is the working code:

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
    
I did this and it worked fine. But it only gets a single record without using a CAML query. I tried to to add the the .get_url() and get_description() to my code that loops through all the items while (enumerator.moveNext()) { var listItem = enumerator.get_current(); markup += '<td>' + listItem.get_item('Name') + '</td>'; markup += '<td>' + listItem.get_item('Level') + '</td>'; markup += '<td> + listItem.get_item("URL").get_description() + '</td>'; markup +='</tr>'; This failed. If I remove the .get_description() it returns [object, Object]. Ideas why how to fit? – PianoMan Jun 10 at 21:15

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.