Forum Discussion
Default view mode/edit mode behavior for Word and Excel Online files
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!
- mharris28Sep 22, 2020Copper Contributor
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.
- AndrewFivePSep 22, 2020Copper Contributor
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
- Aegir BrandsJan 20, 2020Copper Contributor
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.
- AndrewFivePJan 20, 2020Copper Contributor
Aegir Brands There is a bug report for this issue here: https://github.com/SharePoint/sp-dev-docs/issues/5174
- AndrewFivePJan 20, 2020Copper Contributor
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])"
}
}- Aegir BrandsJan 20, 2020Copper Contributor
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.
- Martijn KesselsAug 09, 2019Brass Contributor
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.
- AndrewFivePAug 11, 2019Copper Contributor
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 =/
- meyvazi2345Jul 23, 2019Copper Contributor
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?
- AndrewFivePJul 23, 2019Copper Contributor
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!- meyvazi2345Jul 23, 2019Copper Contributor
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.