Question

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?

Was it helpful?

Solution 2

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;

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top