为 sharepoint 创建 Web 部件

发布于 2024-09-28 07:50:01 字数 220 浏览 1 评论 0原文

我看到了两种不同的方法来为共享点创建 Web 部件。哪一款最受大家青睐?

http://msdn.microsoft.com/en-us /library/aa973249%28office.12%29.aspx

I saw 2 different way to create web parts for sharepoint. Which one is preferred by most?

http://msdn.microsoft.com/en-us/library/aa973249%28office.12%29.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-10-05 07:50:01

任何涉及 VSeWSS 的事情都会以痛苦告终,所以方法 1 肯定不适用。方法 2 也不理想,因为将 html 元素设置为控件变得难以管理,其程度超出了您在该演示中看到的程度。我使用一个相当简单的通用基类,它将用户控件作为类型参数,并让我将所有布局与共享点基础结构很好地分开。如果您以编程方式创建页面/Web 部件,则大多数 Web 部件 xml 也是可选的。

public abstract class UserControlWebPart<T> : Microsoft.SharePoint.WebPartPages.WebPart where T:UserControl
{
    protected UserControlWebPart()
    {
        this.ExportMode = WebPartExportMode.All;
    }

    protected virtual void TransferProperties(T ctrl)
    {
        var tc = typeof(T);
        var tt = this.GetType();

        foreach (var p in tt.GetProperties()) {
            if (p.IsDefined(typeof(ControlPropertyAttribute), true)) {
                foreach (var p2 in tc.GetProperties()) {
                    if (p2.Name == p.Name) {
                        p2.SetValue(ctrl, p.GetValue(this, null), null);
                    }
                }
            }
        }
   }


    protected override void CreateChildControls()
    {
        string controlURL = ControlFolder+typeof(T).Name+".ascx";
        var ctrl = Page.LoadControl(controlURL) as T;
        TransferProperties(ctrl);
        this.Controls.Add(ctrl);
    }

    protected virtual string ControlFolder
    {
        get {
            return "~/_layouts/UserControlWebParts/";
        }
    }

}

Anything involving VSeWSS is just going to end in pain, so method 1 is definitely out. Method 2 isn't ideal either, as setting up html elements as controls becomes unmanageable at a level just beyond what you see in that demo. I use a fairly simple generic base class that takes a user control as a type parameter and lets me keep all the layout nicely seperated from the sharepoint infrastructure. If you are creating pages/web parts programatically most of the web part xml turns out to be optional also.

public abstract class UserControlWebPart<T> : Microsoft.SharePoint.WebPartPages.WebPart where T:UserControl
{
    protected UserControlWebPart()
    {
        this.ExportMode = WebPartExportMode.All;
    }

    protected virtual void TransferProperties(T ctrl)
    {
        var tc = typeof(T);
        var tt = this.GetType();

        foreach (var p in tt.GetProperties()) {
            if (p.IsDefined(typeof(ControlPropertyAttribute), true)) {
                foreach (var p2 in tc.GetProperties()) {
                    if (p2.Name == p.Name) {
                        p2.SetValue(ctrl, p.GetValue(this, null), null);
                    }
                }
            }
        }
   }


    protected override void CreateChildControls()
    {
        string controlURL = ControlFolder+typeof(T).Name+".ascx";
        var ctrl = Page.LoadControl(controlURL) as T;
        TransferProperties(ctrl);
        this.Controls.Add(ctrl);
    }

    protected virtual string ControlFolder
    {
        get {
            return "~/_layouts/UserControlWebParts/";
        }
    }

}
耀眼的星火 2024-10-05 07:50:01

对于我编写的几个 Web 部件,我想我更多地使用方法 #2,而不是方法 #1。看起来更简单,并且有可能在 SharePoint 环境之外重用(取决于业务逻辑的深度)。

For the few web parts I've written, I guess I've gone more with method #2 than method #1. Seems more straightforward and has the potential to be reused outside of the SharePoint environment (depending on the depth of your business logic).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文