Posts Tagged ‘SharePoint’

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!

SP 2010: Developing for performance Part 4 – Logging

January 17th, 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 4 (this article):
In SharePoint 2010 (well, 2007 too for that matter) you need to think about proper logging in your applications to ensure that any problems, issues or other events are lifted to the ULS Logs (Unified Logging System) – that way the administrators can easily view the logs and track down problems with your applications. In this article I will talk a bit about how you can utilize the logging capabilities in SharePoint 2010.

ULS Logs

The ULS Logs are the main place for SharePoint to output it’s diagnostics information. We will take a quick look at how we can read the logs and obviously how we can write custom logging messages to the logs.

Reading the ULS Logs in SharePoint 2010

In order to read the ULS Logs you’ll need access to the SharePointRoot (14LOGS) folder. But to make the life even easier for us Microsoft released a tool called the ULS Viewer which you can download here: http://code.msdn.microsoft.com/ULSViewer

With this tool you can quite easily read through the logs in SharePoint 2010 without any issues.

There’s plenty of resources on the web about how to use the ULS Viewer, so go take a look at any one of them for some usage-instructions.

Download (docx): ULS Viewer documentation

Writing to the ULS Logs from you SharePoint 2010 application

The other side of the logs are of course writing to the logs. This is not a very hard task to do in SharePoint 2010, and I’ll outline the basic steps to do so here.

Normally I create a new class or at least a method to take care of the logging, and it can look like this:

public static void WriteTrace(Exception ex)
{
    SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
    diagSvc.WriteTrace(0,
        new SPDiagnosticsCategory("TOZIT Exception",
            TraceSeverity.Monitorable,
            EventSeverity.Error),
        TraceSeverity.Monitorable,
        "An exception occurred: {0}",
        new object[] {ex});
}

You can use the aforementioned code by calling the method like so:

try
{
    throw new Exception("Oh no, application malfunctioned! UnAwesomeness!!!");
}
catch(Exception ex)
{
    WriteEvent(ex);
}

It’s not very hard at all – once you’ve done that, you’re all set to kick off your custom applications and just call your custom logging method. Obviously you should create a proper logging class to take care of all your logging in your applications.

Results

Using the ULS Viewer you can easily track down the error messages by filtering on your category (in my case it’s TOZIT Exception)

image

image

Event Logs

Even though the ULS Logs takes care of most of the diagnostics logging today, it might be reasonable to output information into the Event Logs sometime.

Writing to the Event Logs from you SharePoint 2010 application

Just as when you’re doing ULS Logging, you can do logging to the Event Logs. It’s just as simple, but you replace the method "WriteTrace" with "WriteEvent" like this:

public static void WriteEvent(Exception ex)
{
    SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
    diagSvc.WriteEvent(0,
        new SPDiagnosticsCategory("TOZIT Exception",
            TraceSeverity.Monitorable,
            EventSeverity.Warning),
        EventSeverity.Error,
        "Exception occured {0}", new object[] {ex});
}

Results

You can view the logs in the Event Logs on your SharePoint server as you would read any other logs.

image

Can I do more?

There’s plenty of cool things to do with the logging mechanism in SharePoint 2010, so you should definitively get your hands dirty playing around with it.

If you for example want to tag the log entries with your company name, clients name or project name – you can easily change that behavior as well. Take a look at my friend Waldek’s post about it here: http://blog.mastykarz.nl/logging-uls-sharepoint-2010/

Related resources

Summary

As a part of the article series focusing on some general guidelines for performance in your applications, logging is a major player. If you master your logs in terms of monitoring and custom application logging you will quickly come to realize how valuable it is.

This is intended as a starting point for you to get familiar with the logging-capabilities in SharePoint 2010.

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 3 (this article):
In this article I will talk about some different techniques for managing caching in your applications. One of the most important thing for most projects today is to keep the performance to a maximum under heavy load times. Caching helps out in achieving parts of that goal.

Some caching techniques in SharePoint 2010

In SharePoint 2010, there’s a few different ways to cache data. In this section you can read about a few of those approaches. Most of the caching techniques I use regularly as a developer derives from or is a direct usage of the capabilities of the .NET framework.

Output Caching (Configurable)

The concept of Output Caching is something that natively comes with SharePoint 2010, as it builds on and relies on ASP.NET caching techniques. This means that you can configure your SharePoint 2010 site to cache the Pages it outputs. The reasoning behind caching a page is obviously that it takes time to generate the content on any page, and on a heavily accessed site it would be a performance impact to generate a new page on every request – that’s where Output Caching comes in handy.

There’s a few goodies about Output Caching, as well as a few gotchas that you should be aware of before taking the journey of configuring your sites for Output Caching;

  • Positive: Quicker response time for your cached pages, faster to render to the client.
  • Positive: Saves on CPU, since it doesn’t have to re-do all the calculations every time.
  • Positive: You can customize Output Caching by using Cache Profiles.
  • Positive: The caching mechanism can store different versions of the resources cached depending on the permissions for the requesting user.
  • Negative: Caching obviously eats more memory as it needs to cache the pages.
  • Negative: Possibility for inconsistencies between WFE’s in a multi-server farm.
  • Negative: Output Caching is a SharePoint Server 2010 capability.

You can create custom caching profiles for your SharePoint 2010 site which allows you to modify and configure the way things are cached – and what should be cached.

Read more: How to configure Output Caching
Read more: Create a custom cache profile using VaryByCustom event handler

Object Caching (Configurable)

In SharePoint Server 2010 you’ve got the option to use Object Caching as well, which is a mechanism to cache specific page items. This is especially likable if you’re playing around with Cross-List data queries and need to cache the results of such a query.

Read more: Object Caching

BLOB Cache (Configurable)

In SharePoint 2010 you also have something called the BLOB Cache, which is a disk-based caching mechanism that caches resources on the disk. Normally these resources are files served by a web page and are named Binary Large OBjects (BLOB).

Normally the BLOB cache will cache files served by the web request like images and video clips.

In web.config you’ve got something similar to the following line which lets you know what file types are cached. You can of course add or remove file types here:

<BlobCache 
    location="D:BLOB"
path=".(gif|jpg|jpeg|jpe|jfif|bmp|dib|tif|tiff|ico|png|wdp|hdp|css|js|asf|avi|flv|m4v|mov|mp3|mp4|mpeg|mpg|rm|rmvb|wma|wmv)$"
    maxSize="10"
    enabled="false" />

Read more: Configure cache settings for a Web Application (SharePoint Server 2010)
Read more: Configure the BLOB Cache

Caching in code (Programmable)

While we know that there’s pre-configurable caching available in SharePoint 2010 (like the Output Cache and BLOB Cache), there’s obviously still a need to create custom caching routines in your applications.

In order for your custom applications to run efficiently and save on server load, you need to consider the importance of using proper caching in your applications.

For instance, if you’ve created an application that is (on every request) fetching information from a database or a SharePoint list (SPList), do you really need that data to be fetched directly from the source – or could you live with having it cached for a few minutes? If it’s not super-important data we’re dealing with that doesn’t need to be up to date every second and every request – please consider building some caching mechanisms in your applications.

There’s a few different approaches to caching in your custom applications, the most common being the ASP.NET Caching (Read about how you can cache a Web Part).

To be honest, there’s no need to write about all the different code-samples you could use for caching right here, as the guys at Microsoft did an excellent job talking about it in this Best Practice article:

Read more: Common coding issues when working with the SharePoint Object Model

Note that the aforementioned article is for SharePoint 2007 (WSS 3.0) originally, but these techniques still apply.

Summary

In this article you’ve read about a few approaches to make your applications perform better in SharePoint 2010. These techniques are a fundamental part of development and configuration when it comes to playing around with SharePoint and making it behave properly in terms of responsiveness and performance.

I’ve been getting a few requests for talking about some various caching techniques in SharePoint, so there you have it. Go have a read at those links and you should be all set to start the adventure that is caching!

Read more: Planning for caching and performance

Enjoy!

SP 2010: Developing for performance Part 2 – SPMonitoredScope

December 21st, 2010 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 2 (this article):
In this article I will talk briefly about what SPMonitoredScopes are and how you can utilize them in combination with your developer dashboard to get some stats and performance information about your custom applications.

