Monday, January 26, 2009

Code blocks are not allowed in this file

Scenario:
I was trying to put together a new UI with inline code. I designed it in Visual Studio as ASPX file and it worked without any issues. But when I moved the same page to SharePoint and accessed it, I got the following error

Error : An error occurred during the processing of /default.aspx. Code blocks are not allowed in this file.

Solution:
Short and simple , use code behind for all the custom pages, sharepoint doesn't allow code block ( ie. Eval and Bind functions ) for security reasons.

Its not like that you can never have a page with inline code, but its not recommended.

Also one more thing, if you really made the page work somehow with inline code, as soon as that page is touched by Designer, you are back to square with the same message. So dont try too hard on that.

Just-in-case you really want to make your inline code based page work.. you need to create a feature and use module tag in element.xml file to upload this file inspite of uploading it with browser or SharePoint Designer. Then it will not complain for code blocks.

:-)

7 comments:

Raghu January 27, 2009 at 2:20 AM  

Hi,

I am facing the following problem:
I created site pages through features and having an inline code in the site page. I added the pageparser entry in the web.config and it worked fine. I opened the site page in SP designer and made some changes. When i refresh the page in SP site i am again getting the same err "code block are not allowed in this file". Is there any issues with opening site pages in SP designer. Awaiting for your valuable reply.

Sandeep K Nahta January 27, 2009 at 11:21 AM  

As I explained, you can certainly make Page Parser entries in web.config to make it work temporarily but I will not recommend that. Also as I mentioned any page with Inline code in it , when modified by SharePoint Designer will bring you back to the same error. So move all your code to Code Behind, you will never get that problem again. I have some blog entries to show the same. Search for Code Behind on my blog.

Raghu January 28, 2009 at 2:40 AM  

Hi Sandeep,

Thanks for your valuable reply. I will try with code behind logic.

Anonymous,  August 20, 2009 at 4:59 AM  

can't we do like this?

http://mohammedfauzi.blogspot.com/2009/06/moss-code-blocks-are-not-allowed-in.html

Sandeep K Nahta August 20, 2009 at 8:10 AM  

This will work .. but its opening holes in security

digambar November 22, 2011 at 2:34 AM  

Hi sandip i dont have Web.config access what can I do. pls explain u r approch in brief

Sandeep K Nahta November 22, 2011 at 3:03 AM  

#1: You can deploy pages with code behind using Sandbox Solution

#2 : you can use Jquery + Web service to do what u want to do :)