Donnerstag, 22. Juli 2010

Create your custom webpart property - eigene Webpart Eigenschaft erstellen

Wenn man ein eigenes Webpart entwickelt, kommt es vor, dass man zur Darstellung Daten vom Webpartnutzer benötigt um Inhalte vollständig anzuzeigen. Hierzu gibt es beim Bearbeiten des Webparts die Eigenschaften, welche Einstellungen am webpart vornehmen.
Dieser Blogpost soll zeigen, wie man diese Eigenschaften mit eigenen Eigenschaften erweitert.
Man erstellt zuerst ein neues Visual Webpart in Visual Studio 2010. Das Visual Webpart erstellt eine .cs Datei in gleicher Hierarchie, wie die .webpart Datei.

When you develop your own webpart, there might be the need to gather data from the webpart-user to display content correctly.
Therefore the webpart does have the properties you see, when clicking "edit webpart", that set things of the webpart.
This blog-post is supposed to show, how you can extend those properties with custom properties.
First you have to create a new Visual Webpart in Visual Studio 2010. This Visual Webpart creates a .cs file in the same directory level like the .webpart file.

sharepoint project


Öffnen Sie diese Datei und dann können Sie zu der VisualWebPart1 Klasse folgende Eigenschaft hinzufügen:

Open this file and then you can add to the VisualWebPart1 class the following property:

[WebBrowsable(true), Category("Own Category"), Personalizable(PersonalizationScope.Shared), DefaultValue(""), WebDisplayName("Property Title"), WebDescription("Property Description")]
public string MyCustomProperty
{
get { return _mycustomproperty; }
set { _mycustomproperty = value; }
}
public static string _mycustomproperty;

Category definiert die Kategorie, in welche die Eigenschaft angezeigt wird. Man kann hier auch eine eigene Kategorie erstellen.
Einfach hier einen neuen Namen hineinschreiben. Alle anderen Eigenschaften der Eigenschaft sprechen für sich und können mit anderen Inhalten getestet werden.

Category defines the category, where the property will be displayed. You can create also your own category. Just enter a new name. All other properties of this property talk for themselves and can be tested with different content.

Im VisualWebPart1UserControl.ascx.cs kann man einfach wie folgt auf diese Eigenschaft zugreifen:

In the VisualWebPart1UserControl.ascx.cs file you can access this property easily like that:

litOutput.Text = VisualWebPart1._mycustomproperty;


Deployen Sie jetzt das Webpart und dann gehen Sie auf "Webpart bearbeiten". Sie sehen dann einen eigenen Reiter und in diesem die Eigenschaft. Geben Sie etwas ein und wenn Sie den Wert im Webpart Usercontrol ausgeben, werden Sie ihn auf der Webseite sehen.

Deploy the webpart and then go to "Edit webpart". You will see your own tab and in there your property. Enter something and if you output the value in the webpart usercontrol, you will see something in the webpage.

Grundsätzlich werden bei folgenden Eigenschaften folgende Controls erstellt:

Basically the following properties generate the following controls:
bool -> Check box
DateTime -> Text box
enum -> Dropdown
int -> Text box
string -> Text box

Aber was macht man, wenn man z.B. Radiobuttons oder andere Controls verwenden will? Dann kann man diese auch selbst erstellen.
In der VisualWebpart1.cs Datei muss die VisualWebpart1 Klasse von folgender Klasse erben: Microsoft.SharePoint.WebPartPages.WebPart.
Anschliessend die GetToolParts() Methode überschreiben.

But what if you want to use controls like radio-buttons or other controls? You can create them on your own.
In the VisualWebpart1.cs file the VisualWebpart1 class needs to inherit from this class: Microsoft.SharePoint.WebPartPages.WebPart.
Then you need to override the GetToolParts() method.

public override ToolPart[] GetToolParts()
{
ToolPart1 mytoolpart = new ToolPart1();
mytoolpart.Title = "My Tab";

List lstToolParts = new List(base.GetToolParts());
lstToolParts.Insert(0, mytoolpart);
return lstToolParts.ToArray();
}


Dieser Code wird die ToolPart1 Klasse dem bestehenden Array hinzufügen, der aus den Default Toolparts besteht.
Dann müssen wir nur noch die ToolPart1 Klasse erstellen.

This code will add the ToolPart1 class to the existing array, that already contains the default toolparts.
Then we need to create the ToolPart1 class.


public class ToolPart1 : Microsoft.SharePoint.WebPartPages.ToolPart
{
public ToolPart1()
{
}

private RadioButtonList myRadioButtons = new RadioButtonList();

//Override the method to create the existing controls and then add your control
protected override void CreateChildControls()
{
base.CreateChildControls();
myRadioButtons.ID = "myRadioButtons";
myRadioButtons.Items.Add("Item 1");
myRadioButtons.Items.Add("Item 2");
myRadioButtons.Items.Add("Item 3");
myRadioButtons.Items.Add("Item 4");
this.Controls.Add(myRadioButtons);
}

//This method happens, when someone will click "OK" or "Apply" and the value will be added to the webpart class-property
public override void ApplyChanges()
{
base.ApplyChanges();
VisualWebPart1 parentWebPart = (VisualWebPart1)this.ParentToolPane.SelectedWebPart;
if (!string.IsNullOrEmpty(myRadioButtons.SelectedItem.Value))
parentWebPart.MyCustomProperty = myRadioButtons.SelectedItem.Value;
}
}


Das war alles. Man kann den Code anpassen und abändern und so ziemlich jedes Control selbst einfügen. Auch Telerikcontrols können hier verwendet werden.

This is it. You can edit or extend this code and insert almost any control. You can also add Telerik controls.

Keine Kommentare:

Kommentar veröffentlichen