5

I know how to retrieve items like Title etc. I can query the columns and fetch data. But it doesnt show the hyperlinks.

Appreciate if someone can help me out!

getListItem.get_item('Link').get_url();
7

Using JavaScript Object Model, if you need to retrieve value and link of a column of type Hyperlink then following methods are used.

  1. get_description(): It will fetch the description of the link you have given
  2. get_url(): It will fetch the URL.

Note: The method which you have used is correct, in case you do not get values in it, then you must check the list items.

Refer below example:

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());});
  • Why did you add .getItemById(1);? and why is it needed? – Sunreuz Sep 28 '15 at 18:04
  • @Sunreuz : Its not needed. Its just an example, which directly gets a list item whose ID is 1. You can also get all items loop them and get URL. See this another example social.msdn.microsoft.com/Forums/en-US/… which is slightly different. Here he first gets Item in variable, then on that calls get_url() function. – Asad Refai Sep 29 '15 at 4:59
  • It works now. But the problem is its not clickable link. Displaying only the URL. So how can I get as a clickable link for every item that are fetched from the list? – Sunreuz Sep 29 '15 at 6:30
  • Are you generating any type of html out of the result you get? If so then you can place this link in href attribute of anchor tag. – Asad Refai Sep 29 '15 at 6:34
  • The column (hyperlink) has some items example www.google.com or www.youtube.com. When I retreive the items and present on my site it should be clickable. I put this code but then it made all the items to 1 single link. listItemInfo += "<li style='height: 20px;'>" + oListItem.get_item('Title') + " " + oListItem.get_item('Link').get_url() + "</li>"; document.getElementById("message").innerHTML += "<a href=" + listItemInfo + "</a>"; – Sunreuz Sep 29 '15 at 12:02
2
string strurl = ((Microsoft.SharePoint.Client.FieldUrlValue)(item["destinationURL"])).Url;

Using CSOM, Convert the item to a FieldURLValue and then get the URL from that object

  • Would you please explain your code. Please provide brief explanations on what was the problem and how your code will solve it. This will be very helpful for other visitors – Asad Refai Sep 28 '15 at 8:08
  • @Asad Refai, I just gave him the answer using CSOM. Convert the item to a FieldURLValue and then get the URL from that object. – Shishirchandra Prasad Sep 28 '15 at 8:13
  • Can you please update your answer, with this explanation please – Asad Refai Sep 28 '15 at 8:14
  • @AsadRefai, Thanks buddy! I am newbie. Will update the answer. – Shishirchandra Prasad Sep 28 '15 at 8:16
  • Everyone at some point are newbies, you can read this help at sharepoint.stackexchange.com/help/answering and get idea about working of SP.SE. Continue your good contribution to the site. :) – Asad Refai Sep 28 '15 at 8:27
0

SharePoint List's Hyperlink Column have two values

  1. Url: To get value of this url use - ListItem.get_item('HyperlinkColumnName').get_url()
  2. Description: To get value of this use - ListItem.get_item('HyperlinkColumnName').get_description()
  • It doesnt work I have tested it. I get this error "it is not possible to retrieve the property get_url for a reference that is undefined or null". In sharepoint list I have a column name "Link". I added two different URL:s. – Sunreuz Sep 28 '15 at 18:03
  • Never mind now I can fetch the hyperlinks from my column. But the thing is its not clickable. You know how to make it clickable? – Sunreuz Sep 28 '15 at 19:05
  • @Sunreuz: To make link clickable you need to make description as hyperlink using JavaScript and add this element into section. Example - <a href='+ListItem.get_item('HyperlinkColumnName').get_url()+'> ListItem.get_item('HyperlinkColumnName').get_description() </a> – Abhijeet Bhor Sep 29 '15 at 17:25
  • I get some error message but is it like this? while (enumerator.moveNext()) { var listItem = enumerator.get_current(); itemString += listItem.get_item('Title') + "<br/>" + "<a href=" + ListItem.get_item('Link').get_url() +'> ListItem.get_item('Link').get_description() + "</a>"; $('#divAllaListItems').html(itemString); } – Sunreuz Sep 29 '15 at 17:33
  • Thank you I figured it out with some help. Now it works. – Sunreuz Sep 29 '15 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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