IT tutorials
 
Applications Server
 

SharePoint 2010 : Writing a WebPart (part 3) - Writing the OPML WebPart and a WebPart Editor

5/8/2013 7:37:07 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2. Writing the OPML WebPart and a WebPart Editor

You just wrote the RSSWebPart. The RSS WebPart required some runtime configuration, specifically you needed to specify an RSS URL to the RSSWebPart. The WebBrowsable(true) attribute provided you a convenient way to allow SharePoint to give you an Editing UI for that property.

Next, you are going to write the OPML WebPart. The OPML WebPart will host a number of RSS URLs and will present the user with the RSS URLs laid out with radio buttons next to them. The idea is that the user can select any of the RSS URLs. In the next section, I will connect the two WebParts so the selected RSSURL in the OPML editor is rendered as an RSS feed in the RSSWebPart.

While it was convenient to edit one single RSS feed URL using a simple text box, editing the OPML WebPart's numerous RSS Feeds perhaps requires a different UI. To keep things simple instead of creating a single line text box, I will instead create a multiline text box allowing the user to enter multiple RSS URLs as comma separated values. This multiline text box would be the custom editor for the OPML WebPart.

In this section, you look at the process of writing yet another WebPart and connecting it to its custom editor. Let's get started.

Back in your Visual Studio project, add another WebPart and call it the OPMLWebPart. In the OPMLWebPart.cs file, write the code shown in Listing 7.

Example 7. The OPMLWebPart Code without the Editor
[ToolboxItemAttribute(false)]
public class OPMLWebPart : WebPart
{
    private RadioButtonList rbl;

    [Personalizable(PersonalizationScope.Shared)]
    public List<String> FeedURLS { get; set; }

    public OPMLWebPart()
    {
      FeedURLS = new List<string>();
    }

    protected override void CreateChildControls()
    {
      rbl = new RadioButtonList() { AutoPostBack=true };

      foreach (string rssFeed in FeedURLS)
      {
        rbl.Items.Add(new ListItem(rssFeed));
      }

      this.Controls.Add(rbl);
    }
}

If you look at the code in Listing 7, note that you've created a simple WebPart with the public property called FeedURLs. What you haven't done though is provide the ability for the user to edit the value of FeedURLs. The WebBrowsable(true) attribute is not going to work here for two reasons.

First, the single line text box is not a good UI to enter multiple RSS Feed URLs, and secondly, List<String> is a complex object. It is not a simple string, or an enum, so the framework won't be able to intelligently serialize or deserialize it. In order to edit the FeedURLs, you have to write a custom editor.

2.1. Writing a Custom Editor

Writing a custom editor for a WebPart is rather simple and involves two steps. First, your WebPart has to implement the IWebEditable interface, and second you have to implement a class that inherits from the EditorPart abstract base class. This inherited class will be the WebPart editor.

Go ahead and implement the IWebEditable the interface in the OPMLWebPart. Implementing the IWebEditable interface would require you to make the change shown in Listing 8 to your OPMLWebPart.

Example 8. IWebEditable Implementation
#region IWebEditable Members

EditorPartCollection IWebEditable.CreateEditorParts()
{
  List<EditorPart> editors = new List<EditorPart>();
  editors.Add(new OPMLEditor());
  return new EditorPartCollection(editors);
}

object IWebEditable.WebBrowsableObject
{
  get { return this; }
}

#endregion

As you can see from Listing 8, the CreateEditorParts method returns you an EditorPartCollection. Therefore, numerous editors can work on one single WebPart at the same time. This makes sense considering that there may be numerous properties on the WebPart and each of them may acquire a different editor. The second property simply returns an object, which is an instance of what is being edited. In this case, the WebPart is being edited so you simply return this.

In the CreateEditorParts method, note that you are adding an instance of a new class called as the OPMLEditor. This is the actual editor which inherits from the EditorPart abstract base class. Therefore, add a new class in your project and call it OPMLEditor.cs. The code for OPMLeditor.cs can be seen in Listing 9.

