Trikks

The digital adventures of Eric Herlitz

How to set a webpart title and description programmatically

Posted by trikks on October 27, 2010

Suppose the headline says it all, here is the code to set the name and description of a webpart programmatically in vb.net.

Just a little snippet

Public Class MyWebPart
    Inherits Microsoft.SharePoint.WebPartPages.WebPart

    ' Method to set WebPart title and description
    ' By Bendsoft 2010
    Public Sub WebPartTitleDesc(ByVal title As String, ByVal desc As String)
        Me.Title = title
        Me.Description = desc
        Try
            Me.SaveProperties = True
        Catch ex As Exception
            Me.Controls.Add(New System.Web.UI.LiteralControl(ex.Message))
        End Try
    End Sub

    Protected Overrides Sub CreateChildControls()

        ' Set the Name of the web part
        Me.WebPartTitleDesc("MyWebPart List Name", "MyWebPart Description")

    End Sub
End Class

No magic really, just make sure to call the method WebPartTitleDesc from the CreateChildControls

About these ads

2 Responses to “How to set a webpart title and description programmatically”

  1. Jon said

    Does this set the name/description of the Web part type you are deploying to Sharepoint, or of one specific instance of the Web part once it is added to a page?

    • trikks said

      This is a bit of an absolute way of setting the Name/Desc of web parts, even if you deploy it with a dwp file it will reset the Title/Desc to whats in the code when loaded. The benefit of this is that you can set the attributes dynamically and from other data sources.

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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: