Posts Tagged ‘SP2010’

So today we launched Pointbird.com, let’s celebrate!

November 29th, 2013 by Tobias Zimmergren

Introduction

The last few weeks and months I’ve been busy setting up the foundation for a new set of products we’re developing. We call them Pointbird. The story behind the name is a tale for another time.

Simple but powerful plugins, products and solutions for SharePoint and other services – that’s what we’re currently building. We’ve also simplified licensing so that all our customers can easily understand our model and implement our solutions without any trouble! Easy as 1-2-3.

pointfinal-for-sidebar

Products

We’ve got a set of products lined up in our respositories that are in testing and conceptual phases. However we’ve already launched a few products that we’ve experienced as the most popular requirements from our customers in the projects I’ve been involved in the last few years. Nobody had found a solution for these common problems, so I drew some sketches and then we started creating them. Some are small, others more complex – but they all offer value to our clients and that’s the most important thing for me.

Pointbird Short Url

So the Short Url product, as the name implies, is a product that can shorten url’s of items in SharePoint lists and libraries.

It’s pretty slick and for the user it’s just a matter of clicking one link in the ECB menu:

We’ve actually already implemented this solution at a few clients, and today as soon as we launched we had a few copies sold during the first few hours. Wohoo!

Pointbird Zip Saver

A common request from a lot of my clients I’ve been working with is to be able to select specific files from a library and then just download them all, including folders. Requirements were simple and very clear, so that’s exactly what we implemented – a “Zip Saver” solution that can download multiple files at once, including folders, and presents them nicely in a zip-file. The zip-file contains the exact same structure as your library that you chose to download. Neat! Very neat!

The Pointbird Zip Saver is a rather popular demand and we’ve implemented this in a few organizations already as well.

Pointbird Notify

A very simlpe yet smooth solution for displaying ad-hoc messages to your users in your various sites and site collections. A popular request is to instead of using a portal start page news list or announcement list, to be able to push out messages to your users in a more persistent and focused way. With the Pointbird Notify solution you can simply create a new list item in a special list containing announcements, and it will automatically display it for your users with the settings you specify. Awesome.

Summary

That’s it. No more marketing – we’re done for now. Hope you enjoy it :-)

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

A while back an announcement was made that TFSPreview.com had been made available for general testing. Various bloggers at Microsoft put an invitation token in their MSDN blogs so everyone can have a go at it.

In this article series we’ll take a very quick look at what the hosted TFS solution by Microsoft looks like.

Articles currently in the series:

Steps to hook up a project to your new build server

This article obviously assumes that you’ve already followed along with the previous articles and hooked up a build configuration for  your TFSpreview account. Now we’ll take a look at how we can get our projects hooking up in a CI/Automated Build scenario with Team Foundation Services.

The steps from this point onwards are basically the same as if it would be an on-premise TFS server in your own domain. Your build server is configured, your code is hosted in TFS and all you’ll need to do is connect your project to the actual TFS and then create a new build definition.

Create a new project (or connect an existing one) and connect to TFS

We’ll start from the beginning and create a new Visual Studio 2010 project (in my case it’ll be an Empty SharePoint Project), and remember to tick the Checkbox "Add to source control":
image

Make sure that the project is connected to your TFS server, check in the source and we can get started:
image

Create a new build definition

At this point (if you’ve followed the articles in this article series) you should have a TFS server, a connection from Team Explorer to your TFS server and also a new project hooked up in your repository. Now its time to create our first build definition so we can automate the builds and deployments.

Start by navigating to your Team Explorer and right-click on Builds and then click the "New Build Definition…":
image

This will give you the following new dialog where you can specify details for your build:
image

Move on to the "Trigger" tab. In my case I want to enable CI (Continous Integration) for my project:
image

Move on to the "Workspace" tab. In my case I’ll leave the Source Control Folder as the default root as seen below. You can choose to specify a specific TFS project if you don’t want to include all.
image

Move on to the "Build Defaults" tab. You’ll need to specify a build controller (you should have one here since we created one in the previous article). You will also need to specify a drop folder, where your binaries are going to be delivered upon the build: 
image

Move on to the "Process" tab. This is where things get really interesting. You can from this dialog specify a variety of different variables for your project when it builds. I’m not going to dig into details here because my good mate Chris O’Brien have covered all of that in his article series about automated builds.
image

Save the build definition and validate that it appears in the Team Explorer:
image

Test the build configuration

In order to validate that our setup now works with TFSpreview.com and our own build server and to validate our newly created build definition, simply make some changes to your project and check it in and have it automatically schedule a new build (We chose Continuous Integration, which will build on each check in). You can see that the build is now scheduled and currently running:
image

And after a while you can validate that it is Completed:
image

The final validation is of course to see the drop folder that we specified and make sure that it now contains our newly built sources:
image

Voila. Build seems to be working.

Summary

This post was intended to give you an overview over the simplicity of creating a simple build definition and getting started with automated builds in TFSpreview (hosted Microsoft TFS). Pretty neat and it seems to be working just the way we want.

Obviously there’s some apparent questions like:

  • What if I want to output my .wsp files as well?
  • What if I want to execute a specific script upon the execution of the build so I can automate test-deployments?
  • Etc. etc.

My first recommendation is to visit Chris O’Brien and read all the posts in his CI/automation series which is simply amazing.

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

A while back an announcement was made that TFSPreview.com had been made available for general testing. Various bloggers at Microsoft put an invitation token in their MSDN blogs so everyone can have a go at it.

In this article series we’ll take a very quick look at what the hosted TFS solution by Microsoft looks like.

Articles currently in the series:


Connect Visual Studio 2010 to your new hosted team project

In order to be able to connect to the hosted TFSPreview team project, you’ll need to comply with the prerequisites I’m listing here.

Prerequisites

Hook up Visual Studio to your new repository/project

Alright, if you’ve downloaded and installed KB2581206 (which means you’re spinning VS2010 SP1 already) you are read to connect. The procedure to connect to the hosted TFS service is basically the same as if you were to connect to any other TFS repository, which is easy and awesome.

In Visual Studio 2010 SP1, simply make these smooth ninja moves and you’re done:
image

Make sure to fetch the URL of your account (As seen in your dashboard, like depicted below):
image

Enter this URL in the Visual Studio 2010 dialogs and we’re ready to kick off:
image

It’ll ask you for your credentials which you need to use to verify your account details:
image

You should now be authenticated and your repository should be available:
image

You’ll go ahead as you normally do and choose the projects that interests you and then you’re basically done:
image

Your Team Explorer should contain your TFS project and you should be able to work with it as you normally would from Visual Studio 2010:
image

This means you’ve got all of your standard tasks and operations available straight from VS 2010 (So you don’t have to go to the website to make changes …):
image

Summary

Easy enough. As soon as you’ve downloaded the required tooling to get connected, you can hook up your new cloud-hosted team project in Visual Studio 2010 without any problems. Give it a spin, it flows quite nicely!

Enjoy.

SP 2010: Developing for performance Part 6 – CSS Sprites

April 8th, 2011 by Tobias Zimmergren

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

SharePoint 2010 developing for performance article series:
In this series of articles I will briefly introduce you to some key concepts when it comes to developing for performance in our SharePoint 2010 applications.

Related articles in this article series

Part 1 – SP 2010: Developing for performance Part 1 – Developer Dashboard
Part 2 – SP 2010: Developing for performance Part 2 – SPMonitoredScope
Part 3 – SP 2010: Developing for performance Part 3 – Caching in SharePoint 2010
Part 4 – SP 2010: Developing for performance Part 4 – Logging
Part 5 – SP 2010: Developing for performance Part 5 – Disposal patterns and tools
Part 6 – SP 2010: Developing for performance Part 6 – CSS Sprites
Part 7 – SP 2010: Developing for performance Part 7 – Crunching those scripts
Part 8 – SP 2010: Developing for performance Part 8 – Control that ViewState

Part 6 (this article):
In web development today its very important to keep performance in mind – this is especially important in any site that is published externally or for whatever reason will have heavy traffic. In this article I will briefly cover the area of CSS Sprites, how they are implemented in SharePoint 2010 and of course how you can create them yourself.

Introduction to CSS Sprites

What are CSS sprites anyway?

CSS sprites is a technique used to reduce the number of requests for images on a site when visiting it. Normally when you see a web site today there’s plenty of files being requested and loaded upon the request made. With the use of CSS sprites we can reduce the amount of files that we need to request from the server by consolidating many image files into one file and use CSS to display the correct part of that consolidated image when it needs to depict your specific image.

Short in short: Take a bunch of separate images and smack them together to one single file. Voila, you’ll only need to request one file from the server.

SharePoint 2010 and CSS Sprites

So why are we talking about CSS sprites here anyway, isn’t this supposed to be all about SharePoint and awesomeness? But of course, and that’s exactly why we’re talking about it – CSS sprites are a natural part of a normal SharePoint 2010 installation. By default, there’s CSS sprite techniques used to render and display images in (for example) the Ribbon menu.

These are some samples of images used with CSS sprites in SharePoint 2010:

FGIMG.PNG jsgridcluster.png
FGIMG jsgridcluster

As you can see in any of the two images above the many small images/icons have been smashed together into one single image file and then there’s some CSS magic to position it. This is basically what it’s all about.

Why should I bother learning to use CSS sprites?

This would be like asking yourself the question "Why would I develop something that is of great performance" or "Why would I do something awesome". No more rhetorical questions please ;-)

Of course the reasoning behind creating CSS sprites are that you can develop web applications that can reduce the page load and enhance the performance of your sites and make them load quicker and be more responsive.

Creating CSS sprites for my applications? How?

There’s a few ways to create CSS sprites for you own applications.

  • Do it manually.
    • This approach requires a lot from you (the designer) in order to hack it up properly and make sure it’s pixel-correct.
  • Do it using a tool
    • This approach is what I normally use.
    • With any of the tools on the market to create CSS sprites you’re better off than doing it manually – all you have to do is dump you images into an application and it’ll output one single image along with the CSS markup for that. Awesome.

For obvious reasons mentioned above I will not be demonstrating how to do this manually but will rather be using any of the hundreds of tools out there to create the sprites.

In this example I’m going to use my main man Waldek’s tool called SpriteGenerator.

There’s a bunch of other tools available to create and generate these sprites as well of course, bing.com is thy friend.

How to: Generate CSS sprites using a tool

In this section I’ll talk you through our current application and what it looks like, what we want to achieve and finally how to achieve it.

Our current application, a visual sample

This is my current sample application before we start using sprites.

Control Panel Web Part (dummy):
image

This is the Visual Studio project, containing all the images and resources:
image

As you can see there’s quite some images being loaded. The images used in the application (my Web Part) will be fetched using a normal HTTP-request and that’s when one could realize how quickly those small requests can add up to impact performance.

This application uses some generic HTML markup like this:

         <div  class="width300p"> 
             <div  class="leftalign floatleft width30"> 
                 <div  class="height70p"><asp:Image  ID="Image2"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/mail.png"  /></div> 
                 <div  class="height70p"><asp:Image  ID="Image3"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/message.png"  /></div> 
                 <div  class="height70p"><asp:Image  ID="Image4"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/more.png"  /></div> 
             </div> 
             <div  class="centeralign floatleft width30"> 
                 <div  class="height70p"><asp:Image  ID="Image5"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/notebook.png"  /></div> 
                 <div  class="height70p"><asp:Image  ID="Image9"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/search.png"  /></div> 
                 <div  class="height70p"><asp:Image  ID="Image13"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/favorites.png"  /></div> 
             </div> 
             <div  class="width30 floatright"> 
                 <div  class="height70p"><asp:Image  ID="Image6"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/info.png"  /></div> 
                 <div  class="height70p"><asp:Image  ID="Image7"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/logoff.png"  /></div> 
                 <div  class="height70p"><asp:Image  ID="Image8"  runat="server"  ImageUrl="/_layouts/images/TOZIT.CSSSprites/question.png"  /></div> 
             </div> 
         </div>

Now, in order for us to utilize the CSS sprites instead, and not have to make a request for each of the images separately – we’ll use the SpriteGenerator and create us some nice CSS sprite magic.

Using the Sprite Generator to make some magic

Like I said before, we’ll be using Waldek’s SpriteGenerator for this sample.

Just open it, put in the images you’d like for it to make some magic with and then simply hit the magic button.

