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.
- AndrewFivePJul 09, 2019Copper Contributor
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
- 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 =/