SPMonitoredScope to track performance in your applications

The monitored scope (SPMonitoredScope) is a class you can use to monitor performance and resource usage in your applications by using it in a simple code block.

The pros about using the monitored scopes are of course that you can easily track down and find bottlenecks as well as do some initial performance monitoring while doing your development.

Using SPMonitoredScope

In order to use the SPMonitoredScope you’ll need to add a using statement for Microsoft.SharePoint.Utilities and then wrap your code in a statement like this:

using (new SPMonitoredScope("CallMethod1 Monitored Scope"))
{
    Controls.Add(new Literal {Text = "Awesomeness<br/>"});
}

You don’t need to add any more code than this in order for it to be hooked up.

See the results

So what now, what if I’ve added some monitored scopes to some of my classes – where can I see the results?

Answer: Open up your developer dashboard on the page where your code is executing and pow, you’ll see it right there:

Note: I’ve added three monitored scopes in my code, as seen below named CallMethod1,2,3
image

You can see that the CallMethod3 is the one dragging this page down in my particular case. Easy enough to keep track of the request time for your pages!

You can even nest your scopes

If you’ve got nested method calls and a bit more code (you’re likely to have that), you can of course nest your monitored scopes. There’s nothing to it really, you can instantiate new monitored scopes inside of a monitored scope:

using (new SPMonitoredScope("My Monitored Scope"))
{
    Controls.Add(new Literal { Text = "When the awesomeness is flying… " });
               
    using (new SPMonitoredScope("My Sub-Monitored Scope"))
    {
        Controls.Add(new Literal { Text = "there’s no stopping it! " });

        using (new SPMonitoredScope("My Sub-Sub-Monitored Scope"))
        {
            Controls.Add(new Literal { Text = "<br/>I’m three levels deep!" });
        }

        using (new SPMonitoredScope("Another deep scope"))
        {
            Controls.Add(new Literal { Text = "Rock and rumble, rock and rumble" });
        }
    }
}

Results: Nested scopes (My Monitored Scope)!
image

Limitations

The SPMonitoredScope is NOT available for Sandboxed solutions unfortunately, so this neat trick will not work if you’re planning on deploying to the sandbox.

Summary

This was a short introduction to the SPMonitoredScope class that you can utilize in SharePoint 2010 when doing custom development. The pros with using these monitored scopes is that you can easily track performance of your custom applications using the Developer Dashboard without even hooking up any debugger or external performance monitor to your servers – this is all done and visualized in the web browser upon displaying the Developer Dashboard.

This is a very easy trick to keep an eye out for performance bottlenecks in your applications for SP 2010.

Enjoy!

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 1 (This article):
This is Part 1 of 5 where I will introduce you to the developer dashboard in SharePoint 2010. The reason for the developer dashboard being a key concept in your SharePoint development tasks is the quick and effective manner of which you can start looking for bottlenecks and problems in your installation without launching any additional tools.

SharePoint 2010 Developer Dashboard

The developer dashboard is a perfect tool for anyone who wants a quick way to access information about what goes on while rendering a page in SharePoint. It contains information about Web Parts, events, DB calls and a whole lot of nifty information.

Activating the Developer Dashboard

Developer Dashboard is a utility that is available in all SharePoint 2010 versions, and can be enabled in a few different ways:

  • PowerShell
  • STSADM.exe
  • SharePoint Object Model (API’s)

Using these different approaches is very simple; All you will need to do is use one of the aforementioned methods to activate the dashboards, as described here:

Activate the Developer Dashboard using PowerShell:

$devdash =
Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$devdash.DisplayLevel = ‘OnDemand’;
$devdash.TraceEnabled = $true;
$devdash.Update()


Activate the Developer Dashboard using STSADM.EXE

STSADM.EXE -o setproperty -pn developer-dashboard -pv ondemand

Activate the Developer Dashboard using the SharePoint Object Model

using Microsoft.SharePoint.Administration;

SPWebService svc = SPContext.Current.Site.WebApplication.WebService;
svc.DeveloperDashboardSettings.DisplayLevel =
    SPDeveloperDashboardLevel.Off;
