Retrieves the URL associated with a given file name

The following function can be used for retrieving the URL associated with a given file name.

/**
 * Retrieves the URL associated with a given file name.
 * @param {string} fileName - The name of the file to retrieve the URL for.
 * @returns {string} The URL corresponding to the provided file name.
 */       
function getUrlFromFileName(fileName) {
            const fileFilter = search.createFilter({
                name: 'name',
                operator: search.Operator.IS,
                values: fileName
            });

            const fileSearch = search.create({
                type: 'FILE',
                columns: ['url'],
                filters: fileFilter
            });

            var scheme = 'https://';
            var fullUrl = scheme + url.resolveDomain({
                hostType: url.HostType.APPLICATION,
                accountId: runtime.accountId
            });
            fileSearch.run().each(function (result) {
                fullUrl += result.getValue({ name: 'url' });
            });
            return fullUrl;
        }

Leave a comment

Your email address will not be published. Required fields are marked *