Thursday, January 18, 2018

Enable possibility to download json files in Sharepoint

Sometime you may need to enable possibility to download json file types in Sharepoint (e.g. we had a case when it was needed for integration purposes – lobby screen at building reception used json file from Sharepoint site to show data to visitors). By default if you will put this file e.g. into /_layouts folder and try to access json file in context of Sharepoint site:

you will get the following error:

The page must have a <%@ webservice class="MyNamespace.MyClass" ... %> directive.
Stack trace:    at System.Web.UI.SimpleWebHandlerParser.ParseReader
   at System.Web.UI.SimpleWebHandlerParser.Parse(ICollection
   at System.Web.Compilation.SimpleHandlerBuildProvider.get_CodeCompilerTy
   at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider
(BuildProvider buildProvider)
   at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
   at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
   at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal
(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert
(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean
allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.UI.WebServiceParser.GetCompiledType(String inputFile,
HttpContext context)
   at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler
(HttpContext context, String verb, String url, String filePath)
   at System.Web.Script.Services.ScriptHandlerFactory.GetHandler
(HttpContext context, String requestType, String url, String pathTranslated)
   at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)

In order to enable possibility to download json file from Sharepoint we need to do the following:

1. Create new application sub folder under your Sharepoint site in IIS manager. If you already have virtual folder you need to convert it to application also in IIS manager. Separate folder is needed in order to not affect whole Sharepoint, but only one exact url:

2. Add web.config to this folder and add mime type record for json extension under system.webServer > staticContent:

   1: <configuration>
   2:    <system.webServer>
   3:       <staticContent>
   4:          <mimeMap fileExtension=".json" mimeType="application/json" />
   5:       </staticContent>
   6:    </system.webServer>
   7: </configuration>

3. In IIS manager open handler mappings for your application sub folder and remove handler for *.json (that's why it is important to use separate application sub folder in IIS – in case of app this change will only affect this sub folder, not whole Sharepoint site. I.e. there won’t be side effects with this approach).

Now if we try to access its content will be successfully downloaded.

No comments:

Post a Comment