Executing Server-Side Code Share Point 2010

Another difference between site pages and application pages is the way they are parsed by the SharePoint platform. Application pages behave like any other ASP.NET page in that they can contain inline server-side code; however, site pages are rendered using the safe mode parser that prevents inline server-side code from executing. This makes sense when you think about it, because site pages are commonly user-generated, and allowing inline server-side code to execute would present a major system stability risk.If this were the whole story, SharePoint would be somewhat limited as a development platform. Of course, functionality can be implemented in the form of web parts, and although this is commonly the best approach, in some situations it’s vital to be able to handle the various events within the ASP.NET page rendering process—for example, when you want to change the master page dynamically.

Two solutions are possible to this problem: the first solution, which is recommended only in exceptional circumstances, is to add a PageParserPath entry to the web.config file, like this:

Be careful when you’re adopting this approach, because it effectively allows users to execute arbitrary code on the server. A better approach is to create a code-behind class and deploy it to the farm. You can then change the Page directive for any page to refer to the code-behind file, as shown:

  1. Using SharePoint Designer, create a new blank site
  2. From the Site tab, select Document Library | Document Library. Name the new library MyCustomPages.
  3. Using Visual Studio, choose File | New | Project. Then select Empty SharePoint Project from the New Project dialog, as shown.
  4. Executing Server-Side Code

  5. Set the site to use for debugging to the blank site that we set up in step 1. Select the Deploy As Farm Solution option.
  6. After the project has been created, choose Project | Add New Item. Add a new Module named MyCustomPages, as shown:
  7. Executing Server-Side Code

  8. At the time of writing, no template is available for creating site pages with codebehind, and when using a SharePoint project, standard ASP.NET pages are not available in the Add New Item dialog. To get around this limitation, we’ll add an application page and then modify it to suit our needs. Choose Project | Add New Item. Select Application Page and name the file MyCustomPage.aspx, as shown:
  9. Executing Server-Side Code

  10. Since application pages are always located in the Layouts folder, Visual Studio has automatically created a mapping to the SharePoint Layouts folder and added our new page to a subfolder named. Because we’re creating a site page rather than an application page, drag the MyCustomPage.aspx file into the MyCustomPages folder, and then delete the Layouts folder since it’s no longer required.
  11. In the MyCustomPage.aspx file, add the following markup:
  12. In the MyCustomPage.aspx.cs file, add the following code:
  13. Since we’re deploying our page to a document library, we need to make a few modifications to the Elements.xml file in the MyCustomPages folder. Change the XML as follows:
  14. We can now build and deploy the project to SharePoint. Choose Build | Deploy.
  15. Using the browser, navigate to MyCustomPages/MyCustomPage.aspx to see the results:
  16. Executing Server-Side Code

  17. Click the Change Title button to modify the title that’s displayed on the page, confirming that our code-behind works as expected.

If we select Edit Page from the Page tab, we’ll also be able to add web parts to our page. In fact, reviewing the options available on the Page tab, we can see that our custom page effectively behaves in the same way as any other site page, allowing us to set permissions or execute workflows.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Share Point 2010 Topics