svc.DeveloperDashboardSettings.Update();

Note that in the preceding samples I’ve used a property called "OnDemand". This can be set to the following values:

  • Off (Disables the Developer Dashboard)
  • On (Enables the Developer Dashboard)
  • OnDemand (Enables the Developer Dashboard upon request by clicking the icon in the upper right corner)

If the Developer Dashboard is set to OnDemand, this button appears in the top right corner just next to your login name:
image

As a side note, you can also enable Tracing for your developer dashboard to enable your dashboard to display the asp.net page trace. I will cover this a bit further down.

Reading the Developer Dashboard information

When you click the small icon in the top right corner, the Developer Dashboard will be opened and displayed at the bottom of your page.

Developer Dashboard example:
image

You can see that it has a Green border right now. That generally means it’s loading quick enough not to be a real problem. It can also render Yellow, which indicates that there’s a slight delay and then it could render a Red border which would mean you definitely need to look into it immediately!

So, what information can you read out of this?

Page Request to the left-hand side

You can read out the Page Request, and see what loads and how long it takes to load. This is perfect to use to track down heavy-loading apps or finding out what’s taking so long to render the page;
image

Web Server, Events, DB Queries, Service Calls, SPRequests, Web Part Events on the right-hand side

image

Dig deeper into the SQL DB queries by clicking on the link

If you’d like to get some more in-depth information about what query was shot away to the DB, click the hyperlink corresponding the query you want to find out about and you’ll see something like this;

image

Displaying ASP.NET Trace information

One thing that I absolutely love about this tool is the ability for the tool to enable Tracing (this should be enabled when you enable the Developer Dashboard using the SP Object Model or PowerShell by setting the following flag:

DeveloperDashboardSettings.TraceEnabled = true;

If you’ve done this, and you’ve got the Developer Dashboard enabled – you should see the following link in the bottom left area of the developer dashboard:
image

If you click it, you’ll see the full ASP.NET Page Trace like this (awesome, very very awesome):
image

The beauty about this is you don’t have to go and edit the web.config and enable tracing there – you just click this little button and it’s all done. I love it.

Developer Dashboard Visualizer – Extend your developer dashboard with diagrams

One of my friends Jaap Vossers wrote up a cool functionality to use in conjunction with the Developer Dashboard – Developer Dashboard Visualizer – which is a cool utility if you want to visualize what the rendering process looks like on your page.

If you’ve downloaded this awesome solution, you’ll see a new Site Collection feature that you’ll need to enable:
image

Once that’s done, next time you’ll visit the Developer Dashboard awesomeness it’ll look something like this:
image

Developer Dashboard activation as a Feature

My good friend Wictor Wilén wrote a quick and cool feature to ease the activation of the Developer Dashboard settings (find it here) which allows you to more easily change the settings of the developer dashboard without doing it by typing any scripts or code.

Once you’ve downloaded and installed the solution, you’ll find a new Farm feature that should be activated, which provides some functionality to Central Administration for administering the Dev Dashboard:
image

If you’ve deployed and got this feature activated, head on over to General Application Settings and you’ll see that you’ve got a new header called "Development Settings" containing a link to "Developer Dashboard Settings".

Click it and you’ll see this very awesome page that enables you to configure the Developer Dashboard without actually writing any code!

image

Summary

In this article you’ve read about the Developer Dashboard and how to enable it, and a few extra tips that you can download and install to make your experience with the Developer Dashboard even cooler.

This was part 1 in an article series talking about a few concepts we’ll need to understand in order to properly plan and design for performance in our SharePoint 2010 applications.

Stay tunes for parts 2-5.

Enjoy!

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

Introduction

I’ve been getting some questions lately about modifications to web.config files and SharePoint 2010 – is it possible to automate so that the administrator don’t have to manually edit those files on each WFE?

Yes, that’s the easy answer. This was possible in SharePoint 2007 as well.

In this article I’ll quickly demonstrate how you (using a Web Application feature) can make modifications to web.config automatically.

Please note that this is just a getting-started point which you’ll have to work deeper into to tweak it to your likings for updates and removals from the web.config file.

Automatic web.config changes using a SharePoint 2010 feature

Basically, if you want to get some type of change into the web.config, it oftentimes has to do with adding custom settings or values that you’ll later reference in your applications.

In this scenario, we’ll add the following key into web.config using a Feature Receiver, so we don’t have to do it manually after/before deploying our solutions:

 <add key= "isAwesome " value= "1 " />

So, in order to achieve this programmatically without actually editing the web.config by hand – you could utilize the class called SPWebConfigModification something like this:

 public  override  void  FeatureActivated(SPFeatureReceiverProperties  properties)
 {
     var  value = 1; // We want to set the key isAwesome to the value "1" 

     var  webApp = properties.Feature.Parent as  SPWebApplication ;
     var  mySetting = new  SPWebConfigModification 
     {
         Path = "configuration/appSettings",
         Name = string.Format("add [@key='isAwesome'] [@value='{0}']", value),
         Sequence = 0,
         Owner = "Zimmergren.SP2010.WebConfigModifications.Awesomeness",
         Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
         Value = string.Format("<add key='isAwesome' value='{0}' />", value)
     };

     webApp.WebConfigModifications.Add(mySetting);
     webApp.Update();
     webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
 }

By executing this cute snippet of code you will actually add the setting called isAwesome to the web.config of your web application. (This will be persisted to all the WFE’s of course).

Please note: You will need to tweak your code quite a lot to make the sweet SPWebConfigModification class behave the way you want on activation/deactivation of the feature. This should at least give you a starting point :-)

