Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

My client is upgrading from WSS 3.0 to SharePoint 2010 (yes, I know the year is 2014, but baby steps). I am migrating my custom solutions to work in in 2010. My current issue is the way SharePoint 2010 handles namespaces.

Microsoft introduced the concept of namespaces with the .NET Framework in 2002. It is a good system to organize classes, and allows for identical class names from different libraries in the same application. For example:

The "WebPart" class belongs to the System.Web.UI.WebControls.WebParts namespace. So, to inherit a WebPart class, the full name is System.Web.UI.WebControls.WebParts.WebPart.

So for all of my web parts for company ABC, I use a namespace heirarchy like this:

ABC.WebParts - The root namespace.
ABC.WebParts.Sites - All classes that belong to the Sites library (feature).
ABC.WebParts.Sites.SiteLinks - The full path to the SiteLinks web part in the Sites feature.

Now, with SharePoint 2010 in Visual Studio 2012, when I create a new web part, the class is added to the namespace! For example:

ABC.WebParts - The root namespace.
ABC.WebParts.Sites - All classes that belong to the Sites library (feature).
ABC.WebParts.Sites.SiteLinks - The namespace for the SiteLinks web part. ONLY the SiteLinks web part!
ABC.WebParts.Sites.SiteLinks.SiteLinks - The full path to the SiteLinks web part in the Sites feature.

This namespace is referenced in MULTIPLE locations; the .cs file, .xml files, .webpart files, etc. throughout the feature.

For a comparison, if you think of classes and namespaces as a phone number with area code, where you used to have the phone number (757) 555-1212, your phone number is suddenly (757-555-1212) 555-1212, and there are no other phone numbers in the (757-555-1212) area code but yours. It makes no sense.

So, I have two choices: (1) Accept that this is the way things have to work, or (2) find every instance of namespace referenced and change it to the correct way - the way namespaces have worked since 2002.

Am I wrong here? Am I missing something? Have other developers run into this issue?

share|improve this question

1 Answer 1

I can confirm that the (not-so-good) behaviour you are experiencing is indeed the default one. Whoever wrote the Visual Studio template though that using the web part class name inside the namespace was a good idea - probably this was done to avoid name collisions.

That said, unless you find or write an alternate template afaik you are pretty stuck with what you get unless you manually change the autogenerated namespaces. If you do it soon after you create the web part you should "only" have to change

  • the .cs file
  • the .webpart file
  • if you are writing a visual wp you may need to change also the safe controls namespace declaration.

While we are at it, you may have noticed that the need to change the namespace in the webpart file could have been avoided if Microsoft used an alternate declaration:

<type name="$SharePoint.Type.GUID-HERE.AssemblyQualifiedName$" />

If you mark a class with a guid attribute you can then refer to the assembly qualified name with the above expression, which eliminate the need to update the namespace. Just remember that the guid string in the attribute must be lowercase and without curly braces or it won't work as expected...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.