Sharepoint SmartPart – ToolParts

Today I was creating a web part through means of asp.net usercontrol and the infamous SmartPart for Sharepoint 2007 and realized I hadn’t created a user control that allows properties using the toolpart model. I have used SmartPart on many occasions but normally prefer creating “real” web parts from scratch as it’s what I’m used to when I need to allow admins to “Modify Shared Web Part” and specify properties. However for this scenario I wanted to use smart part but after some Googling I couldn’t find anyone who had documented how to properly implement a toolpart (at least one I could understand J). So here goes, I hope this helps someone….

So my scenario is that I need a url from the user which will populate the hyperlinks within the web part itself as this web part can be installed on many different pages/sites/site collections etc.

First you have your web part class itself

demoWP.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="demoWP.ascx.vb" Inherits="SmartPartDemo.demoWP" %>
MY HTML HERE

<br />

<br />

demoWP.ascx.vb

Imports SmartPart
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebPartPages

Partial Public Class demoWP
    Inherits System.Web.UI.UserControl
    Implements SmartPart.IAdvancedUserControl

#Region "ToolPart"

    Dim mypart As SmartPart.SmartPart
    Private _strURL As String

    <ComponentModel.Browsable(False), ComponentModel.DefaultValue(0), WebPartPages.WebPartStorage(WebPartPages.Storage.Shared)> _
Property strURL() As String
        Get
            Return _strURL
        End Get
        Set(ByVal Value As String)
            _strURL = Value
        End Set
    End Property

    Public Function GetCustomToolParts() As Microsoft.SharePoint.WebPartPages.ToolPart() Implements SmartPart.IAdvancedUserControl.GetCustomToolParts
        Dim toolParts(1) As WebPartPages.ToolPart
        Dim MyTp As New toolpart1
        MyTp.Title = "Custom Properties"

        toolParts(0) = MyTp
        Return toolParts
    End Function

    Public Sub SetContext(ByVal webpart As SmartPart.SmartPart) Implements SmartPart.IAdvancedUserControl.SetContext
        mypart = webpart
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim url As String = mypart.GetCustomProperty("strURL")

        Dim lit As New Literal
        lit.Text = "<a href='" + url + "?id=xxx'>My Link</a>"
        Me.Controls.Add(lit)

    End Sub

End Class

Then the toolpart code file

toolpart1.vb

Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.SharePoint

Public Class toolpart1 : Inherits Microsoft.SharePoint.WebPartPages.ToolPart

    Dim MyPart
    Dim lbl As Literal
    Dim manualsurl As TextBox
    Dim li As ListItem

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyPart = Me.ParentToolPane.SelectedWebPart
    End Sub

    Protected Overrides Sub CreateChildControls()
        CreateControls()
    End Sub

    Public Overrides Sub ApplyChanges()
        EnsureChildControls()
        GetControlValues()
        MyPart.RefreshPropertyValues()
        MyPart.SaveWebPartProperties = True
    End Sub

    Public Overrides Sub SyncChanges()
        ApplyChanges()
        ApplyChanges()
        MyBase.SyncChanges()
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
        EnsureChildControls()
        PopulateControlValues()
    End Sub

    Protected Overrides Sub RenderToolPart(ByVal output As System.Web.UI.HtmlTextWriter)
        EnsureChildControls()
        RenderSelectReportDropDownListControls(output)
    End Sub

    Private Sub CreateControls()

        lbl = New Literal
        lbl.Text = "URL: "
        Controls.Add(lbl)

        manualsurl = New TextBox
        manualsurl.ID = "URLTxt"
        Controls.Add(manualsurl)
    End Sub

    Private Sub GetControlValues()
        MyPart.SetCustomProperty("strURL", manualsurl.Text)
    End Sub

    Private Sub PopulateControlValues()
        manualsurl.Text = MyPart.GetCustomProperty("strURL")
    End Sub

    Private Sub RenderSelectReportDropDownListControls(ByRef output As System.Web.UI.HtmlTextWriter)
        output.Write("</br>")
        manualsurl.RenderControl(output)
    End Sub

End Class

For this to all work you need to be creating a ‘web application’ which once built will give you a dll file which will go into your Sharepoint bin folder.

You should now notice a textbox in the toolpart pane allowing you to specify the url under ‘Custom Properties’.

Please leave a comment below if this has helped you out or you have found a better way!

References

http://smartpart.codeplex.com/Thread/View.aspx?ThreadId=15243

Advertisements

2 Responses to Sharepoint SmartPart – ToolParts

  1. Its like you read my mind! You seem to know a lot about this, like you
    wrote the book in it or something. I think that you could do with a few pics to drive
    the message home a little bit, but instead of that, this is great blog.
    An excellent read. I’ll certainly be back.

  2. I am regular visitor, how are you everybody? This piecxe
    of writing posted at this sitte is actually fastidious.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: