The Best Practice MSDN page has some interesting hints to generate better code. In this post I write about some point of that article, provide samples and fix bugs which are in the MSDN article.
Caching
You should only cache thread safe objects. What’s that?
This means, that you should only cache objects, which can not be changed from the outside of your code. An itemCollection (as list.Items) is changed, if another user adds an item. But if you cache a DataTable which you can get from an itemCollection with list.Items.GetDataTable() it will not be changed later on, and can be cached.
For months the Webpart could be installed through a solution/feature without any problems. Then I had to change something in the code. When I tried to upgrade the solution to see the changes, the Webpart could not be added to a page anymore.
The assembly is deployed to the GAC through the solution. It has been upgraded.
Looking at the xml file in my solution did not show any errors. It looks fine.
A couple of days ago Microsoft released 10 themes for SharePoint. They provided solutions for you, so that you can build features to install and deploy the themes on your SharePoint farm. The problem with the provided themes is, that they are only meant to be installed on English SharePoint sites! The path to the images is hard coded to the English LCID.
Daniel Brown has already created a solution which you can install. His solution has the same limitation with the language as the provided sample by Microsoft.
As I’ve
mentioned earlier, the SPD can be downloaded from Microsoft for free.
All you have to do is register before you can download the SharePoint Designer.
So don’t wait, and get it 🙂 Please remember, that if you modify a SharePoint page, it will be stored in the database and loose its reference to the template. This is called unghosted. Joel Oleson has written a great article about this.
By now most of you should know that there is a SharePoint Diagnostics Tool. This really is a great tool, since it scans your assemblies for memory leaks.
You should also know, that there is an occasion you can create a memory leak which the tool will not recognize.
1: using (var site = new SPSite(“http://yoururl”))
<span class=lnum> 2: </span>{ 3: SPWeb web = null;
<span class=lnum> 4: </span> <span class=kwrd>try</span>
5: {
<span class=lnum> 6: </span> web = site.OpenWeb();
7: // do something with this web
<span class=lnum> 8: </span> <span class=rem>// ...</span>
9: // done. don't need it anymore. but i need another web
<span class=lnum> 10: </span> web = site.AllWebs[<span class=str>"webname"</span>];
11: }
<span class=lnum> 12: </span> <span class=kwrd>finally</span>
13: {
<span class=lnum> 14: </span> <span class=kwrd>if</span> (web != <span class=kwrd>null</span>) web.Dispose();
15: }
<span class=lnum> 16: </span>}
The SPWeb from line 6 will not be disposed in line 14, because in line 10 we assign another SPWeb to the web!
How do you copy the content from a Word document to a Wiki library? A simple Copy&Paste does not work, as it omits the pictures from the document.
Andreas Strothmann has posted a interesting way for deploying Word docs to a Wiki library.
His steps are:
- Open your Word document
- Save it as docx if it is an old doc
- Publish the document to a SharePoint Blog
Note: the pictures included in the Word document will be uploaded to the Blog picture library! So do not delete the Blog site later, and make sure your Wiki visitors can read items from the picture library.
- Open the Blog entry in your browser and click on the Edit Button
- Copy the whole entry
- Paste the clipboard to a Wiki page