Summary

In this short introductory article about web.config modifications I’ve talked a bit about how you can automate the process of altering your web.config files without actually opening them on the file system.
Nothing fancy, but a good starting point for those of you who requested some info on the topic.

Enjoy!

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

Introduction

In SharePoint 2010, you’ve got some new capabilities for error reporting and logs. One of the most noted features for me as a developer is that whenever you bump into an error message – you’ll be presented with a correlation ID token.

In this article I will try to explain how you can fetch that token from SharePoint, and also provide a small snippet of code to be able to build your own “Log Searcher Web Part” for your administrators.

Correlation ID, what’s that?

In SharePoint 2010, you get a Correlation ID (which is a GUID) attached to your logs/error messages when something happens. This ID can then be used to lookup that specific error from the logs.

This Correlation ID is used per request-session in SharePoint 2010, and if you are in the process of requesting some information from SharePoint and bump into some problems along the way – your Correlation ID will be the best starting point for searching for what went wrong along that request!

You might get an error message like this in SharePoint 2010 (don’t worry, if you haven’t seen one like this one yet – just hang in there, sooner or later you will.. Winking smile)

image

Search for the log messages based on the Correlation ID token

So if you’ve gotten such an error message and want to find out more information about the actual error and don’t have the time to go around poking in the logs and searching – there’s a few methods you can do this rather easily, as described below.

Using PowerShell to lookup a log message based on Correlation ID token

One of the quick-n-awesome ways to do this is to simply hook up a PowerShell console (SharePoint 2010 Management Shell) and then just write the following command (replace the <GUID> with the Correlation Id):

get-splogevent | ?{$_Correlation -eq "<GUID>" }

This will give you the details about your specific error like this in the console:

image

You might want to be more precise and get more specific details out of your query, then you can try something like this:

get-splogevent | ?{$_.Correlation -eq "<GUID>"} | select Area, Category, Level, EventID, Message | Format-List

This will give you the details about your specific error like this with some juicy details:

image

Finally if you would want these messages to be put into a text or log file instead, you could just add the classic “> C:Awesome.log” after the command like this:

get-splogevent | ?{$_.Correlation -eq "<GUID>"} | select Area, Category, Level, EventID, Message | Format-List > C:Awesome.log

image

On MSDN they have an overvoew of using SP-GetLogEvent which I would recommend!

Using SQL queries to find log message based on Correlation ID token

You can utilize the SQL logs database to fetch the specific error message based on Correlation id as well. In your logging DB (usually called WSS_Logging, but can be called something else if you’ve changed it) there is a view called ULSTraceLog which you can query with a simple SQL query and fetch the results.

