Join Now!
1) blah!bLah!BLOG! has a good post at: http://blah.winsmarts.com/2006/05/19/writing-custom-editors-for-sharepoint-2007-and-aspnet-20-webparts.aspx 2) Sahil Malik as another good post at: http://www.developer.com/net/asp/print.php/3627871 3) Ted's WSS book. If you are a developer and you don't yet have it, get Ted Pattison's book: Inside Microsoft Windows SharePoint Services 3.0. It will make your life easier.
public class SiteListViewer : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable{ private string _myEditorPartValue = ""; [Personalizable(true), WebBrowsable(false)] public string myEditorPartValue { get { return _myEditorPartValue; } set { _myEditorPartValue = value; } } protected override void Render(HtmlTextWriter writer) { writer.Write(myReturnedValue); } EditorPartCollection IWebEditable.CreateEditorParts() { List<EditorPart> editors = new List<EditorPart>(); editors.Add(new ListViewPicker()); return new EditorPartCollection(editors); } object IWebEditable.WebBrowsableObject { get { return this; } }}
1) The use of the IWebEditable interface. 2) There are two new functions: EditorPartCollection IWebEditable.CreateEditorParts() and object IWebEditable.WebBrowsableObject. These are what construct the EditorPart. 3) The CreateEditorParts funcion. "editors" is an array. This means that you can add as many EditorParts as you want to the webpart. My example uses one, but you can have as many as you need.
public class ListViewPicker : EditorPart{ private HtmlSelect _selSelectBox; private string _selectedValue = ""; [Personalizable(true), WebBrowsable(false)] public string selectedValue { get { return _selectedValue; } set { _selectedValue = value; } } public ListViewPicker() { this.ID = "ViewPicker"; this.Title = "I have a title!"; } protected override void CreateChildControls() { _selSelectBox = new HtmlSelect(); _selSelectBox.ID = "_selSelectBox"; Controls.Add(_selSelectBox); } protected override void Render(HtmlTextWriter writer) { writer.Write("Label:.<BR>"); _selSelectBox.RenderControl(writer); } public override bool ApplyChanges() { EnsureChildControls(); SiteListViewer _part = WebPartToEdit as SiteListViewer; _part.myEditorPartValue = _selSelectBox.Items[_selSelectBox.SelectedIndex].Value.ToString(); return true; } public override void SyncChanges() { SiteListViewer _part = WebPartToEdit as SiteListViewer; EnsureChildControls(); selectedValue = _part.myEditorPartValue; //Call Function(s) to populate the _selSelectBox control here }}
1) This class is not a Webpart, but rather an EditorPart. 2) There is a constructor in this class. In it be sure to set the ID. I haven't tested the theory, but blogs posts have said that while this is not required in ASP.Net pages, it is in SharePoint. But it's also good form. So do it. Yeah. Do it or I'll tell on you. 3) Render doesn't have to be overrided here. But if you don't override Render, your controls will appear with no formatting in the order they were added to the Controls array. Not pretty. 4) ApplyChanges. This is one of two essential functions in the class. ApplyChanges tells the host webpart what value is being passed back to it. It is called when a user clicks OK or Apply in the Editor Pane. Note the line: SiteListViewer _part = WebPartToEdit as SiteListViewer; Here, you create an reference to your host webpart. WebPartToEdit comes with the EditorPart and gives you the reference to the host. But you must cast it as the classname of the host. This is what threw me off at first. Here, it's a SiteListViewer as that is the name of my host webpart. Once you have this, you can access the public properties of the host part and thus pass values. There is no Update command as the SPWebPartManager takes care of that. Also note the "return true;" line. You can establish logic here to validate values. By returning false, you keep the OK from confirming the data. It will display a generic message. I haven't worked out how to pass specific error messages yet. 5) SyncChanges. This is the function that takes the value stored in the host and passes it back to the webpart when it is created. This is also where you will need to put in any code that will populate your control. This is where I put the code call to dynamically fill the dropdown with the SubSite list.
Posted on 10/11/2007 at 10:15 AM Permalink | Share This Post | Comments (1) | Leave a Comment
Blog Tags:
All Blogs
BDC Code Community Features General Guides InfoPath Language Variations MOSS Search SharePoint Site Variations SQL Tools Variations Variations Editor Web Parts Workflow WSS