Using StyleCop to enforce source code standardization

StyleCop is a Visual Studio add-on used to enforce C# source code style rules.

After installation, you can run the checks from the new menu voice Tools | StyleCop; after execution, it will display its alterts as warnings in the Error window.

You can configure the type of warnings to be signaled with the online editor, that modifies the Settings.StyleCop file (simply double click on the file on your file system to edit it).

It's enough to have the Settings.StyleCop file (or better: a link, so that all projects reference a common file) in your Visual Studio project, so that after each build these additional checks are done. And StyleCop can work as a custom MSBuild task too.

You can find more informations in the blog post Enforcing coding standards with Microsoft StyleCop.

Parallel programming in .NET 4 and custom TaskScheduler

.NET 4 introduces the concept of Parallel Programming. To simplify drammatically, its main concepts are:

  • Task: higher abstraction of Thread; conceptually, a thread is a way of accomplishing a task. Above all, you can cancel a Task (and its implementation should cancel the inner thread);
  • PLINQ: evolution of LINQ to allow parallel execution of LINQ queries.

You can get an introduction to the news of .NET parallel programming in the following two articles:

In my case I needed to create a custom TaskScheduler to handle priorities (like in Threads) for a custom queue management system. So, I got help from the following two articles:

SPDisposeCheck tool

In SharePoint, there are various rules about disposing objects. I suggest you to read these two complete articles:

In brief, you should always dispose SPSite and SPWeb objects, with the exception that "According to SharePoint best practices, SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web should not be explicitly disposed by user code".

I strongly suggest you to check your code installing the very useful tool SPDisposeCheck. In particular, when you install it, you can also integrate it in Visual Studio and run it automatically after each build, so that it throws error both if you forget to dispose some objects and (above all) if you dispose objects when you shouldn't.

How change your Active Directory picture, so that it shows in Lync clients

Here it's a trick to update the Active Directory associating a picture to a user profile, and it shows in Lync clients too!

Here is the script content, you should save it in a .vbs file, change your distinguished name and the file path, and execute it. Btw, you can get your Active Directory distinguished name using ADExplorer.

SavePictureToAD "LDAP://myDistinguishedName", "C:\myFace.jpg"
Function SavePictureToAD(szADsPath, szLoadFileName)
   Set objUser = GetObject(szADsPath) 
   objUser.Put "thumbnailPhoto", ReadByteArray(szLoadFileName)
   MsgBox "Done"
End Function
Function ReadByteArray(strFileName)
   Const adTypeBinary = 1
   Dim bin
   Set bin = CreateObject("ADODB.Stream")
   bin.Type = adTypeBinary
   bin.LoadFromFile strFileName
   ReadByteArray = bin.Read
End Function