I’ve created a query to fetch the items from the logging database like this:

 select 	[RowCreatedTime],  [ProcessName],  [Area],  
 		[Category],  EventID,  [Message] 
 from  [WSS_UsageApplication].[dbo].[ULSTraceLog] 
 where  CorrelationId=< pre>'B4BBAC41-27C7-4B3A-AE33-4192B6C1E2C5'

This will render your results in the SQL Query window like this:

image

This can of course be implemented in a Web Part for your convenience as well (I’ve created a bunch of diag. and logging web parts that I’ve got deployed to Central Admin) that could look like this (this way you don’t need physical access to the ULS logs all the time, but can do a quick lookup from the web part):

image

Get the current Correlation ID by using code

I got this piece of code from my good friend Wictor’s blog post. Thanks Wictor – this made my Logging-project more satisfying!

With the following code you can fetch the current Correlation Id of a request.

Create a method (in my case called GetCurrentCorrelationToken()) to wrap up the functionality of returning the current token like this:

 public  class  CorrelationId 
 {
     [DllImport ("advapi32.dll")]
     public  static  extern  uint  EventActivityIdControl(uint  controlCode,ref  Guid  activityId);
     public  const  uint  EVENT_ACTIVITY_CTRL_GET_ID = 1;

     public  static  Guid  GetCurrentCorrelationToken()
     {
         Guid  g = Guid .Empty;
         EventActivityIdControl(EVENT_ACTIVITY_CTRL_GET_ID, ref  g);
         return  g;
     }
 }

Then from wherever in your code you can simply call it by using this approach:

 protected  void  Button1_Click(object  sender, EventArgs  e)
 {
     Label1.Text = CorrelationId .GetCurrentCorrelationToken().ToString();
 }

(Normally you might want this in a try/catch statement or something like that)

image

Summary

This article should give you (administrator and developer) an insight in how you easily can track the specific errors your users encounter in their/your application(s). If a user gets an error message in SharePoint 2010, they’ll also see the Correlation ID.

If you can train your users to write down that Correlation ID along with a few simple steps on how the error might have been occurring, you’re going to have a much easier way to find the details about that specific error than ever before.

Enjoy!

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

Introduction

In SharePoint 2010, there is a new Service Application called Word Automation Services. This Service Application is used to convert documents from Word to different formats.

Word Automation Services can open about the same formats as Windows Word 2010 can:

  • Filetypes it can open include:
    .docx, .docm, .dotx, .dotm, .doc, .dot, .rtf, .mht, .mhtml, .xml (Word xml)

  • Filetypes it can save as include:
    .docx, .docm, .dotx, .dotm, .doc, .dot, .rtf, .mht, .mhtml, .xml (Word xml), PDF, XPS

In this article you’ll see an example of how you can utilize the Word Automation Services in order to build a custom solution that takes care of converting documents (as listed above) into PDF documents.

Programmatically work with Word Automation Services in SP 2010

Note: You cannot deploy a solution working with the API’s in the Word Automation Services in the Sandbox. Rather you’ll need to target your application as a Farm Solution.

There’s not really a whole lot to it. Just follow along with these few steps and you’ll be fine!

In the API for Word Automation Services you’ll find a few different ways to convert documents including:

In the following example I’ll demonstrate how to use the AddLibrary() method in order to convert the contents of an entire document library into PDF documents! (Yes, that is way awesome)

1. Add the Word Automation Services API reference to your project

The following reference needs to be added to your project:
image

It can be found here:
<path>14ISAPIMicrosoft.Office.Word.Server.dll

2. Add the proper using-statements

 using  Microsoft.Office.Word.Server.Service;
 using  Microsoft.Office.Word.Server.Conversions;

3. Create a job to convert an entire Document Library to PDF’s

First, fetch the WordServiceApplicationProxy object (so we don’t have to hard-code the service app name..). This first line requires the Word Automation Service Application to be added to the default proxy group.

Second, we instantiate a new ConversionJob class and shoots in your WordServiceApplicationProxy as a parameter:

 var  wordAutomationProxy = 
     (WordServiceApplicationProxy )
     SPServiceContext .Current.GetDefaultProxy(typeof  (WordServiceApplicationProxy ));
                 
 var  conversionJob = new  ConversionJob (wordAutomationProxy);

