0

I've got a jslink client-side rendering file for an edit form. It has a url column for which the basic structure of the item data is returned like:

"https://<link path>,<friendly display for link>"

And our code splits it on the comma to then manipulate and display the information. That was working until somebody uploaded a file with commas in its name. Those are encoded like this:

"https://<path>/This,, that,, and the other.xlsx, This, that, and the other.xlsx"

I'm trying to figure out how to break the information into the two parts. Does anybody know how SP does this internally?

2 Answers 2

1

Couldn't you just try to split on the comma first, and if you end up with more than two parts then indexOf('.xlsx,') + 5 would give you the index of the comma you're trying to split on, and you could slice using that info. Or string.match(regex) if you need to handle other file types and need to check for other extensions. You can get index information from the match.

1
  • That sounds complex and like it might have a lot of edge cases that are tricky to account for -- especially since it could be any value that a URL column allows, not just files. I found the answer which I'll post.
    – Rothrock
    Mar 20, 2019 at 15:28
0

I found that internally SP 2013 uses SPClientTemplates.Utility.ParseURLValue() to return an object of the form {URL:<value>,Description:<value>} from the value of a URL column while rendering an edit or display form. (I assume a list view too, but I didn't check that.)

I went poking in the _layouts/15/clienttemplates.debug.js and this is how they do it.

    var urlValue = {
        'URL': 'http://',
        'Description': ''
    };

    if (valueStr == null || valueStr == '')
        return urlValue;
    var idx = 0;

    while (idx < valueStr.length) {
        if (valueStr[idx] == ',') {
            if (idx == valueStr.length - 1) {
                valueStr = valueStr.substr(0, valueStr.length - 1);
                break;
            }
            else if (idx + 1 < valueStr.length && valueStr[idx + 1] == ' ') {
                break;
            }
            else {
                idx++;
            }
        }
        idx++;
    }
    if (idx < valueStr.length) {
        urlValue.URL = (valueStr.substr(0, idx)).replace(/\,\,/g, ',');
        var remainderLen = valueStr.length - (idx + 2);

        if (remainderLen > 0)
            urlValue.Description = valueStr.substr(idx + 2, remainderLen);
    }
    else {
        urlValue.URL = valueStr.replace(/\,\,/g, ',');
        urlValue.Description = valueStr.replace(/\,\,/g, ',');
    }
    return urlValue;

Your Answer

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

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