Adding assemblies and namespaces to web.config to avoid duplication of code

In many (like all of them) items in SharePoint (aspx/ascx/master) I work with you keep repeating the same sort of code in the page directive.


Adding the ability to work with controls from the Microsoft.SharePoint.WebControls and Microsoft.SharePoint.Publishing.WebControls namespaces.
This code is “duplicated” in all these items and I don´t like repeating myself so wouldn´t it be better if these declarations could be “inserted” in some other way?

The solution

First of all, what I always do is start by updating my web.config file in my Visual Studio solution with tagprefixes that I use as you can see below:

This is because I want intellisense support in VS for my aspx/ascx/master items.
Of course in SharePoint we have to update the web.config by the SPWebConfigModification API through a feature. I showed you how to do that in a previous post so I won´t go into that but below is how that web.config file looks.

I have mapped Microsoft.SharePoint.WebControls namespace from the Microsoft.SharePoint.dll to the tagprefix
msw and mspw is mapped to the same sort of namespace but in the publishing dll.

As an example now I can use the SPUrl expression prefix provided by the Microsoft.SharePoint.Publishing.WebControls.SPUrlExpressionBuilder class like this:

without adding the declaration at the top of the page/controls.

And also this now works without adding anything else:

  1. Leave a comment

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: