Open Word Document in Client Application

Good Morning,

I have a List or Library Control on a Form that links to a Document Library containing word documents, due to filtering there will only ever be one Word Document returned in the Control. The issue I have is that clicking the item opens it in Word Online which does not give our users required functionality. I know that within Sharepoint Admin settings you can set all items to open in client applications this does not work and we have raised this with Microsoft who are aware.
We had this in Forms Designer on related items controls and I used the code below to fix the issue:

$('.ms-vb2 a').click(openQuoteDocument);

// Function to open Quote Document in Word Application
function openQuoteDocument () {
var documentURL = $(this).attr('href');
documentURL = 'ms-word:ofe|u|'+documentURL;
window.location.href = documentURL;
}

I would like to use this same functionality again but its the on ‘.ms-vb2 a’ I need help with I have tried inspecting the table element and cant see what class I would need to use in its place?

Thanks

Dear Tony,

Please try this selector:

$(fd.control("SPDataTable0").$el).find("a").not(".k-link").not(".k-button")

Hi Alex,

Doesn’t seem to work, am I using it correctly?

$(fd.control("SPDataTable0").$el).find("a").not(".k-link").not(".k-button").click(openQuoteDocument);

// Function to open Quote Document in Word Application
function openQuoteDocument () {
	var documentURL = $(this).attr('href');
	documentURL = 'ms-word:ofe|u|'+documentURL;
	window.location.href = documentURL;
}

Thanks

Dear Tony,

Please try the following code:

function openQuoteDocument () {
	var documentURL = $(this).attr('href');
	documentURL = 'ms-word:ofe|u|'+documentURL;
	window.location.href = documentURL;
    return false;
}

After that, please provide me errors you’ve received. Notice that you should click the document manually.

Hi Alex,

Tried that, still the same, I am clicking the document manually a new window opens and the document opens in Word Online.

I tried to debug the function and it is never called on click of document so I think the issue lies in the selector for the .click as opposed to the function itself…

$(fd.control("SPDataTable0").$el).find("a").not(".k-link").not(".k-button").click(openQuoteDocument);

Thanks again…

Dear Tony,

Could you provide temporary access to the form in the private message so I can research your current issue and others more closely?

You can either share a site with an external user (azverev@180821.onmicrosoft.com): https://docs.microsoft.com/en-us/sharepoint/external-sharing-overview

Or provide credentials of one of the existing users - then you can change the password.

Hi Alex,

I have sent you an invite to Share the site…

Thanks again.

Dear Tony,

Thank you, I’ve received the invitation.

Dear Tony,

Please check this code:

fd.spRendered(function() {
	$.urlParam = function(name,url){
		var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(url);
		return results[1] || 0;
	}
	
	fd.control("SPDataTable1").ready().then(function(dt) {
		$(fd.control("SPDataTable1").$el).find("a").not(".k-link").not(".k-button").click(openQuoteDocument);	
	});
	
	
	function openQuoteDocument () {
		var docUrl = $(this).attr('href');
		var docName = $.urlParam("file",docUrl);
		var docLibraryUrl = "https://180821.sharepoint.com/sites/third/Shared%20Documents/";
		docURL = 'ms-word:ofe|u|'+docLibraryUrl+docName;
		window.location.href = docURL;
    	return false;
	} 
})

Notice that you should:

  1. Replace “SPDataTable1” with the corresponding internal name of your List/Library control.
  2. Replace “docLibraryUrl” with the corresponding URL of your Library.

Hi Alex,

Got that working great; the next challenge is if I use the code to filter the control it stops this function working is there a way of integrating the two functions below: -

fd.spRendered(function() {

    $.urlParam = function(name,url){
	var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(url);
	return results[1] || 0;
	}

    var dt2 = fd.control('SPDataTable2');
	if (dt2.widget) {   
        filterDT2();
    } else {
        dt2.$on('ready', function() {
            filterDT2();
        });
    }
    function filterDT2(){
        dt2.filter = "<Eq><FieldRef Name='QuotationReference'/><Value Type='Text'>" + quoteReference + "</Value></Eq>";
    }

    fd.control("SPDataTable2").ready().then(function(dt) {
    	$(fd.control("SPDataTable2").$el).find("a").not(".k-link").not(".k-button").click(openQuoteDocument);	
    });

    // Function to open Quote Document in Word Application
    function openQuoteDocument () {
	    debugger;
	    var docUrl = $(this).attr('href');
	    var docName = $.urlParam("file",docUrl);
	    var docLibraryUrl = "https://**********.sharepoint.com/ERP/Quotation%20Repository/";
	    docURL = 'ms-word:ofe|u|'+docLibraryUrl+docName;
	    window.location.href = docURL;
	return false;
    }
}); 

Any help very much appreciated.

Thanks

Dear Tony,

I guess it’s happening because of the filter updates the document link after we set the custom click handler, so the solution is quite simple, just try to add a timeout to set a click handler after the filter has finished its work:

setTimeout(function() {$(fd.control("SPDataTable2").$el).find("a").not(".k-link").not(".k-button").click(openQuoteDocument);},3000);

I’ve set “3000” by default, but you can adjust this setting to fit your form.

Works Great!!

Thanks again Alex

Any way this can be implemented for all document types? I have the same issue, but there are also Excel and Powerpoint files in my Library control.

Hello @abolam!

Currently, this can be done only with custom code. The example of the code for Word documents is above.

If you are interested in this feature and don’t want to deal with the code, our paid support team can add an option to open all documents in Client Application in Plumsail Forms.

Please send a message to support@plumsail.com to get an estimation.

Hi, thankyou mnikitina

I made my own custom solution, however it appears to be intermittent. It worked yesterday, today all my documents are opening in the browser! I can see that Microsoft have made some changes to their online apps recently could this be something they have changed? I'm using Office 365 SharePoint.

Regards
Andy

Hello @abolam!

Unfortunately, yes. Most likely the recent Microsoft updates in SharePoint Online could have broken your custom solution.