Tip: Check out how to use the SpriteGenerator here

The result of using the SpriteGenerator can look something like this:
image

This is one consolidated image containing all the graphic from my application. But instead of having about 30 different icons in my project, I now have only one.

The generator will create the CSS needed as well, and it can look something like this:

.sprite { background: url(‘/_layouts/images/TOZIT.CSSSprites/TOZIT_Sprite.png’) no-repeat top left; }
.pc { background-position: -0px 0; width: 47px; height: 64px; }
.question { background-position: -47px 0; width: 53px; height: 64px; }
.info { background-position: -100px 0; width: 54px; height: 64px; }
.mail { background-position: -154px 0; width: 54px; height: 64px; }
.more { background-position: -208px 0; width: 54px; height: 64px; }
.user { background-position: -262px 0; width: 55px; height: 64px; }
.world { background-position: -317px 0; width: 55px; height: 64px;  }
.video { background-position: -372px 0; width: 56px; height: 64px; }
.favorites { background-position: -428px 0; width: 57px; height: 64px; }
.edit { background-position: -485px 0; width: 60px; height: 64px; }
.date { background-position: -545px 0; width: 64px; height: 57px; }
.file { background-position: -609px 0; width: 64px; height: 64px; }
.group { background-position: -673px 0; width: 64px; height: 58px; }
.home { background-position: -737px 0; width: 64px; height: 62px; }
.left_arrow { background-position: -801px 0; width: 64px; height: 59px; }
.logoff { background-position: -865px 0; width: 64px; height: 78px; }
.notebook { background-position: -929px 0; width: 64px; height: 51px; }
.rigth_arrow { background-position: -993px 0; width: 64px; height: 57px; }
.search { background-position: -1057px 0; width: 64px; height: 61px; }
.sound { background-position: -1121px 0; width: 64px; height: 58px; }
.stop { background-position: -1185px 0; width: 64px; height: 59px; }
.vault { background-position: -1249px 0; width: 64px; height: 53px; }
.message { background-position: -1313px 0; width: 66px; height: 64px; }
.image { background-position: -1379px 0; width: 84px; height: 84px; }
.tozitlogo { background-position: -1463px 0; width: 250px; height: 101px; }

Now, what this really means is that you now have the ability to actually use only one single image to portray several.

New Visual Studio structure

Obviously, with the new update and only using one single image – the Visual Studio project might look something like this:

image

New HTML markup

And finally, there needs to be some changes in the HTML markup to reflect the changes with the images. The markup could look something like this:

         <div  class="leftalign floatleft width30"> 
             <div  class="height70p sprite mail">&nbsp;</div> 
             <div  class="height70p sprite message"  >&nbsp;</div> 
             <div  class="height70p sprite more"  >&nbsp;</div> 
         </div>

How does using CSS Sprites affect requests, hence performance?

Dealing with sprites isn’t very hard to get started with as you saw in the previous short samples. But what’s more interesting from a performance perspective is of course how it can affect the performance of the requests being made towards the server.

Let’s do some generic Fiddler-magic to see what the requests look like without using sprites and then what the requests look like with the use of sprites.

Fiddler test 1: NOT using CSS sprites

image

Request Count 17 total requests made to the server!
Bytes Sent 8 137 bytes
Bytes Received 76 164 bytes

Fiddler test 2: Using CSS sprites

image

Request Count 3 total requests made to the server!
Bytes Sent 1 722 bytes
Bytes Received 139 369 bytes

What you can easily conclude from the initial tests is that there’s more requests without the CSS sprites, while the size can be slightly larger for a consolidated image when using the sprites.
It’ll load and render quicker by downloading only the 3 files instead of the 17 files due to the aggregation time it takes to render the content.

Visualize the results, please!

Using CSS sprites
(Time in seconds to load the data)
NOT using CSS sprites
(Time in seconds to load the data)
image image

RESPONSE BYTES
image/png:    120 250
text/html:    16 593
~headers~:    1 748
text/css:    778

RESPONSE BYTES
image/png:    52 417
text/html:    16 709
~headers~:    6 260
text/css:    778

ACTUAL PERFORMANCE
Aggregate Session time:   
00:00:00.531 (0.5 seconds)
Sequence (clock) time:
00:00:00.4281250 (0.4 seconds)

