简体   繁体   English

将常规C#代码添加到SharePoint 2010网站页面

[英]Add general C# code to a SharePoint 2010 site page

I want to add a C# class to an existing SharePoint 2010 site page. 我想将C#类添加到现有的SharePoint 2010网站页面。 The site page, being created either through the SharePoint UI or in SharePoint Designer, has a web part on it that I want to access and edit its properties. 通过SharePoint UI或SharePoint Designer创建的网站页面上有一个我要访问和编辑其属性的Web部件。 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)? 我可以在Visual Studio中创建一个不可见的Web部件,将其部署在页面上,并运行我想以这种方式运行的代码,但我觉得这可能是不好的做法(或者是它)?

I'm kind of talking about how a general non-SharePoint .aspx page can reference a C# file, kind of like below: 我在谈论一般的非SharePoint .aspx页面如何引用C#文件,如下所示:

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

Would a line like above be added to the .aspx file? 像上面这样的行会被添加到.aspx文件中吗? How is the .cs file actually deployed on the SharePoint site? .cs文件如何实际部署在SharePoint站点上? Finally, is this even possible? 最后,这甚至可能吗?

I'm new to SharePoint and to this site, so I appreciate all help! 我是SharePoint和这个网站的新手,所以我感谢所有的帮助!

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). 默认情况下,出于安全原因,SharePoint不支持常规页面中的文件后面的代码(尽管如alfonso所提到的,应用程序页面支持内联代码和代码隐藏文件)。 You can override this setting in the web.config, but that is not considered to be a good practice. 您可以在web.config中覆盖此设置,但这不是一个好习惯。

You certainly could create another web part to do what you want. 您当然可以创建另一个Web部件来执行您想要的操作。 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指令的Inherits属性:

<%@ 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. 创建您的类,让它继承自System.Web.UI.Page ,然后将其部署到GAC。 Not only is this supported, but this is how most of the out of the box SharePoint pages do it. 这不仅受到支持,而且大多数现成的SharePoint页面都是这样做的。

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. 唯一的方法是使用你所说的webparts或用户控件。 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 您应该查看MSDN上的SharePoint开发人员模块

http://www.mssharepointdeveloper.com http://www.mssharepointdeveloper.com

There are various ways how to customize SharePoint 2010 as already mentioned earlier here you could use 如前所述,您可以使用各种方法来自定义SharePoint 2010

  • WebParts 的WebParts
  • ApplicationPages or LayoutsPages ApplicationPages或LayoutsPages
  • Custom MasterPages 自定义MasterPages
  • DelegateControls DelegateControls

There are more ways how to extend SharePoint, so it really depends on your situation or on the requirement you've to achieve. 有多种方法可以扩展SharePoint,因此它实际上取决于您的情况或您要实现的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM