为sharepoint创建web部件

为sharepoint创建web部件,sharepoint,web-parts,Sharepoint,Web Parts,我看到了两种为sharepoint创建web部件的不同方法。哪一个最受欢迎 对于我编写的几个web部件,我想我更多地使用了方法2,而不是方法1。看起来更简单,并且有可能在SharePoint环境之外重新使用(取决于您的业务逻辑的深度)。任何涉及VSEWS的事情都会以痛苦告终,因此方法1肯定是过时的。方法2也不理想,因为将html元素设置为控件在演示中看到的级别上变得不可管理。我使用了一个相当简单的通用基类,它将用户控件作为类型参数,并使所有布局与sharepoint基础结构保持良好的分离。如果您

我看到了两种为sharepoint创建web部件的不同方法。哪一个最受欢迎


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

任何涉及VSEWS的事情都会以痛苦告终,因此方法1肯定是过时的。方法2也不理想,因为将html元素设置为控件在演示中看到的级别上变得不可管理。我使用了一个相当简单的通用基类,它将用户控件作为类型参数,并使所有布局与sharepoint基础结构保持良好的分离。如果您是以编程方式创建页面/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/";
        }
    }

}
公共抽象类UserControlWebPart:Microsoft.SharePoint.WebPartPages.WebPart其中T:UserControl
{
受保护的UserControlWebPart()
{
this.ExportMode=WebPartExportMode.All;
}
受保护的虚拟void TransferProperties(T ctrl)
{
var tc=类型(T);
var tt=this.GetType();
foreach(tt.GetProperties()中的var p){
如果(p.IsDefined(typeof(ControlPropertyAttribute),true)){
foreach(tc.GetProperties()中的变量p2){
if(p2.Name==p.Name){
p2.SetValue(ctrl,p.GetValue(this,null),null);
}
}
}
}
}
受保护的覆盖无效CreateChildControls()
{
字符串controlURL=ControlFolder+typeof(T).Name+“.ascx”;
var ctrl=Page.LoadControl(controlURL)作为T;
转让财产(ctrl);
this.Controls.Add(ctrl);
}
受保护的虚拟字符串控制文件夹
{
得到{
返回“~/_布局/UserControlWebParts/”;
}
}
}

我想我漏掉了第二条路。SharepointOverflow和Stackoverflow都没有多大帮助。以前真的很好。我遵循了这个链接,我想我对结果感到高兴。谢谢杰西的意见。我知道你使用第二种方法的意思。Web部件基于asp.net(2.0),因此它比sharepoint应用程序更有用。谢谢你的话。谢谢汤姆。这看起来更好,我真的很欣赏每种用法的内幕。干得好。再一次。谢谢。