自定义属性编辑器在SharePoint Web部件 [英] Custom property editor for Web parts in SharePoint

查看:228
本文介绍了自定义属性编辑器在SharePoint Web部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了具有一定的配置属性自定义的WebPart。这些属性的值是一个网站的URL和列表名称。我想显示一个下拉列表中的所有站点名称,并列出为所选网站。我怎样才能显示在SharePoint属性的自定义编辑器组件?我不希望得到默认的文本编辑器。

I have created a custom WebPart that has some configuration properties. The values for these properties are a Site URL and list name. I want to show a drop down list with all site names and lists for the selected sites. How can I get to show a custom editor component for a property in SharePoint? I don't want to get the default text editor.

推荐答案

您将要创建一个自定义ToolPart。这里是一个指南:的http:// SharePoint的insight.blogspot.com/2008/10/sharepoint-creating-web-part-with.html

You will want to create a custom ToolPart. Here is a guide: http://sharepoint-insight.blogspot.com/2008/10/sharepoint-creating-web-part-with.html

基本上在你的WebPart代码,你将需要重写在GetToolParts功能,然后返回一个 ToolPart []

Basically in your WebPart code you will need to override the GetToolParts function and return a ToolPart[]

更改toolpart构造函数接受的SPWeb 对象(通过它从你的Web部件的 SPContext.Current.Web )对象。要获得列出的名单,在你toolpart,您将需要创建的CreateChildControls()方法中一个DropDownList。 。使用你从构造了可以使用每个获得当前网站的所有列出的SPWeb对象

Change your toolpart constructor to accept an SPWeb object (pass it the SPContext.Current.Web object from your Web Part). To get the list of lists, in your toolpart you will need to create a dropdownlist inside your CreateChildControls() method. Using the SPWeb object you got from the Constructor you can use a for each to get all the lists for the current site.

因此,在您的Web部件做到这一点:

So in your web part do this:

public override ToolPart[] GetToolParts()
{
    ToolPart[] tps = new ToolPart[3];

    WebPartToolPart wptp = new WebPartToolPart();
    CustomPropertyToolPart cptp = new CustomPropertyToolPart();
    tps(0) = cptp;
    tps(1) = wptp;
    tps(2) = new ListSelectionToolPart(SPContext.Current.Web, "List Settings");

    return tps;
}

private string _TargetListGUID;
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(false)]
[WebDisplayName("Target List GUID")]
[WebDescription("GUID of the Target List")]
[SPWebCategoryName("Internal")]
public string TargetListGUID {
  get { return _TargetListGUID; }
  set { _TargetListGUID = value; }
}

添加类是这样的:

public class ListSelectionToolPart : WebPartPages.ToolPart
{
  private SPWeb _web;
  protected DropDownList ddlLists;

  public New(SPWeb Web, string ToolTitle)
  {
    _web = System.Web;
    this.Title = ToolTitle;
  }

  protected override void CreateChildControls()
  {
        Literal litLists = new Literal { Text = "<b>List:</b><br />" };
        ddlLists = new DropDownList {
        AutoPostBack = true,
        ID = "ddlLists"
        };
        ddlLists.Style.Add("width", "100%");
        foreach (SPList list in _web.Lists)
        {
         ddlLists.Items.Add(new ListItem(list.Title, list.ID.ToString()));
        }
        this.Controls.Add(litLists);
        this.Controls.Add(ddlLists);
  }

  public override void ApplyChanges()
  {
    (this.ParentToolPane.SelectedWebPart as MyWebPart).TargetListGUID = ddlLists.SelectedValue;
  }

}



上面的代码假定的名称你的Web部件是MyWebPart,并且有一个TargetListGUID属性。要添加一个网站选择,你可以做相当多的toolpart同样的事情(添加另一个DropDownList的)。如果使用SelectionChanged事件就可以了,你可以用它来填充列表下拉。

The above code assumes the name of your WebPart is MyWebPart and that there is a TargetListGUID property. To add a site selection you can do pretty much the same thing in the toolpart (add another dropdownlist). If you use the SelectionChanged event on it you can use it to populate the lists dropdown.

这篇关于自定义属性编辑器在SharePoint Web部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