ACTUAL PERFORMANCE
Aggregate Session time:
00:00:01.390 (1.3 seconds)
Sequence (clock) time:
00:00:00.5625000 (0.5 seconds)

I did some tests with two machines, two different projects and a couple of clients to request the pages and the results (on average) was something like this in rendering time. The chart below is a relative visualization of the results per average.

Chart

Summary

The impact of a system with a lot more requests for data than these samples, more users and in generally more traffic would differ a bit more than the charts here. Using sprites in combination with caching the data is a good practice to keep the amount of requests to the server to a minimum.

Even if this minor change don’t have any critical impact on improving the performance it’s important to consider. "All improvements are good improvements".

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

SharePoint 2010 developing for performance article series:
In this series of articles I will briefly introduce you to some key concepts when it comes to developing for performance in our SharePoint 2010 applications.

Related articles in this article series

Part 1 – SP 2010: Developing for performance Part 1 – Developer Dashboard
Part 2 – SP 2010: Developing for performance Part 2 – SPMonitoredScope
Part 3 – SP 2010: Developing for performance Part 3 – Caching in SharePoint 2010
Part 4 – SP 2010: Developing for performance Part 4 – Logging
Part 5 – SP 2010: Developing for performance Part 5 – Disposal patterns and tools
Part 6 – SP 2010: Developing for performance Part 6 – CSS Sprites
Part 7 – SP 2010: Developing for performance Part 7 – Crunching those scripts
Part 8 – SP 2010: Developing for performance Part 8 – Control that ViewState

Part 5 (this article):
In SharePoint 2010 (and 2007 for that matter) there’s a few objects in the API’s that requires your special attention in order to behave properly. If you do not consider the disposal patterns and rules set forth, your application may very well suffer performance issues. In this article I will briefly touch upon the subject of disposing your SharePoint objects and foremost enlighten how important it is to dispose the objects!

What does Disposing mean and why is it important?

When I deliver training, there’s always the question about why disposing is important. In SharePoint there’s valid grounds for saying it’s important to dispose, more than just saying "It’s best practice". If you don’t properly dispose some of your objects in SharePoint you’ll quickly face performance issues since those objects don’t get caught and disposed by the Garbage Collector in a timely fashion as most other objects do in .NET.

What is a "dispose pattern"?

http://en.wikipedia.org/wiki/Dispose

The Dispose Pattern is the approach used to properly dispose and clean up the resources you’re using in your projects when programming in .NET (or other runtimes). Normally there’s an automatic garbage collector doing the cleanup for you – but in certain scenarios (like the ones described later in this article), you’ll need to manually dispose your objects.

IDisposable

In Microsoft .NET when an object inherits from the IDisposable interface it means that the Garbage Collector will call the .Dispose() method of that object when it’s no longer used. The Dispose() method in turn calls the Close() method which generally means you should call the .Dispose() method instead of the .Close() method to make sure the objects are properly disposed. Keep reading to see why this is so important!

Why is manual disposal really, really important in SharePoint?

Some of the objects you’re working with heavily in the SharePoint object model (for example the SPWeb and SPSite) are mostly using unmanaged code – and since the managed part of the code is quite small it doesn’t leave a large memory footprint and hence the Garbage Collector don’t necessarily dispose of that object – which means that it’ll be occupying server resources for a longer time if you don’t manually dispose of those objects.

What happens if I forget to dispose?

There’s several things that you may notice in your applications if you’ve implemented a solution that are not properly disposing their objects.

  • Memory consumption.
    • The consumption of your server memory may peak and the worker process (w3wp.exe) may consume a lot more memory than it would normally have to consume.
  • Application Pool recycling.
    • If the worker process consumes too much memory, it’ll recycle the application pool.
    • If you’ve got an underperforming application causing overwhelming  memory consumption the Application Pool will recycle more often.
  • Performance issues!
    • Slow response times
    • Timeouts
    • Unexpected errors
  • Headache
    • User headache
    • Support headache
    • Admin headache
    • Developer headache (ultimately)

In other words: Make sure you’re properly disposing your objects at all times!

Sandboxed Solutions and Resource Usage – Think about dispose patterns!

