OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
670 stars 96 forks source link

Office.context.document.url does not get updated when a document is renamed #1507

Closed ig-jb closed 6 months ago

ig-jb commented 3 years ago

Message from office-js bot: We’re closing this issue because it has been inactive for a long time. We’re doing this to keep the issues list manageable and useful for everyone. If this issue is still relevant for you, please create a new issue. Thank you for your understanding and continued feedback.

Currently, I believe the only way to get access to the document name is to parse Office.context.document.url. This value gets updated after document reload (i.e. close and reopen) if the document is renamed; however the old value remains accessible after rename unless the user performs the reload.

Expected Behavior

Office.context.document.url references the latest value for document url after renaming the document

Current Behavior

The old name of the document remains present in Office.context.document.url

Steps to Reproduce, or Live Example

  1. Check the value of Office.context.document.url
  2. Rename the document in Word or Excel.
  3. Check the value of Office.context.document.url. You can see it is the same as in step 1
  4. Close the document and reopen it.
  5. Check the value of Office.context.document.url. You can see it now shows the new name of the document.

Context

We are replacing a feature from a VSTO addin which allowed the user to save Excel or Word documents to our web application. This limitation means the old name gets used when uploading the document; which will lead to user confusion.

Your Environment

MandytMSFT commented 3 years ago

@ig-jb Currently, we don't have a direct way to get the file name. As an workaround, Could you try getFilePropertiesAsync like below to see if it can meet your requirement ? For Office.context.document.url didn't get updated, i log a BUG 4655764 for internal track

Office.context.document.getFilePropertiesAsync(function (asyncResult) { var fileUrl = asyncResult.value.url; console.log(fileUrl); });

Besides, You can add a request on our UserVoice page if you think providing a direct API to get document name is more helpful.

dsebastien commented 3 years ago

Being able to get the document's name is very useful for scenarios where we want to create an add-in uploading specific file to third-party systems.

dgrayling commented 3 years ago

Mandy's solution is correct, code snippet for converting the callback to a promise for use in async:

const promise = new Promise((resolve, reject) => { Office.context.document.getFilePropertiesAsync(function (asyncResult) { const fileUrl = asyncResult.value.url; if (!fileUrl || fileUrl === "") { reject('no url') } else { resolve(fileUrl); } }) })

Note also that this api can return file paths so you need to check for that.