Using CSOM (JavaScript) how do you get the URL and description fields from a Hyperlink field?
-
Did you check the SPFieldUrlValue class? msdn.microsoft.com/en-us/library/ee552494(v=office.14).aspx– Vardhaman DeshpandeNov 17, 2012 at 18:43
-
Yes - there's no complete example on there of how to use it.– RyanNov 17, 2012 at 18:45
1 Answer
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());});
-
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?– PianoManJun 10, 2016 at 21:15