Deploying Web Parts without creating an application solution

One of the things you have to ensure when deploying web parts is that the safecontrols entry is added to the web.config file. Without it, you don’t get to have any fun.

The standard way of doing this is through the manifest file of the wsp package. The challenge is that this entry makes it into an application deployed solution rather than a globally deployed solution. If you want to use this in multiple web applications, you will have to deploy it a number of times. This takes time and becomes a hassle when you have to update the solution. Depending on the changes in the solution, you may have to retract it first, then deploy it again. You then need to keep track of where it was deployed.

If you could keep it as a globally deployed solution, life would be easier and faster. Since safecontrols are only a modification to the web.config, it’s very easy to create an alternative approach.  One of the great gems of the SharePoint OM is SPWebConfigModification. This ensures that changes are applied to the web.config file on all servers for all zones. Using a web application scoped feature, you can easily apply the changes to the relevant web applications.

The challenge is that this API uses XPath a lot and can be easy to get wrong. The Mystery Foundation comes to the rescue; it contains a class called WebConfigModifications that wraps common web.config operations; one being a method for adding safecontrol entries.

Here is the actual function:

And this is how it can be used:

You can add more than one entry at a time, and when you are done, calling the Commit method will update the system.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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: