0

I am using REST api to get data form a list. The only thing tripping me up is a hyperlink column containing a link to a word document in a library. The data is being returned like so:

https://sharepoint/site/DocLibrary/Document.docx,%20Project%20Definition

I need just the URL but rest is returning the display text also, noted by the comma and text following it on the end of the URL:

,%20Project%20Definition

I am trying to inject the URL into a anchor but the display text value is messing it up. I know I can parse the URL and remove the comma and following text but was hoping I wouldn't have to hard code it, especially since someone could put a comma in the document name.

2 Answers 2

0

Somewhat helpfully, the value returned will have a double comma in the place of every comma in the document name.

Thus a link to https://sharepoint/site/DocLibrary/Document, etc.docx with a description of Something becomes https://sharepoint/site/DocLibrary/Document,, etc.docx, Something. This means you can ignore all double commas and just do a substring based on the first single comma in the string.

It's ugly, but it can be consistently parsed, as demonstrated in the answer to this question: Parsing SharePoint's hyperlink format with JS

The link-parsing code from that answer:

function parseSPLink(SPlink)
{
    if(SPlink.indexOf(",,")==-1 || SPlink.indexOf(", ")<SPlink.indexOf(",,"))
    {
        return SPlink.split(",")[0];
    }
    //if there isn't a double comma in the string or the separator comes
    // before the first double comma, then we know that the first
    //comma must be the separator, so we just return the text before that 
    //Otherwise we loop until we find the right index
    else 
    {
        var ind = SPlink.indexOf(",,");
        var ind2 = ind;
        var temp = SPlink;
        while(temp.substring(ind+2).indexOf(",,")!=-1)
        {
            temp = temp.substring(ind+2);
            ind=temp.indexOf(",,");
            ind2 = ind2 + ind + 2;
        }    
        ind2 = ind2 + 2 + temp.substring(ind+2).indexOf(", ");
        return SPlink.substring(0,ind2).replace(/,,/g,",");
    }
};

I hope that helps!

2
  • Thanks! I'll try it out. I was also concerned about the user putting commas in the description. If worse comes to worse I'll just switch to a text column. They won't be using it to navigate to the document from the list. Mar 2, 2015 at 22:28
  • JavaScript's String.indexOf() method returns the first instance of the specified character, starting from the left, so you shouldn't have to worry about any commas in the description (you'll have already encountered the comma-space separator by then). Good luck!
    – Thriggle
    Mar 2, 2015 at 22:48
1

Hyperlink column in SharePoint REST has the following format (JSON):

{
   Description: "Description"
   Url: "Url"
}

The following JavaScript example demonstrates how to retrieve Hyperlink column:

var listTitle = '<list title>';
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var endpointUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
$.getJSON(endpointUrl, function(data) {

    data.value.forEach(function(item){
        var hyperlinkVal = item.Document; 
        console.log(hyperlinkVal.Url + ',' + hyperlinkVal.Description);

    });

});

Update

For SharePoint 2010:

var listTitle = 'MetaDocs';
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var endpointUrl = _spPageContextInfo.webAbsoluteUrl +  "/_vti_bin/listdata.svc/" + listTitle;
$.getJSON(endpointUrl, function(data) {

    data.d.results.forEach(function(item){
        var hyperlinkVal = item.Document.split(','); 
        var hyperlinkUrl = hyperlinkVal[0];
        var hyperlinkDesc = hyperlinkVal[1]; 
        console.log(hyperlinkUrl + ',' + hyperlinkDesc);

    });

});
6
  • In SharePoint 2010, the REST web services return XML instead of JSON. The result of accessing a hyperlink field looks like this: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Link xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">http://sharepoint/site/DocLibrary/Document.docx, Project Definition</Link>
    – Thriggle
    Mar 2, 2015 at 21:48
  • Thriggle - right, I did try the URL property but found out that only works in 2013. Mar 2, 2015 at 22:25
  • In SharePoint RESTful Web Services, you could specify data format (JSOM or XML). In the example I have provided is demonstrated how to read data in JSOM format and nothing more. So, your statement is incorrect Mar 2, 2015 at 22:28
  • See my updated answer with example for SP2010 Mar 2, 2015 at 22:50
  • Your updated answer is better :) That said, steveareeno was concerned about commas in the document name or description, which would cause the links to be parsed incorrectly in your example code.
    – Thriggle
    Mar 2, 2015 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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