If you’re developing applications for the Sandbox in SharePoint 2010 (User Code Solutions / Sandboxed Solutions) you may be aware of the resource point system that will limit your application’s usage of resources on the server. This is a great way to keep the admins calm and developers keen on producing quality code.

A thing to note is that if you don’t correctly dispose your objects they will consume more server resources which in turn would lead to the resource points increasing. If the resource usage reaches the limits set forth by SharePoint for a sandboxed solution – it’ll deactivate it.

In other words: Make sure you’re properly disposing your objects at all times!

Let’s visualize the performance problem!

Okay, so now that I’ve got your attention – let’s do a quick performance test to see how the process handles the memory if we create the same application with and without disposal patterns in SharePoint.

I created a simple application that will work heavily with the SPSite and SPWeb objects on one of my servers. After hooking up a performance counter and monitoring the memory consumption repeatedly during a few hours of repeated execution it was easy to line down a conclusion which you can see in the chart below.

Performance summary

The following chart displays the same application with and without implementing the dispose patterns in a SharePoint 2010 execution environment.

Zimmergren Sample Memory Consumption Chart

You can see by the results of the two applications above that when we’re properly disposing our objects there’s a notable difference in the performance in our application – and hence the overall server resource usage.

In other words: Make sure you’re properly disposing your objects at all times!

How to: Implement dispose patterns in your SharePoint code

At this point we know it’s very important to dispose our objects in SharePoint – let’s take a look at how we can do that properly and what tooling and guidelines we can use to help us in this important quest!

Approach 1 – Manually calling Dispose()

The absolutely most general and simple approach to dispose your objects is to simply call the .Dispose() method of your objects:

 SPSite  site = new  SPSite ("http://zimmergren/");
 // Do stuff 
 site.Dispose();

Approach 2 – Encapsulating the statement in a using() block

A more common approach is to encapsulate the code in a using-block where the object will be automatically disposed when we’re reaching the end of our block.

     using (SPSite  site = new  SPSite ("http://zimmergren"));
     {
         // Do stuff 
     } 

Approach 3 – Utilize a try/finally block

Whenever you’re expecting to catch an exception or somehow might stumble onto exceptions and need to handle them – a better approach for disposing is to create a try-finally block and dispose the object in the finally-block.

Sample 1: Without exception handling

     SPSite  site = null ;
     try 
     {
         site = new  SPSite ("http://zimmergren");
         // do stuff 
     }
     finally 
     {
         if (site!=null ) site.Dispose(); 
     }

Sample 2: With exception handling

     SPSite  site = null ;
     try 
     {
         site = new  SPSite ("http://zimmergren");
         // do stuff 
     }
     catch (Exception  ex)
     {
         // Handle the exception 
         // Possibly send it to the logs 
     }
     finally 
     {
         if (site!=null ) site.Dispose();
     }

SharePoint 2010 Logging information: http://zimmergren.net/archive/2011/01/17/sp-2010-developing-for-performance-part-4-logging.aspx

Approach 4 – A mix of the aforementioned approaches

In some scenarios it might be a necessity to mix the aforementioned methods for disposing.

     using (SPSite  site = new  SPSite ("http://zimmergren"))
     {
         foreach (SPSite  oSite in  site.WebApplication.Sites)
         {
             try 
             {
                 // Do stuff 
             }
             catch (Exception  ex)
             {
                 // Log and handle exceptions 
             }
             finally 
             {
                 if (oSite!=null ) oSite.Dispose();
             }
         }
     }

Using SPDisposeCheck.exe to help us check for issues

It’s one thing to be pro-active and think about the dispose patterns when you’re developing your applications – but sometimes you can’t cope for every scenario in your complex code. Don’t worry though – you’ve got one of my best friends to help you out with that – the SPDisposeCheck.exe tool that Microsoft released to check for disposal problems.

Download and install it

There’s a new version of the popular dispose-check tool for SharePoint called SPDisposeCheck. You can find it here: http://code.msdn.microsoft.com/SPDisposeCheck

Grab your copy of the tool and hang on tight for the ride!

Configure it

When you’ve installed the tool, you can see a new menu option in the "Tools" menu:

image

