I’ve recently written a small utility class for SharePoint and thought I’d share it with the community. The class allows you to automate the creation, modification or deletion of WebParts and their content on any number of SharePoint pages.
It came from a requirement to automate the updating of an active site with new functionality, such as adding a WebPart in a specific zone of a particular page or changing the properties of other WebParts. Obviously some of this could be implemented by changing the elements manifest for the pages in question, but this would effectively replace the content with what was defined in the manifest. I needed a way of modifying certain WebParts while leaving changes made by end users intact.
The changes are defined within a XML document containing a set of actions. Actions define where the updates should occur. Each action is comprised of a set of content updates. These define the actual WebPart to add, remove or update.
When updating a WebPart all that is required is to specify each property to update and the new value for it. Removing simply requires the details of the WebPart, while adding expects the definition of the WebPart, which is the same as when defining a WebPart within a feature.
A sample XML document is provided with the source code, along with a XSD for the schema.
The code itself is self explanatory and I’ve tried to keep it as simple as possible. WebParts are identified by their namespace, display title and containing zone. I intend to update this to allow you to optionally specify the display title and zone, allowing for updates to multiple WebParts with the same namespace to occur.
The code is currently in beta, so expect some updates in the not too distant future.
The C# source is hosted in CodePlex and can be found by following this link.