Reviews..
| | |
This Month
May 2008
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Year Archive
Login
User name:
Password:
Remember me 
View Article  How to develop editor for a webpart in Sharepoint 2007
In my previous postings on webparts, i have demonstrated how to do it from scratch. In this article, i will explain how to incorporate editor for a webpart. If you have sufficient permissions, you are allowed to configure web parts. When you start configuring web parts via the user interface, a web part tool pane opens. Any configuration information  stored in the pane is persisted in the SharePoint content database. In my previous posting, you learned how to create properties that can be configured using the web part tool pane. The figure below shows an example of a web part tool pane.





Out of the box, SharePoint offers different editors for different types of data. For instance, a string property is represented by the text box editor, while a Boolean property is represented by check boxes. In this section, you will learn how to create custom editor parts, which will be shown in the web part tool pane when you are configuring a web part.

All editor parts inherit from the EditorPart base class, so that is the first step you need to take when creating a custom editor part. Editor parts look a lot like normal web parts. For instance, if you want to add child controls to an editor part, you need to override its CreateChildControls() method. The following code fragment adds a text area consisting of five lines to an editor part:

protected override void CreateChildControls()
{
_txtNormalBox = new TextBox();
_txtNormalBox.ID = “txtNormalBox”;
_txtNormalBox.Text = “[Custom editor part]”;
_txtNormalBox.TextMode = TextBoxMode.MultiLine;
_txtNormalBox.Rows = 5;
Controls.Add(_txtNormalBox);
}

You will also need to override the ApplyChanges() method of an EditorPart instance. This
method is responsible for mapping values in the editor part to corresponding properties in the
associated web part. The following code fragment shows how the ApplyChanges() method sets a
property in an instance of the MyWebPart web part called NormalValue:

public override bool ApplyChanges()
{
MyWebPart objMyWebPart  = (MyWebPart)WebPartToEdit;
objMyWebPart.NormalValue = _txtNormalBox.Text;
return true;
}

The final piece of an editor part that must be implemented is the SyncChanges() method. This method is the opposite of the ApplyChanges() method; it retrieves property values from a web part and stores them in the editor part. The following code fragment shows an implementation for the SyncChanges() method that retrieves the NormalValue property of a MyWebPart web part and stores it in the text area of the web part editor tool pane:

public override void SyncChanges()
{
// Make sure the text area child control is created.
EnsureChildControls();
MyWebPart objMyWebPart = (MyWebPart ) WebPartToEdit;
_txtNormalBox.Text = objMyWebPart.NormalValue;
}

So far, you have seen how to create an editor part. Before a custom editor part is shown in the web part editor tool pane, you need to implement support for this in your web part. In this example, we are creating a web part called MyWebPart. First, we need to add one or more properties that are read from and written to within the editor part. The custom editor part in this section expects the
presence of a NormalValue property in a web part, so that is the first thing that needs to be implemented.

The following code snippet shows how to implement the NormalValue property:

private string _strNormalValue = String.Empty;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName(“Normal value”), WebDescription(“Normal value description”)]
public string NormalValue
{
get { return _strNormalValue; }
set { _strNormalValue = value; }
}

After that, you need to override the CreateEditorParts() method. This method is responsible for returning a collection of custom editor part controls that are shown in the web part editor tool pane when a web part is in edit mode. Basically, this method is used to create a new instance of one or more custom editor parts and add those to the editor part collection. This collection is used
when the web part tool pane is rendered. The following code fragment shows how to add the MyWebPartEditorPart editor part to the collection of customer editor parts that is shown when the MyWebPart web part is being edited:

public override EditorPartCollection CreateEditorParts()
{
MyWebPartEditorPart objEditor = new MyWebPartEditorPart ();
objEditor.ID = ID + “normalEditor1”;
objEditor.Title = “Normal Editor title”;
objEditor.ToolTip = “Normal Editor tooltip”;
objEditor.TabIndex = 100;
objEditor.GroupingText = “Normal editor grouping text”;
ArrayList objEditorParts = new ArrayList();
objEditorParts.Add(objEditor);
EditorPartCollection objEditorPartsCollection = new
EditorPartCollection(objEditorParts);
return objEditorPartsCollection;
}

and here is the complete code:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
namespace KwikSliver.Sharepoint.WebpartDemo
{
public class MyWebPartEditorPart : EditorPart
{
TextBox _txtNormalBox;
protected override void CreateChildControls()
{
_txtNormalBox = new TextBox();
_txtNormalBox.ID = “txtNormalBox”;
_txtNormalBox.Text = “[Custom editor part]”;
_txtNormalBox.TextMode = TextBoxMode.MultiLine;
_txtNormalBox.Rows = 5;
Controls.Add(_txtNormalBox);
}
public override bool ApplyChanges()
{
MyWebPart objMyWebPart = (MyWebPart )WebPartToEdit;
objMyWebPart.NormalValue = _txtNormalBox.Text;
return true;
}
public override void SyncChanges()
{
EnsureChildControls();
MyWebPart objMyWebPart = (MyWebPart)WebPartToEdit;
_txtNormalBox.Text = objMyWebPart .NormalValue;
}
}
}