Clicking the "SharePoint Dispose Check" menu item will bring up the SPDisposeCheck configuration menu like this:

image

In this dialog you can configure how the tool should behave, and if it should execute after each build. What’s even cooler is you can choose how to treat the problems.

When you’re building your Visual Studio project, the SPDisposeCheck will perform a post-build command (if you’ve ticked the Execute After Build checkbox) – and you’ll see the output directly in your Error-window:

image

Tip!
Always have this tool installed, and every now and then run the SPDisposeCheck to make sure your code is properly disposing your objects. Otherwise it’ll warn you like in the picture above :-)

False positives

When it comes to checking for dispose problems or leaks with this tool, it can sometimes give you something you’d refer to as "a false positive". What that generally means is that although the tool might report a problem, it really isn’t.

Ignoring reports

Sometimes with the SPDisposeCheck tool you’ll get quite a bunch of "false positives" reported, or for whatever other reason you’d like to ignore certain error messages from the SPDisposeCheck tool – you can do that by implementing the SPDisposeCheckIgnore attribute (available as source code in the SPDisposeCheck installation folder).

The following code snippet is taken from the "SPDisposeCheckIgnoreAttribute.cs" file in the SPDisposeCheck installation folder. Add this code to your project (you can change the namespace..):

 using  System;
 namespace  Zimmergren.SP2010.DisposePatterns
 {
     public  enum  SPDisposeCheckID 
     {
         // SPDisposeCheckIDs. 
         SPDisposeCheckID_000 = 0,  
         SPDisposeCheckID_100 = 100,
         SPDisposeCheckID_110 = 110,
         SPDisposeCheckID_120 = 120,
         SPDisposeCheckID_130 = 130,
         SPDisposeCheckID_140 = 140,
         SPDisposeCheckID_150 = 150,
         SPDisposeCheckID_160 = 160,
         SPDisposeCheckID_170 = 170,
         SPDisposeCheckID_180 = 180,
         SPDisposeCheckID_190 = 190,
         SPDisposeCheckID_200 = 200,
         SPDisposeCheckID_210 = 210,
         SPDisposeCheckID_220 = 220,
         SPDisposeCheckID_230 = 230,
         SPDisposeCheckID_240 = 240,
         SPDisposeCheckID_300 = 300,
         SPDisposeCheckID_310 = 310,
         SPDisposeCheckID_320 = 320,
         SPDisposeCheckID_400 = 400,
         SPDisposeCheckID_500 = 500,
         SPDisposeCheckID_999 = 999 
     }
 
     [AttributeUsage (AttributeTargets .Method | AttributeTargets .Assembly, 
         Inherited = false , AllowMultiple = true )]
     public  class  SPDisposeCheckIgnore  : Attribute 
     {
         public  SPDisposeCheckIgnore(SPDisposeCheckID  Id, string  Reason)
         {
             _id = Id;
             _reason = Reason;
         }
 
         protected  SPDisposeCheckID  _id;
         protected  string  _reason;
 
         public  SPDisposeCheckID  Id
         {
             get  { return  _id; }
             set  { _id = Id; }
         }
 
         public  string  Reason
         {
             get  { return  _reason; }
             set  { _reason = Reason; }
         }
     }
 }
 

Once you’ve done that, you can use the attribute on your methods and assemblies to tell them to ignore that specific item.

Example usage of the SPDisposeCheckIgnore attribute:

         [SPDisposeCheckIgnore (SPDisposeCheckID .SPDisposeCheckID_110, 
             "False Positive, nothing to see here, move along!" )]
         private  static  void  MyAwesomeMethod()
         {
             // Your method code with false positives 
         }
 

What if I’m an awesome coder already?

Too many times have I encountered problems in projects due to not properly checking for memory leaks.

Better safe than sorry. That’s all I’m going to say about that :-)

Summary & Links

What we’ve learned from this article is that you should always keep in mind how you handle your objects in your code – and especially when it comes to the SharePoint objects that are invoking unmanaged code like the SPWeb and SPSite objects (to name two common ones).

Make sure you’ve downloaded the latest version of the SPDisposeCheck tool to get the aforementioned fancy integration into Visual Studio 2010. It’s pretty awesome indeed!

Links / Resources

Enjoy!