开发者

Add general C# code to a SharePoint 2010 site page

I want to add a C# class to an existing SharePoint 2010 site page. The site page, being created either through the SharePoint UI or in SharePoint Designer, has a web part on开发者_如何学Python it that I want to access and edit its properties. I could just make an invisible web part in Visual Studio, deploy it on the page, and run the code I want to run that way, but I feel like that may be bad practice (or is it)?

I'm kind of talking about how a general non-SharePoint .aspx page can reference a C# file, kind of like below:

<%@ Page language="C#" Inherits="System.Web.UI.Page" CodeFile="Example.cs" %>

Would a line like above be added to the .aspx file? How is the .cs file actually deployed on the SharePoint site? Finally, is this even possible?

I'm new to SharePoint and to this site, so I appreciate all help!


By default, SharePoint does not support code behind files in regular pages for security reasons (although, as alfonso mentioned, inline code and code behind files are supported by application pages). You can override this setting in the web.config, but that is not considered to be a good practice.

You certainly could create another web part to do what you want. I have done something similar to that before. But typically, when I want to change the behavior of a custom page, I change the Inherits attribute of the Page directive:

<%@ Page 
    language="C#" 
    DynamicMasterPageFile="~masterurl/default.master"
    Inherits="MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" 
    %>

Create your class, have it inherit from System.Web.UI.Page, then deploy it to the GAC. Not only is this supported, but this is how most of the out of the box SharePoint pages do it.


I think there's no "correct" way to do this with a site page. The only way would be using webparts as you said, or user controls. However, you can add code very easily to an application page. This guy explains this very well.


You should have a look at the developer modules for SharePoint on MSDN

http://www.mssharepointdeveloper.com

There are various ways how to customize SharePoint 2010 as already mentioned earlier here you could use

  • WebParts
  • ApplicationPages or LayoutsPages
  • Custom MasterPages
  • DelegateControls

There are more ways how to extend SharePoint, so it really depends on your situation or on the requirement you've to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