Create a Public Link (shared link) in Javascript

I’m making an app to view office documents using Microsoft Office Online API, however, I wasn’t able to create a public link for the file using Javascript.


var downloadUrl = context.fileList.getDownloadUrl(fileName);

This is a local link to the logged in user, Microsoft Online Viewer can’t read it, can some one help me with the link creating process in java-script?

OCA.MSOfficeViewer.FileClick = function (fileName, context) {
	    //downloadUrl is not a direct/public link to the file, thus it'll not be readable by microsoft viewer, 
	    //we need to replace by a public link
    var downloadUrl = context.fileList.getDownloadUrl(fileName);
    var currentUrl = encodeURI(context.fileList.linkTo() + '?path=' + context.dir);
	    //need to get nextcloud URL dynamically instead of hardcoding in the app
    var onedrive = "";
		var linkElement = onedrive + baseUrl + downloadUrl;

If someone manages to help with this, they’ll be credited when the app is released.


There is a difference between the download link and a public share link. The download link requires the user to provide credentials, so your approach is not going to work as the Office online viewer cannot access the file. While you could create a public link, this is probably not the best approach.

My suggested solution would be to have a dedicated endpoint in your app that provides a onetime link to download the file which is then handed over to the external service. This link would then be identified by a unique token, similar to a public share link. This has the benefit, that the file is not always shared as a public link and opening documents doesn’t lead to a long list of links in the user interface.