Next we need to specify a UserToken for the job to tell the job under what credentials to run.
We also need to specify a
name for the job.
Finally you can add whatever
Settings you want for your job, I’ve chosen that I want my files to be output as PDF’s.

 conversionJob.UserToken = SPContext .Current.Web.CurrentUser.UserToken;
 conversionJob.Name = "Zimmergren.SP2010.WordAutomationDemo Conversion Job" ;
 conversionJob.Settings.OutputFormat = SaveFormat .PDF;

Next we will simply specify a library where the original .doc or .docx files reside and point out the destination library like this, and start the job by adding it to the timer job queue:

 conversionJob.AddLibrary(origLib, destinationLib);
 conversionJob.Start();

This is the sample code in one snippet

 protected  void  btnConvert_Click(object  sender, EventArgs  e)
 {
     try 
     {
         SPList  origLib = SPContext .Current.Web.Lists[ddlLibraries.SelectedValue];
         SPList  destinationLib = SPContext .Current.Web.Lists["PDFLibrary" ];

         var  wordAutomationProxy = 
             (WordServiceApplicationProxy )
             SPServiceContext .Current.GetDefaultProxy(typeof  (WordServiceApplicationProxy ));
                 
         var  conversionJob = new  ConversionJob (wordAutomationProxy);

         conversionJob.UserToken = SPContext .Current.Web.CurrentUser.UserToken;
         conversionJob.Name = "Zimmergren.SP2010.WordAutomationDemo Conversion Job" ;
         conversionJob.Settings.OutputFormat = SaveFormat .PDF;

         conversionJob.AddLibrary(origLib, destinationLib);
         conversionJob.Start();

         Label1.Text = "Conversion job started!" ;
         Label1.Visible = true ;
     }
     catch (Exception  ex)
     {
         Label1.Visible = true ;
         Label1.Text = "Error:<br/>" ;
         Label1.Text += ex.Message;
     }
 }

So what’s the results?

When you’ve created a new ConversionJob, it’ll be added to the Timer Job schedule to be run by SharePoint. When the job has been run, it can look like this:

Original Document Library, filled with some Word documents:
image

These files will then be converted to PDF’s and put into my PDFLibrary like this:
image

Summary

To get started with the Word Automation Services, you don’t really need to do a lot of coding. Just specify the references, hook up a reference to your service app and create a new ConversionJob and you’re up and running.

This article demonstrated how to convert an entire library of documents to PDF’s with a single click.

Awesome. Enjoy!

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

Introduction

One of the coolest new set of functionality for SharePoint 2010 is the Taxonomies (Term Store, Term Sets, Terms) that you can easily create using the amazing Managed Metadata Manager service application.

In this article I’ll talk briefly about how you can utilize the SharePoint API to programmatically work with Taxonomies and create terms and fetch the terms in your term store. This should give you some nice ideas on how to get going!

A simple example of a taxonomy

Let’s say we’ve got a taxonomy worked out and implemented in SharePoint. It could look something like this (I’m using some made up samples below):

image

So, if you’ve got a taxonomy configured in your Managed Metadata Service Application, you can work with those programmatically – and that’s what this little tip is about.

Work with the taxonomy API’s programmatically in SP 2010

In this article I will talk about some of the basics to get started with taxonomies in SharePoint 2010 programmatically. First, of course, we need to create a new project and add the references for the Taxonomy API.

Preparing for development

First of all, create a new project (In my case, I chose to create a Visual Web Part project).

You’ll need to add the following reference to your project:

image 

You’ll find this reference here:
C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.Taxonomy.dll

Namespaces

So, the first thing we would like to do is to learn how we can read the taxonomies we’ve got in our store. To do this, we utilize Microsoft.SharePoint.Taxonomy.

There’s a few good-to-know classes in this namespace that we’re going to work with:

The above classes are stated in their hierarchically correct order, meaning means that you start out with the TaxonomySession which contains the TermStore, which contains the Groups.. and so on.

Reading the Metadata store (Managed Metadata Service) programmatically

