Pages

1/30/2010

SharePoint: New VB web part not showing up in Web Part Gallery: New Web Parts (NewDwp.aspx)

 

This issue described here is actually more of a Visual Basic issue than SharePoint, but it often pops up in SharePoint web part deployment.

The issue: You create a web part in C# with a “namespace Max.Demo”, build it, deploy the DLL, add it to safe controls and go to Site Actions, Site Settings, Web Part Gallery, New Web Parts. It shows up in the list just fine with a name like: assemblyname.namespacename.classname.

You try the exact same steps above using Visual Studio, and the web part does not show up in New Web Parts.

The problem is with the namespace and how you entered it into <SafeControls>, actually it’s because you did not know the real namespace name.

In C# when you add “namespace Max.Demo” to a class then that is the namespace name generated. In VB when you add “namespace Max.Demo” you are creating a namespace name that is built from two parts, what you typed added to what is specified in the project’s properties screen as the “Root namespace”.

 

So in VB if your properties look like this:

    image

and your code looks like this:

  Namespace Max.Demo
    Public Class VBWebPart
    Inherits WebPart

VB creates a namespace named:   MyWebPartProject.Max.Demo

 

So for VB projects:

  • Only set your namespace in Project Properties
  • Or delete the namespace in Project Properties (leave it blank) and set the namespace in code
  • Or remember that the namespace in Project Properties is combined with what you set in code

 

If you want to see just how everything got named then create a little project with this code:

 

Imports System.Reflection
 
Module Module1
 
    Sub Main()
        Dim a As Assembly = Assembly.LoadFrom("C:\yourprojectpath\vbwebpart.dll")
        Console.WriteLine("Fullname:" & a.FullName)
        Console.WriteLine()
 
        For Each t As Type In a.GetExportedTypes
            Console.WriteLine(t.Namespace + " " + t.Name)
        Next
        Console.ReadLine()
    End Sub
 
End Module

 

.

1 comment:

Anonymous said...

Thanks Mike, I have spent many hours on google trying to find a solution. Never thought of placing the project namespace in front of those in the class, as all web examples are founded on C#. Paul

Post a Comment

Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.