Code vs XML

As a SharePoint developer you can very often chose between code and XML. In my early days as a SharePoint developer, I favored XML, but as I have grown older and wiser, my preference has shifted to doing as much as possible through code. There are two things that you don’t have in XML:

  • Conditional statements
  • Exception handling

From code it’s very easy to check if a list, view, user or whatever you are creating already exists before you try to create it. If your code can handle that something may already exist, the road to creating upgradeable code is very short.

At some point, something will fail. Keeping as much as possible in code makes exception handling easier. In order to understand this we must first see how features work.

Features generally contain one or more XML elements for SharePoint to process. Features may also have a feature receiver that can perform whatever you code it to do. During feature activation, the XML files are processed first, then your feature receiver is called.

If the feature receiver failed, the XML processing might have succeeded. The net result is that some things may remain in the system even though the feature didn’t fully activate. You may correct your error and try again, but then the XML processing may fail since there are some remnants from the previous attempt. Using a pure code approach allows you to adapt to a lot of different scenarios.

There are some things that you cannot do from code; i.e. List Definitions. For those cases I try to split functionality across features and keeping each one as pure as possible.


Leave a Reply

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

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: