This week was very interesting. I got my first web parts wrapped into a SharePoint solution and deployed on a farm. I encountered some issues during that process:
- PortalSiteMapProvider: It is a good thing to use it to build high-performance navigation structures. But to get the best performance out of it you need to put it the web.config instead of instantiating it each time you render your navigation structure. Ok, web.config changes can be made in a feature receiver using the WebConfigModification class. BUT this does not work for the provider section. So, if you want high-performance navigation you need to deploy the web.config change manually or use one of the standard providers.
- Dispose: There are two great MSDN articles explaining everything on SPWeb and SPSite disposal. You may think. The little detail that you never, ever should dispose SPContext.Current.Web is not clearly mentioned. So, bear that in mind.
- SPContext.Current.Web: It’s a good thing but it can be null when used in a type’s constructor. So, always make a null check before using it in the constructor.
- XSLT location: It’s a good thing to use XSLT transformations in your Web Part. You can change the rendering without recompile. But where to put the XSLT stylesheet? In general you want to place it in the content database, so that it easily can be accessed/changed and is the same for all the servers in the farm. So you need a HTTP request to retrieve the XSLT and that will slow down your Web Part because it makes the transformation each time it is rendered. Ok, so we put the XSLT transform object into the cache and make the intialization thread-safe. Now, how should we update that cache object to reflect changes in the XSLT stylesheet? This question is for next week to be answered. I suppose it will be a compromise: Refresh it based on a fixed time interval.
- CSS location: The above is kind of the same issues for the styling of your Web Part. Place it in the content database to easily be able to change it. This gives you the following options (and surely more than listed):
– Reference it in the master page.
– Reference it in the page layout(s).
– Emit a HtmlLink control to Page.Head.Controls in the render method.
- SharePoint utilities and stuff: If you are new to SharePoint programming (like me) you do not know the tools it comes with. Microsoft.SharePoint.Utilities has some interesting classes. SPUrlUtility is my best friend to combine URLs. Also check the SPUtility. Working with SharePoint lists you must check-out the SPQuery object, its’ properties and CAML.