locked
How do I add C# to webpage in SharePoint Designer? RRS feed

  • Question

  • I've looked and tried to figure it out but no luck! I keep getting a configuration error. Any help would be appreciated.

    Thanks!

    • Changed type Mike Walsh FIN Thursday, July 8, 2010 2:37 AM always use the question type so people get credit for their replies to you
    • Moved by Mike Walsh FIN Thursday, July 8, 2010 2:38 AM spd questions except for wf go to design/Customization (From:SharePoint - Development and Programming (pre-SharePoint 2010))
    Wednesday, July 7, 2010 6:26 PM

Answers

  • Hi,

     

    In web.config file, Find  “SafeMode” element.

    If you can see the child element “PageParserPaths”, add new child element of the element “PageParserPaths” as following:

             <PageParserPath VirtualPath="/*" CompilationMode="Always"

             AllowServerSideScript="true" IncludeSubFolders="true"/>

    If you can’t see the “PageParserPaths”, you need add new element as following:

          <PageParserPaths>

             <PageParserPath VirtualPath="/_mpg/*" CompilationMode="Always"

             AllowServerSideScript="true" IncludeSubFolders="true"/>

          </PageParserPaths>

     

    Hope this helps

     

    Thanks!

    Stanfford

    • Marked as answer by Stanfford Liu Friday, July 16, 2010 6:31 AM
    Thursday, July 8, 2010 7:38 AM
  • yes. you can only one element for SafeMode and PageParserPaths.

     

    • Marked as answer by Stanfford Liu Friday, July 16, 2010 6:31 AM
    Friday, July 9, 2010 2:58 AM

All replies

  • You can't add inline C# to a page using SharePoint Designer.  It's been specifically disallowed.  This is controlled by the PageParserPath setting in the web.config file and the default setting prohibits parsing code on any page served from the content database.  You can read more about it here:

    http://msdn.microsoft.com/en-us/library/bb862025(office.12).aspx

    So you can only add C# code to pages using SharePoint Designer if you modify the default PageParserpath setting.


    Paul Stork SharePoint Server MVP
    Wednesday, July 7, 2010 7:18 PM
  • So I have to modify the PageParserPath setting in web.config correct? I do not see PageParserPath in the web.config file.

    Thanks for the help.

    Wednesday, July 7, 2010 7:30 PM
  • Hi,

     

    In web.config file, Find  “SafeMode” element.

    If you can see the child element “PageParserPaths”, add new child element of the element “PageParserPaths” as following:

             <PageParserPath VirtualPath="/*" CompilationMode="Always"

             AllowServerSideScript="true" IncludeSubFolders="true"/>

    If you can’t see the “PageParserPaths”, you need add new element as following:

          <PageParserPaths>

             <PageParserPath VirtualPath="/_mpg/*" CompilationMode="Always"

             AllowServerSideScript="true" IncludeSubFolders="true"/>

          </PageParserPaths>

     

    Hope this helps

     

    Thanks!

    Stanfford

    • Marked as answer by Stanfford Liu Friday, July 16, 2010 6:31 AM
    Thursday, July 8, 2010 7:38 AM
  • Thanks for the reply Stanfford.  I had seen this code before while researching but I kept getting a
    "Parser Error Message:
    There are multiple root elements." message.  All that was in my web.config file were my connection strings under the configuration element. 

    Thanks for the help,

    Damia

    Thursday, July 8, 2010 4:51 PM
  • yes. you can only one element for SafeMode and PageParserPaths.

     

    • Marked as answer by Stanfford Liu Friday, July 16, 2010 6:31 AM
    Friday, July 9, 2010 2:58 AM
  • Hi Stanfford,

    Just a question over here, though as I understand from this conversation that modifying PageParserPath would be the only way to embedded c# code but, won't this be any performance issue ? as we are setting CompilationMode="Always" so in real will it be compiling the page each time it will be served ? (Am i right ?)

    So if this is the case won't it will make a process slow if we use a lot of code in so many pages ? Secondly is it good to follow the practice of embedding c# code in our pages ? OR should we avoid this method where applicable.

    (Sorry for so many question!)

    Friday, July 9, 2010 5:20 AM
  • HI Shaikh,

     

    I agree with what you have considered.

    Actually SharePoint didn't recommend to embed in-line code in content page, it represents a danger to the environment’s performance and security.

    SharePoint leave this option there in order to allow flexible development by the Web designer.

     

    Friday, July 9, 2010 6:54 AM
  • Thanks for clarification Liu.
    Friday, July 9, 2010 9:06 AM
  • Sorry I'm a little confused...I added the code is that not one element?

    Thanks

    Friday, July 9, 2010 2:01 PM
  • i know you add the code into the page.

    in order to you can run this code, you have to modify the web.config  as i said before.

    if the issue insists, please post the whole error message into forum?

     

    Saturday, July 10, 2010 3:52 AM
  • I did modify the web.config file the first time and it did not work.  I have solved the issue now though, thanks for the replies. 
    Monday, July 26, 2010 3:16 PM
  • Hello

     It is not good practice to add the code in aspx page and change web.config as follows

     <PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true"/>

    If you are using SharePoint you have to stick to SharePoint architecture if you want to have code within page best way is to use webpart because using code within aspx and changing web config is opening door to the possibility of executing sp methods by the users which could harm the site.It is also forcing asp .net custom code over Sharepoint structure. If you have no other way of having the functionality to work but to have it in asp .net at that time you can move the aspx page to layout at that time you can treat it as any custom asp .net page.

    Smiles

     


    Momo
    Tuesday, September 21, 2010 3:42 PM