Sample code from my Visual Web Part’s user control (I have a control called tvMetadataTree in the user control):

     public  partial  class  TaxonomyWebPartUserControl  : UserControl 
     {
         protected  void  Page_Load(object  sender, EventArgs  e)
         {
             SPSite  thisSite = SPContext .Current.Site;
             TaxonomySession  session = new  TaxonomySession (thisSite);

             TreeNode  treeNode = new  TreeNode ();
             treeNode.Text = "Metadata Awesomeness" ;
             tvMetadataTree.Nodes.Add(treeNode);

             foreach (TermStore  termStore in  session.TermStores)
             {
                 var  tsNode = new  TreeNode (termStore.Name, null , null , "" , null );
                 treeNode.ChildNodes.Add(tsNode);
                 //treeNode = tsNode; 

                 foreach (Group  group in  termStore.Groups)
                 {
                     var  node = new  TreeNode (group.Name, null , null , "" , null );
                     treeNode.ChildNodes.Add(node);
                     //treeNode = node; 
                     
                     foreach (TermSet  termSet in  group.TermSets)
                     {
                         node = new  TreeNode (termSet.Name, null , null , "" , null );
                         treeNode.ChildNodes.Add(node);
                         treeNode = node;

                         foreach (Term  term in  termSet.Terms)
                         {
                             AddTermSet(term, treeNode);
                         }
                     }
                 }
             }
         }

         void  AddTermSet(Term  term, TreeNode  treeNode)
         {
             var  node = new  TreeNode (term.Name, null , null , "" , null );
             treeNode.ChildNodes.Add(node);
             treeNode = node;

             foreach  (Term  t in  term.Terms)
             {
                 AddTermSet(t, treeNode);
             }
         }
     }

The end result will be a simple TreeView control filled with the Metadata structure from the store, looking something like this:

image

That’s about what you would need to get started with this!

Summary

Alright, so I wrote this small article up due to some students asked me for some taxonomy sample code a while back. I hope everyone enjoys this tip on how to programmatically work with the Taxonomies in SharePoint Server 2010!

As always, enjoy!

Book: SharePoint 2010 Web Parts in Action

August 1st, 2010 by Tobias Zimmergren

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

Introduction

As you might know there’s a whole lot of SharePoint 2010 books coming out quite soon, and some have already been released to the market.

In this article I will highlight one of the books I like in particular, the SharePoint 2010 Web Parts in Action by my good friend Wictor Wilén published by Manning.

As a Technical Reviewer for this book, I’ve been able to read it through and give feedback on it to the publishers and author. This is not a technical review though, this is just my expressed opinion on the book.

Book coverage

This is a developer-focused book, covering a lot of the aspects that you (the developer) needs to get a grip on. If would think this book suits both SharePoint-beginners as it will suit people moving from SharePoint 2007 to SharePoint 2010. However, I would recommend that you have a basic understanding of .NET and C# and how ASP.NET works before digging too deep into SharePoint development – after all, SharePoint builds on top of these technologies :-)

The book consists of three main parts:

  • Part 1: Getting Started
    • Introducing SharePoint 2010 Web Parts
    • Using and configuring Web Parts in SharePoint 2010
  • Part 2: Developing SharePoint Web Parts
    • Building Web Parts with Visual Studio 2010
    • Building the user interface
    • Making Web Parts customizable
    • Web Part Resources and Localization
    • Packaging, deployment and security
    • Troubleshooting Web Parts
    • Web Part caching
    • Dynamic Web Parts
    • External application Web Parts
    • Mobility
    • Web Part design patterns
  • Part 3: Dashboards and Connections
    • Connecting Web Parts
    • Building dashboards

This book will walk you through topics from the basic understanding of Web Parts in SharePoint 2010 to more advanced topics like Web Part design patterns, connectable web parts and building dashboards.

Who should get this book?

So why would someone get this book, you might ask.

First of all, I’m getting it myself – because it’s a great reference to have when you’re a SharePoint developer, that’s for sure!

Part from my (slightly subjective) opinion as to why I would get it, you might want to get it because the following reasons:

  • Pedagogical
  • Good and thorough coverage of the topics
  • Code samples
  • Written by an experienced developer who knows the common problems you might run into