and below shown is the code for implementation of the MyWebPart web part. This web part displays the custom MyWebPartEditorPart web part in edit mode:

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace KwikSilver.Sharepoint.WebpartDemo
{
[Guid(“288802c4-4dfe-45b6-bb28-49dda89ec225”)]
public class MyWeblPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(“Normalvalue: “ + NormalValue);
}
public override EditorPartCollection CreateEditorParts()
{
MyWeblPartEditorPart objEditor = new MyWeblPartEditorPart ();
objEditor.ID = ID + “normalEditor1”;
objEditor.Title = “Normal Editor title”;
objEditor.ToolTip = “Normal Editor tooltip”;
objEditor.TabIndex = 100;
objEditor.GroupingText = “Normal editor grouping text”;
ArrayList objEditorParts = new ArrayList();
objEditorParts.Add(objEditor);
EditorPartCollection objEditorPartsCollection = new EditorPartCollection(objEditorParts);
return objEditorPartsCollection;
}
private string _strNormalValue = String.Empty;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName(“Normal value”), WebDescription(“Normal value description”)]
public string NormalValue
{
get { return _strNormalValue; }
set { _strNormalValue = value; }
}
 }
}

and here is the end result for all the efforts made above!..












View Article  How to develop menu for a Webpart in Sharepoint 2007
In this posting i will demonstrate how to create menu for a webpart in Sharepoint 2007.

Every web part on a SharePoint page has a web part menu that contains standard options such as the ability to minimize or close the web part. An example of a standard web part menu is shown in figure down below:

In this section, you will learn how to create custom web part menu verbs that are added to the verbs menu in a web part’s header. To start with, it is good to realize that there are three kinds of web part menu verbs:

• Client-side verbs. These verbs perform some kind of action on the client side.
• Server-side verbs. These verbs perform some kind of action on the server side.
• Both. These verbs perform actions on both the client and server side.

You can create a client-side verb by creating a new instance of the WebPartVerb class. The first argument that needs to be passed to the WebPartVerb constructor for a client-side verb is the verb’s id. This needs to be unique. The second argument refers to the client-click event handler. This typically refers to a piece of JavaScript that needs to be executed once an end user clicks on a web part menu verb. The following code fragment shows how to create a client-side verb:

WebPartVerb objFirst = new WebPartVerb(“FirstVerbId”, “javascript:alert(‘Hello from verb!’);”);
objFirst.Text = “first verb text”;
objFirst.Description = “first verb description”;
objFirst.ImageUrl = “_layouts/images/kwiksilver/favicon.gif”;

If you want to create a server-side verb, you need to instantiate a new WebPartVerb class, pass it a unique verb id, and specify a server-side event handler to be executed once an end user clicks on the verb. The next code fragment shows how to create a server-side verb:

WebPartVerb objSecond = new WebPartVerb(“SecondVerbId”,new WebPartEventHandler(SecondVerbHandler));
objSecond.Text = “second verb text”;
objSecond.Description = “second verb description”;

The final web part menu verb type, the kind that performs both client-side and server-side actions, is created by passing a unique verb id, a server-side event handler, and a client-side event handler. You can add custom web part verbs by overriding the Verbs property of a web part and adding custom verbs to it. The code listing shows how to add custom verbs to the web part menu and define an event handler that handles a server-side verb click event:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.Net;
using System.Net.Mail;
namespace KwikSilver.Sharepoint.WebpartDemo
{
[Guid(“288802c4-4dfe-45b6-bb28-49dda89ec225”)]
public class MyMenuWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public override WebPartVerbCollection Verbs
{
get
{
WebPartVerb objFirst = new WebPartVerb(“FirstVerbId”, “javascript:alert(‘Hello from verb!’);”);
objFirst.Text = “first verb text”;
objFirst.Description = “first verb description”;
objFirst.ImageUrl = “_layouts/images/kwiksilver/favicon.gif”;
WebPartVerb objSecond = new WebPartVerb(“SecondVerbId”, new WebPartEventHandler(SecondVerbHandler));
objSecond.Text = “second verb text”;
objSecond.Description = “second verb description”;
WebPartVerb[] objVerbs = new WebPartVerb[] {objFirst, objSecond};
WebPartVerbCollection objVerbCollection = new WebPartVerbCollection(base.Verbs, objVerbs);
return objVerbCollection;
}
}
protected void SecondVerbHandler(object sender, WebPartEventArgs args)
{
//Do something...
}
}
}

This code adds two web part verbs to the web part menu: a client-side verb and a server-side verb. Both verbs are shown in Figure.



and Figure down below shows what happens when you click on the client-side verb. Some JavaScript will
be executed that displays an alert box.




In this posting, you have seen that adding verbs to a web part menu enhances the user interface experience and is quite easy to accomplish.

In my next posting, you will find how to create webpart with custom editor in it.

WELCOME!
Hey there!, I am Purushotam R. Tumkur, an IT Engineer by profession working on Consulting, Development arena specially under Microsoft Technologies. The motivation to bring this site upfront is the reason I want to share my views, thoughts, capabilities and personalities through the channel of the world and technology has exposed to us as "boon". I limit my self through blogging by writing professional and personal thoughts. I am sure you will appreciate me in this matter, hope you will enjoy ride.


My favourites (opens new window)