Example 9. OPMLEditor Class
public class OPMLEditor : EditorPart
{
  private TextBox opmlCSV;

  public OPMLEditor()
  {
    this.ID = "MyEditorPart";
  }

  protected override void CreateChildControls()
  {
    opmlCSV = new TextBox()
    {
      TextMode = TextBoxMode.MultiLine,
      Width = new Unit("300px"),
      Height = new Unit("100px")
    };
    Controls.Add(opmlCSV);
  }

public override bool ApplyChanges()
  {
    EnsureChildControls();
    OPMLWebPart part = WebPartToEdit as OPMLWebPart;
    if (part != null)
    {
      List<String> feeds = new List<String>();
      string[] rssfeeds = opmlCSV.Text.Split(',');
      foreach (string rssfeed in rssfeeds)
      {
        feeds.Add(rssfeed.Trim());
      }
      part.FeedURLS = feeds;
    }
    else
    {
      return false;
    }
    return true;
  }

  public override void SyncChanges()
  {
    EnsureChildControls();
    OPMLWebPart part = WebPartToEdit as OPMLWebPart;
    if (part != null)
    {
      StringBuilder sb = new StringBuilder();
      foreach (String rssFeed in part.FeedURLS)
      {
        sb.Append(rssFeed);
        sb.Append(",");
      }
      if (part.FeedURLS.Count != 0)
        sb.Remove(sb.Length - 1, 1);

      opmlCSV.Text = sb.ToString();
    }
  }
}

					  

As you can see, the EditorPart abstract base class requires you to implement two methods.

The ApplyChanges method takes the data entered on the EditorPart and applies it to the WebPart. The SyncChanges method goes in the reverse direction. When the WebPart is put in edit mode, the SyncChanges method reads the data from the WebPart, and applies it to the editing user interface. The editing user interface can be seen in the CreateChildControls method of the OPML editor.

At this point, you can also edit the feature receiver to add an instance of the OPMLWebPart to the WebPartPage.aspx that you had created earlier. Since the steps for that are pretty much the same as what I've already shown for RSSWebPart, I'm going to skip describing them. If you prefer, you can also configure the WebPart at runtime right through the browser.

With the code finished, go ahead and build then deploy the project. Visit the site in your browser and drop an instance of the OPMLWebPart in a WebPartZone. Edit the WebPart and you should you be able to see a multiline text box that would allow you to enter multiple RSS Feed URLs as comma seperated values. This can be seen in the Figure 12.

Figure 12. Editing the OPMLWebPart using a custom editor

So far so good since the page is coming along well. You now have two WebParts, one that shows the RSS feed and another that lets you host a number of RSS feed URLs and allows you to pick one of them. Wouldn't it be nice if you picked the RSS feed URL from the OPML WebPart and that feed was rendered in the RSSWebPart on the right?

What you will do next is to make the necessary code changes so these two WebParts can talk with each other.

 
Others
 
- SharePoint 2010 : Writing a WebPart (part 2) - Configuring the WebPart During Deployment
- SharePoint 2010 : Writing a WebPart (part 1) - Writing the RSSFeed WebPart
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 4) - Uploading Excel Reports to the Reports List in Microsoft Dynamics CRM
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 3) - Exporting Dynamic Data to Excel PivotTables
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 2) - Exporting Dynamic Data to Excel Worksheets
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 1) - Exporting Static Data to Excel Worksheets
- Active Directory 2008 : Automating the Creation of User Accounts (part 2) - Importing Users with LDIFDE
- Active Directory 2008 : Automating the Creation of User Accounts (part 1)
- SharePoint 2010 : SharePoint Pages - Deploying Pages
- BizTalk Server 2009 : Advanced Orchestration Concepts - The Cost of Parallel Shapes
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us