Default view mode/edit mode behavior for Word and Excel Online files

Iron Contributor

I can't find the documentation for this, but I think it's a relatively new feature. I want my Word/Excel/PowerPoint online files to open in view mode instead of edit mode. When you open an Office Online file, the URL is like this [site collection]/_layouts/15/Doc.aspx?sourcedoc=[source location]&action=default&mobileredirect=true&DefaultItemOpen=1. The three paramtersare action, mobileredirect, and DefaultItemOpen. 

Does anyone know how to change the action parameter so that its value is "view" instead of "default"? Is this set at the tenant level, or site collection and/or library level?

21 Replies

I would like to be able to change this default view as well.

 

On the default site, any document library opens the document in Editing View, but, I noticed that on a new site created with Microsoft Teams, the default view when Word Online opens is Reading View.  

 

 

I want to do this as well. I have trolled through all the settings that I could think of with no success. Were you able to find out how to do it?

No, I haven't found out anything more.

Hi Bynum Hoekstra

I had a bit of a look around and found this blog post in which some of the responses give hope for a solution.

@Onno van Ewyk I've replied to this blog post with a solution for this issue. Please see my reply below:

 

Hi All,

 

You can achieve setting the default to 'View" mode by using SharePoint Column Formatting.

 

Simply update your column (in my case I updated Name to keep the experience seamless) with the following json and your documents will now open in Preview instead of Edit:

 

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "a",
"txtContent": "@currentField",
"attributes": {
"target": "_blank",
"href": "=if(indexOf([$ContentTypeId], '0x0120') >= 0, '?id=' + [$FileRef], @currentWeb + '/_layouts/15/Doc.aspx?sourcedoc='+ [$UniqueId] + '&action=View')"
}
}

 

What this does is checks if the item is a folder and uses existing behavior to open the folder otherwise it will open the document in view mode.

Hope this helps anybody looking for a way to solve this issue!

@AndrewFiveP 

 

Thank you for this snippet! It has almost resolved the issue for me, it does open the documents in read only mode, but it also breaks the folders so that when a user clicks on them it opens a new window stating an error has occurred. It seems like the part that is supposed to ignore folders is not working correctly, but I am not familiar with JSON so I cannot change the behavior

 

Is there something I need to change in the JSON snippet to make it ignore folders?

 

@meyvazi2345 

 

Whoops! Looks like you're right and this was too purpose built... I've updated the JSON in my post to reflect a wildcard search for the folder content type that works better in any situation.

Let me know if that fixes the issue for you!

@AndrewFiveP 

 

Hi Andrew! Thank you for replying! I just tested the updated JSON and it is still behaving the same way, the files open in view mode no problem, but the folders still open into an error page which I have attached. It states that the Object reference is not set to an instance of an object. Thank you again for replying and getting a solution out there for this! Please let me know if I can give any other information that would be helpful.

SharepointJSON.PNG

@meyvazi2345 Oh dear, Im not 100% sure what might be occurring here... Are you using this in SharePoint online? I did find that this solution will struggle with folders that have special characters that will cause the URL to break...

@AndrewFiveP I find your JSON snippet very useful. Folders are handled properly as well, but images and video's are not. I guess it should be possible to add other ContentTypeID so the script uses standard behaviour as well, but I don't know how to do this. Any hints? Thanks.

@Martijn Kessels Currently the script simply treats anything other than folders as office documents so it opens them up in office web apps. You can add additional conditions for each additional content type and set their behavior as needed. Unfortunately there isn't a switch function so you'll need to nest if branches =/

@AndrewFiveP we used your code snipped and it was working a treat. However, SharePoint must have changed something recently that threw a spanner in the works. It now returns a 500 error when you click the link. I tried it on different libraries and tenancies and same issue.

 

Look forward to hear from you.

 

 

@Aegir Brands You're correct. Looks like they've made some kind of change that is causing the '@currentWeb' reference to not load and it breaks the URL. You can instead directly call out the "preview" mode until they decide to fix this:

 

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "a",
"txtContent": "@currentField",
"attributes": {
"target": "_blank",
"href": "=if(indexOf([$ContentTypeId], '0x0120') >= 0, '?id=' + [$FileRef], [$ServerRedirectedEmbedUrl])"
}
}

 

 

 

 

 

 

 

@AndrewFivePthanks for the quick reply. I tried your adjusted code but I get a json format error when pasting it into SharePoint json format window. Cant quite see what is causing this though. 

@Aegir Brands The code blocks in this forum are wrapping my URL in the json in html <a> tags even though the code is marked javascript =(

 

I removed the code block and just posted raw text that doesn't include the <a> tags. That should work better.

You will need to protect the document to enable Viewing mode by default. Otherwise on Teams/Browser you can go to File>Info>Protect Workbook and there is a subtext: "To prevent accidental changes, this workbook has been set to open as read-only."

 

 

@AndrewFiveP  This is great!  Is it possible to apply this formatting to only one sub-folder of a SharePoint library?  It seemingly is applying to my entire library, but I'd prefer it to apply in only one specified place.

@mharris28 Yes, it is possible with some additional conditional logic added to the code to fit your specific needs. You'll need to follow the documentation found here to achieve that:
https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting