Saturday, August 18, 2012

Deploying WSP Files with PowerShell - Part 1

When I started with SharePoint 2010 development, WSP (Web Solution Package) deployments on dev box was thought to be a trivial job; like selecting "Deploy Solution" from the Build menu in Visual Studio, as simple as that.


But later on, the question that started bugging me was; how do I get my WSP files to SharePoint instances running on QA and Production servers? The thought that crossed my mind was to point the Visual Studio 2010 to the remote SharePoint server and get it done, but later only I came to realize the fact that Visual Studio 2010 won't allow you to deploy SharePoint solutions to any remote servers expect your local machine. The Good news is, starting with VS.NET 2012 this restriction is not longer there, and you can deploy solutions to SharePoint 2013 running on a remote box.


After some search, I zeroed in on the definite solution what I was looking for, which was nothing but PowerShell. With SharePoint 2010, Microsoft had deployed this new scripting platform as an alternative to STSADM (a command-line administration tool for Office SharePoint Server 2007 servers and sites). With PowerShell, Microsoft had come up with a unified scripting platform covering the entire spectrum of Microsoft Server products, there by allowing devs, admins and IT Pros to leverage a common scripting platform for automating the day to day tasks. 

The hearsay is that, Microsoft is planning to bump off STSADM from future releases of SharePoint; but at that time of writing, STSADM is still available with SharePoint 2013 Preview, don't know whether Microsoft might drop off it in RTM release, well wait and see.

Solution:

So lets get started and deploy the WSP file using PowerShell; before we start off, make sure the all the points mentioned in the pre-requisites are in place before move on.

Prerequisites:
1. The commands are to be executed on the machine where SharePoint is installed.
2. You must be a member of the Administrators group on the machine which you are to execute the Windows PowerShell scripts.
3. Must be a member of SharePoint Farm Administrators group account
4. db_owner of Content DB that of Central Admin WebApp
5. db_owner of Config DB
6. db_owner of Content DB that of WebApp you wish deploy the solutions against
7. Copy the WSP files to be deployed, on to SharePoint server
8. The following widows services should be up and running

    - SharePoint 2010 Administration
    - SharePoint 2010 Timer



Failing to start the SharePoint Administration service will cause to throw an error while Installing solution, with the error text "Admin SVC must be running in order to create deployment timer job".

Lets begin with how to deploy a feature, scoped to Site Collection containing assemblies to be deployed to GAC. The process involves three steps.

Step 1: Add solution
Step 2: Install\Deploy solution
Step 3: Activate features


1. To perform the above steps, open SharePoint PowerShell Console on SharePoint Server. Make sure you are running PowerShell under a user having Farm Admin privileges, else you will encounter an error stating that

"The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered"







2. First of all we are going to Add the solution; for that key in the following script and press enter, which will add the WSP solution to SharePoint farm.

Add-SPSolution -LiteralPath "<ENTER ABSOLUTE PATH TO WSP FILE>"


if everything goes well, you should see the deployed column with "False" status. 

Internals: In this step, an entry is made to the SharePoint farm ConfigDB. This particular step can also be achieved using the Object model, however the same isn't possible through Central Admin UI.


3. Next we need to Install the solution, for that key in the following script and press enter.


Install-SPSolution -Identity "<NAME OF WSP FILE>" -WebApplication <URL TO WEB APP> -GACDeployment



Note: The -GACDeployment switch is required only if the WSP file contains .NET assemblies (.dll) which needs to be deployed to GAC, else this can be omitted.


Internals: In this step, the WSP file is unpacked and copied to the respective locations. This particular step is executed by Timer Job.

4. Next we need to see whether the solution is deployed or not. For that key in the following script and press enter.


Get-SPSolution "<NAME OF WSP FILE>"


you should see the "Deployed" status as True, which means the solution was successfully deployed by the Timer Job; if yes, proceed to step 5. 

If not the possible cause could be, either of the following.

- The "SharePoint 2010 Timer Job" windows service isn't running
- Timer job might be busy with some long running tasks.

If you wish to see whether a timer job has been scheduled for this particular solution, key in the following script


$solution = Get-SPSolution -Identity:"<NAME OF WSP FILE>"
Write-Host $solution.JobExists

If you see True, then the job is in queue and just need to wait for the timer to take up your request. If False, either it could be because of the timer had already completed the job or could be some other reasons.

5. Next is to activate the feature, before that we need to get the name or GUID for this feature; for that key in the following script and press enter



Get-SPFeature | Where-Object {$_.DisplayName -like "*<FILE NAME>*" }



Copy the value listed under the ID field. In the scope field, you could see that this feature is scoped to Site Collection, that why the value "Site" is displayed.

6. Finally we are at the final stage of deployment, that is feature activation. For this key in the following script and press enter.


Enable-SPFeature -Identity "<PASTE GUID VALUE>" -URL <PASTE SITE URL>



If everything went well, you are done with deploying the WSP to SharePoint site using PowerShell.

Closing Note:

On the contrary, developers tends to shy away from these kind of arcane stuff because of the fact that, its difficult to get hold of the syntax and beyond that its a time consuming and tedious job. So a lazy programmer like me always wants to gets things done in a jiffy by leveraging scripts\tools available on the Internet. To get a hold off on, how SharePoint deployments work under the hood, I thought of getting my hands dirty by writing the PowerShell scripts by hand.


My thought went in a different direction, what if a project had hundreds of WSP files? am I going to do this one at a time like this? if so, definitely its going to be an overkill, so there should be some way to make the life of dev\admin better, right! I came to stumble upon this cool PowerShell script by Allan Merolla, which takes up all the WSP files contained in a folder and processes them one by one, cool isnt it? but the down side with this script is, it wont do any logging or feature activation stuff, which needs to be handled manually.


Happy WSP Deployment !!!




Sunday, June 17, 2012

SharePointKochi User Group Meet v1

The maiden session of KochiSharePoint User Group met at Hexis Labs on Saturday, 16th June 2012. The User Group (UG) is formed with an objective to keep up with the burgeoning developments in Enterprise Collaboration space, there by providing a platform to share and learn the technologies and products associated with it; the UG also gives special emphasis on SharePoint, the flagship product from Microsoft on enterprise collaboration vertical. 


The session started off with stimulating talk by Praseed Pai, the far-famed Software Consultant and Solutions Architect, he is also an avid blogger and speaker at many tech forums including K-MUG. He started off the session by talking about the changing landscape of custom software development which is giving out in favour of packaged software solutions in the enterprise space. He highlighted the some of key features required by every enterprise software and showed how Microsoft SharePoint features aligns with those. He concluded the talk by  mentioning how .NET developers could take their career to the next level with SharePoint, by leveraging their existing ASP.NET skills and added how enterprises could save themselves from the phenomena of "Reinventing the Wheel" by adopting SharePoint as their Collaboration platform thereby cutting down risk associated with custom development.

The next session was by Abraham Peter, a senior SharePoint developer working for Ernst & Young. His session titled "Why & How SharePoint", covered SharePoint from business standpoint equating the six pillars of SharePoint with real life business scenarios and pointed how business could seamlessly streamline their operations using SharePoint. Next he covered how  different variants of Document Management features of SharePoint how they stack up each other. In the last slides he covered how to start off with SharePoint development and various offerings by Microsoft and other third party providers to set up SharePoint development box. Later he demoed the cloud version of SharePoint development services from CloudShare.com

The final session was by Shalvin PD a renowned independent consultant and trainer in .Net and SharePoint technologies with years of experience. You can find him speaking at prominent tech events like Kerala Microsoft User Group, Community Tech Days and others. He started off with a straight dive into SharePoint by demoing how to provision some of the key features covered in the six pillars of SharePoint including Sites, Content and Communities. He covered a good ground on developing SharePoint solutions with Visual Webparts, Sandbox Webparts and covered Central Administration module there by giving the audience a feel of SharePoint development and its features.

We had a adhoc session by Ms.Nimisha, a SharePoint developer from Orion, she briefly covered what all to watch out for especially for developers coming to SharePoint from ASP.NET background. Her session covered topics including the changes that SharePoint makes when installing it to a machine, the SharePoint site hierarchy, sandbox solution and its relevance and many more. Her initiative to share her experiences was well appreciated by the UG.

The adhoc session was followed by the QA session, which was then followed by a self introduction of each and every member in the audience. It was exciting to see people attending the UG not just from development background but also from IT infrastructure services and academia too. 

The SharePointKochi UG hosts would like to specifically thank each and everyone of you who turned up for the meet and made this UG initiative a success and thanks to those who wished to attend but couldn't turn up because of personal reasons. A special word of thanks to Hexis Labs for arranging the Infrastructure without which the event would have been made possible. 


Thanks to God almighty.




Tuesday, January 10, 2012

Descriptive Text for Enums

While working on an windows application, I came to across an interesting problem, which is of trivial in nature. I had a drop-down (combo box) control which was populated with field names from an Enum, which is acting as a container for designation values. The problem with Enums were, the field names can't contains spaces, where as the actual designations did contain spaces. This made the end users feel a bit odd, the way which values were listed in the drop-down control. So putting some thoughts on this, I came up with a solution, which did hit the bulls eye. So here's how I did it.


First I need a container to hold this descriptive text, for this an attribute class called "EnumDescription" was defined to hold the string
[AttributeUsage(AttributeTargets.Field)]
public class EnumDescription : Attribute
{
    public EnumDescription(string Value)
    {
        if (!string.IsNullOrEmpty(Value))
            Description = Value;
        else
            throw new NullReferenceException("EnumDescription not provided");
    }

    public string Description { get; set; }
}

Next I started decorating the enum fields with the previously created attribute class "EnumDescription"; this allowed me to pass descriptive text to the enum fields, which helped me get past the .NET enum field naming contsraint.
enum Designations
{
 [EnumDescription("General Manager")]
 GeneralManager = 1,

 [EnumDescription("Project Manager")]
 ProjectManager,

 [EnumDescription("Technical Manager")]
 TechnicalManager,

 [EnumDescription("Group Project Manager")]
 GroupProjectManager,

 [EnumDescription("QA Manager")]
 QAManager,

 [EnumDescription("Accounts Manager")]
 AccountsManager,

 Designer
}
Next step of the puzzle is to read the string contained in the custom attributes. For this a simple custom attributes reader was implemented, but when I ran a unit test against the code snippet, it failed. The reason being, some enum fields doesn't require custom attributes as the designation names they represent didn't contain spaces. The code snippet I had written, now need to deal with enum fields with and without custom attributes, i.e; the catch here is to return the value contained in the EnumDescription, if the attribute is defined, else the field name itself should be returned.

public static string GetText(Enum e)
{
 string ReturnVal = string.Empty;
 EnumDescription oEnumDescription = null;

 if (e != null)
 {
  FieldInfo oFieldInfo = null;
  oFieldInfo = e.GetType().GetField(e.ToString());

  if (oFieldInfo != null)
  {
   object[] CustomAttribs = oFieldInfo.GetCustomAttributes(typeof(EnumDescription), false);

   if (CustomAttribs != null && CustomAttribs.Length >= 1)
   {
    oEnumDescription = (EnumDescription)CustomAttribs[0];
   }
  }

  if (oEnumDescription != null)
   ReturnVal = oEnumDescription.Description;
  else
   ReturnVal = e.ToString();
 }

 return ReturnVal;
}


Now the problem of getting descriptive text is sorted out. The next piece of the puzzle is how to set enum value based on the custom attribute text value? for this a SetText method was written, which will accept the value to be set, either based on the description or on the field name.

public static T SetText<T>(string Value) where T : struct
{
 T ReturnVal = default(T);
 EnumDescription oEnumDescription = null;
 bool DescriptionFound = false;

 if (!string.IsNullOrEmpty(Value))
 {
  //iterate through all the enum fields and check for the given Value in the EnumDescription
  foreach (FieldInfo fld in typeof(T).GetFields())
  {
   object[] CustomAttribs = fld.GetCustomAttributes(typeof(EnumDescription), false);

   if (CustomAttribs != null && CustomAttribs.Length >= 1)
   {
    oEnumDescription = (EnumDescription)CustomAttribs[0];

    if (oEnumDescription != null &&
     oEnumDescription.Description.ToLower() == Value.ToLower())
    {
     DescriptionFound = true;
     ReturnVal = (T)Enum.Parse(typeof(T), fld.Name);
     break; //we found the matching field so exit the foreach loop
    }
   }
  }

  //If no attribute with the given text was found, look for field names
  if (!DescriptionFound)
  {
   foreach (FieldInfo fld in typeof(T).GetFields())
   {
    if (fld != null &&
     fld.Name.ToLower() == Value.ToLower())
    {
     DescriptionFound = true;
     ReturnVal = (T)Enum.Parse(typeof(T), fld.Name);
     break; //we found the matching field so exit the foreach loop
    }
   }
  }
    
 }

 if (!DescriptionFound)
  throw new ApplicationException("Invalid description or field name: " + Value);

 return ReturnVal;
}

with these two methods in place I can now access and assign Enum descriptive text to Enums.
Here's a the code snippet which I used to test my two methods.
class Program
{
 static void Main(string[] args)
 {
  Designations SelectedDesig;
  string Value;

  SelectedDesig = Designations.GeneralManager;
  Value = EnumStringReader.GetText(SelectedDesig);
  Console.WriteLine("GetDescription: " + Value);

  SelectedDesig = EnumStringReader.SetText<Designations>("Designer");
  Console.WriteLine("SetEnum from Description: " + SelectedDesig);
 }
}
You can download the sample C# project from this link.

Sunday, August 21, 2011

K-MUG Session

Yesterday I came to attend the monthly user group meet organised by Kerala Microsoft User Group(K-MUG) held at Orion. Being a technology enthusiast I always enjoy being part of such tech communities, which really helps one to keep posted with the current developments in and around the developer arena; moreover I see events like these as venue to meet and network with like minded professionals in the industry as well.

The session kickstarted with "Design Patterns in .Net" by Yanesh Tyagi. He started by discussing on Gang Of Four patterns, which was well covered by backing with real time scenarios on when and where to apply these patterns. The best part of the session was the code demo, where he showed patterns in action with sample applications using C#. While discussing the "Null Object" pattern, a question turned out from the audience like can a line be drawn between a Null Object pattern and a Monad.The session was well delivered and shed insight on how the GoF patterns can be put to use for writing better libraries and frameworks. 

The second session was by me, which was on "Profiling & Instrumenting .NET Applications". The talk started with a brief overview on the types of software profilers, which was followed by a demo on profiling .NET applications with CLR Profiler, Performance Monitor and the VS.NET 2010. Some of the common caveats which could lead to memory leaks in .NET applications were also discussed during the session.

While the final session was on "JavaScript MVVM in ASP.NET with KnockoutJS" by Shiju Varghese. The presentation started with why one should use KnockoutJS (A JavaScript library by Steven Sanderson's a Program Manager for Microsoft), the talk moved on to discussing its technology\browser agnostic nature and the minimal footprint in of terms of file size. Thereafter he demoed a simple web application written in ASP.NET. The demo application implemented two way data binding at the client side using KnockoutJS. I was amazed to see how easy it was to implement such a kind of functionality using KnockoutJS, with very minimal lines of code. I would say every web developer should give it a try.

If you wish to present a topic which you feel like worth sharing, then please feel free to drop in a mail to admin@k-mug.org


Tuesday, August 2, 2011

Where are the Sandbox Assemblies Stored?

You might have heard that, SharePoint farm solutions are always deployed to GAC, while sandbox webpart solutions are deployed to 'Solution Gallery' at the site collection level (under '_catalogs/wp' url). So the question is, where does the assemblies of a sandbox solution gets deployed to? Initially my assumption was that, it was getting deployed to content DB. After searching on this, I came to stumble on this insightful article which briefs on this very same question. In the article the default location for sandboxed assemblies is given as  'C:\ProgramData\Microsoft\SharePoint\UCCache'. The author specifically mentions that, this path is configurable by admin. So don't get confused, if you don't find any assemblies in the above location when the sandbox code is executing.

At last, got my query resolved.

SharePoint Developer Arsenal

When I started with SharePoint 2010 development, I had to turn to net for solutions from time to time on tasks like, writing CAML queries free of syntax errors, SPMetal's limitations in handling certain fields, developing sandboxed visual web parts, accessing SharePoint webservices using jQuery, Powershell scripts et al. While all these searches ended up with a handful of freeware SharePoint utilities, which had substantially improved my productive time. Thanks to those who were generous enough to share their custom solutions with the SharePoint community through CodePlex and Visual Studio Gallery. Here's the list of utilities and libraries, which I believe as must to have for every SharePoint developer.


Visual Studio 2010 SharePoint Power Tools - This tool pack is definitely a complement to the existing set of SharePoint templates shipped with VS.NET 2010. With this power pack, you could develop sandbox solutions using visual webpart instead of the standard Webpart (non-visual). This tool pack shows you errors during compilation phase itself, while trying to access a class or a method restricted in a sandbox environment. This feature however is accessible if you refer the SharePoint assemblies located in  {SharePointRoot}\UserCode\assemblies\Microsoft.SharePoint.dll

Community Kit for SharePoint - I would say this is the best utility for SharePoint 2010 developers available in CodePlex. With version 2.0, new features has been incorporated like WSPBuilder conversion tool (WCT), Creating webparts with Contextual behavior and many more. 

Flash(FLV) Video Player webpart - If you are searching for a webpart to play  those FLV formats on the browser, then this one should fit the bill. This webpart uses an opensource flash video player to play the FLV files.

SharePoint Manager 2010 (SPM) -  An object model explorer for your SharePoint site. This one is a great time saver when compared to Central Admin as this helps you to easily navigate to the object you are looking for with few clicks. It gives you much better view of the AppPool and IIS settings. You could even view things like items in the sites recycle bin, quota settings and more. A hearty thanks to the author 'CARSTEN KEUTMANN' for contributing such a great tool.

Useful Custom Workflow Activities - This tool pack provides a set of predefined custom workflow actives like Sending EMail with HTTP attachments, User Info Lookup and more. At the time of writing a new workflow activity named 'Send EMail Extended' was released, which gives you the option to specify the senders email address while sending out emails, isn't that cool?

ILoveSharePoint - A collection of utilities for SP 2007 and SP2010. This pack provides webpart for executing PowerShell scripts from the web UI.

SPServices - Ever had to call a SharePoint webservice from Javascript\JQuery? If yes, then this is the right tool for the job. The SPService is a jQuery library acting as a wrapper for the Sharepoint webservices. The best part with this library is that, it is pretty well documented with samples.

PowerShell Heaven - SPoshMod an easy to use PowerShell wrapper scripts for developers and admins. PowerShell script snippets used by admins and developers for their day to day tasks like IIS manangement, farm management, solution deployment etc. 

SharePoint 2010 FBA Pack - Forms based authentication pack for SharePoint 2010. This includes web parts for registering users, changing passwords and password recovery. It includes tools for managing users and roles and for approving registrations. 

Fusion Charts for SharePoint -  A set of charts for presenting data in 2D and 3D representations. The supported chart types include, bar charts, pie charts, area charts, stacked charts, line charts and more. The chart uses XML format as input and supports data export functionality in CSV and XML formats. Here's a step by step instruction on how to install for SharePoint 2010.

ULSLog Viewer - A log viewer for viewing Sharepoint logs contained in the SharePoint Root\Logs. Incidentally there's another ULSLogViewer from Microsoft too, which provide similar functionality.

LINQ to SharePoint DSL Extension for Visual Studio 2010 - An enhanced library for generating and mapping entities for using with LINQ to SharePoint.

CAML Intellisense - Memorizing CAML structs\syntax is not a big deal, but getting intellisense support is going make the life easier for any SharePoint developer. The good news is that, this package provides rich support while writing CAML queries along with small description on what the method\property is all about.


Click here to browse through all of the SharePoint projects hosted at CodePlex. You can also browse through the solutions hosted at "Visual Studio gallery" targeted towards SharePoint developers.

Saturday, July 30, 2011

SharePoint 2010 TroubleShooting Tips

The saying goes 'to Err is human', with this in mind Microsoft had made commendable improvements on the instrumentation side with SharePoint 2010, by shipping a slew of features and tools for developers and admins as well. Now the task of uncovering those mysterious bugs and performance bottlenecks, inadvertently got in during the development phase, is a breeze. Let me show you how these new features\tools is going to save you from the intricacies of debugging hard to find bugs in SharePoint 2010. Here I will be discussing on the following tools\features.


1. Developer Dashboard
2. Using SPMonitoredScope with Developer Dashboard
3. Demystifying Correlation ID
4. Using ULSLogViewer and SPDiag.exe
5. WSS_Logging
6. Setting Debug Flag
7. Attaching Debugger during run-time
8. Using SPDisposeCheck

Developer Dashboard
If you are a SharePoint developer moving from MOSS (2007) to SharePoint 2010, you are definitely going to appreciate this new feature called 'Developer Dashboard' introduced with SharePoint 2010. This enables you to have a run down of all the operations in a particular page request, including page execution time, execution time of each HttpHandlers, Web parts, database queries and stored proc names. Here is a screen shot of how the developer dashboard will look like in your browser. In short this feature, allows you to trace down the code\component which is behaving unusual. You can click the image to view in its original size. 

Fig -1

Now let me show you how to get the 'Developer Dashboard' visible on your page. First of all this feature is disabled by default, you have to enable this either through Server Object Model or by using commands from 'stsadm' and 'PowerShell'. 

For those new to PowerShell, let me have a word on this. With SharePoint 2010, Microsoft has associated PowerShell which is a versatile, powerful and extensible command line shell, which goes beyond the boundaries of vanilla command line tools. Moreover the existing 'stsadm' will be replaced by Powershell sometime in future. I would recommend learning PowerShell which will be worth the time and effort, looking at the rich set of features it offers

So coming back to the point, enabling 'Developer Dashboard' in your SharePoint site. First I am going to show you how to get this done with 'stsadm'. Before this, make sure that you are running the Command line window as farm admin.


Stsadm –o setproperty –pn developer-dashboard –pv ondemand 


After executing this command you need to login to a SharePoint site as site collection owner. In the site you should see a new icon on to the top right hand side of the page like the one below. 

Fig -2

Clicking on that should toggle the 'Developer Dashboard' on your current page as shown in Fig-1. You can turn it off with the following 'stsadm' command.


Stsadm –o setproperty –pn developer-dashboard –pv ondemand 


Enable Developer Dashboard using PowerShell:
The following command is a single line command, so make sure to take out the line breaks if you are copying from here.

[Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings.DisplayLevel =
 ([Enum]::Parse([Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel],“OnDemand”));

Disable Developer Dashboard using PowerShell:
The following command is a single line command, so make sure to take out the line breaks if you are copying from here.


[Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings.DisplayLevel =
([Enum]::Parse([Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel],“Off”));

Enabling Developer Dashboard using Server Object Model from .NET Code:


public void EnableDD()
{
  SPDeveloperDashboardSettings DDSettings = new SPWebService.ContentServuce.SPDeveloperDashboardSettings;

  DDSettings.DisplayLevel = SPDeveloperDashboardLevel.OnDemand;
  DDSettings.TraceEnabled = true;
  DDSettings.Update();
}


Disabling Developer Dashboard using Server Object Model from .NET Code:


public void DisableDD()
{
  SPDeveloperDashboardSettings DDSettings = new SPWebService.ContentServuce.SPDeveloperDashboardSettings;

  DDSettings.DisplayLevel = SPDeveloperDashboardLevel.Off;
  DDSettings.TraceEnabled = false;
  DDSettings.Update();
}

SPMonitoredScope 
You could unleash the true potential of 'Developer Dashboard' when used with 
SPMonitoredScope class. This class helps you to monitor performance and resource usage for a specific code block. Say you notice a degradation in performance  in a page containing a webpart developed by your team. After going through the code, you suspect a specific method block which seems like the culprit behind this. In such scenarios you could warp the suspected code block\method inside the SPMonitoredScope class, which will provide you with details on resource usage, performance bottlenecks and on how the component interacts with other components. When you wrap a code block inside the SPMonitoredScope, the statistics are written to the Developer Dashboard and to ULS (Unified Logging Service) logs. So here's how to get that done.

using (new SPMonitoredScope(“CodeMonitor_001”))
{
   SuspectedCodeBlock(); 
}


if you wish to collect the code statistics only when the number of requests exceed than the required number of requests or the time taken to process the requests exceeds the a specified number of seconds; in such scenarios you could use the following overloaded form of the SPMonitoredScope class.


using (new SPMonitoredScope("CodeMonitor_001",
                             TraceSeverity.Verbose,
                             2000,
                             new SPRequestUsageCounter(5),
                             new SPSqlQueryCounter()))
{
    SuspectedCodeBlock();
}


The above code block will flag a critical event, if the request takes more than 2 seconds to complete or the number of requests goes beyond 5.


Correlation ID
Truth to be told, when I started with SharePoint development seeing the screen shot as the one shown below was a nightmare, as I couldn't infer any clues from those error message and even couldn't comprehend what this Correlation ID was? Can you infer anything thing from this screen shot? certainly not! 


So what is this Correlation ID? Well Correlation ID is a GUID token generated for each and every request received by the SharePoint Web Front End (WFE) servers. This ID is passed across to all subsequent operations in response to the request, i.e. if the web request inturn makes a database call, you could trace this Correlation ID in the SQL Profiler too. In short all operations linked to a web request request will have this Correlation ID attached.

So next question is, how to figure out the root cause of an error from the Correlation ID? For this you need to search for the CorrelationID inside the ULS Logs, which is located in the "{SharePoint root}\Logs". Finding the CorrelationID among the huge set of log files may seem like searching for a needle in a hay stack. For this Microsoft is providing a set of tools to help you analyze the ULS logs in a better way. Lets explore those tools to surface the root cause from the Correlation ID.


SharePoint Diagnostic Studio (SPDiag) - This is powerful diagnostic tool to troubleshoot SharePoint 2010 issues. SPDiag diagnosis issues by collecting instrumentation data from ULS logs, Windows Event logs, IIS, performance counters  sharepoint logs, SQL databases and presenting those using preconfigured reports. To find the Correlation ID, first of all you need to download and install the tool from here on to your SharePoint 2010 box. 


1. After this, open the SPDiag application window and create a new project by passing the host name of the sharepoint server. 
2. Provide the farm admin credentials.
3. Take a Full snapshot of the farm.
4. In the search box key in the Correlation ID, which will bring up the message corresponding to the provided correlation id.


The documentation on SPDiag is available here.


ULSViewer - Unlike SPDiag, this utility parses and displays only the ULS log data in a friendly interface which is available as freeware. For your information, this tool is not supported by Microsoft even though its available for download from the Microsoft domain.  In the ULSViewer, open the log file with date when the error occurred, here you will see a column titled 'Correlation' this is where you could spot the CorrelationID. Next to the Correlation column you will find a message column containing the detailed error info.


WSS_Logging - With SharePoint 2010, Microsoft had gone one step further by introducing a logging database, which stores a wealth of instrumentation data on the SharePoint servers and farm. The database reads logs from all front end servers through a timer job. You could also search for the Correlation ID using the following SQL query.


SELECT
          [RowCreatedTime],  
                [ProcessName],  
                [Area],  
                [Category],  
                [EventID],
                [Message] 
FROM
            [WSS_UsageApplication].[dbo].[ULSTraceLog] 
WHERE
            CorrelationId='21AC2020-3AEB-1069-C2DD-08112B30309A'

I hope by now, you should be confident enough to tackle the errors with those cryptic 'Correlation ID'.


If you wish to see the logging improvements shipped as part of SharePoint 2010, here's two articles from the SharePoint escalation team worth reading. Part 1, Part 2.

Debug Flag
Lets look at something simple, which lets you view the error information in the SharePoint page itself, instead of going through the CorrelationID lookup. A word of caution, this should not be done on a production server. Lets see how to set the Debug Flag.


1. Open the directory where you web application is located, which should be "C:\inetpub\wwwroot\wss\VirtualDirectories\". 
2. Open the web.config file.
3. Locate the entry "CallStack", set the value to "true"
4. Locate the entry "CustomErrors", set the 'mode' attribute to 'off'


Now going to the page, where the Correlation ID showed up should display the classical ASP.NET YSOD (Yellow Screen Of Death) with detailed error info.


Attach Debugger:
With VS.NET 2010 you could easily debug the SharePoint features by putting a break point in your code and pressing F5 as you do for a normal asp.net application. But there are scenarios where the code you want to debug might not get executed straightaway like Event receivers, timer jobs etc, in such cases the best approach is to add the following line of code where you wish the set the break point.


Debugger.Launch()


Executing the above piece of code will cause the 'Attach Debugger' window to show up, to which you could attach VS.NET and debug as you do normally.




SPDisposeCheck
During development chances are that you might miss out server objects like SPSite, SPWeb from properly disposing, which could end up hogging the server memory in the log run. Did you know that, each instance of SPSite and SPWeb contains a reference to an SPRequest object that, in turn, contains a reference to an unmanaged COM object that handles communications with the database server. So as to prevent the leftover COM instances from accumulating in the memory, Microsoft had come up with an excellent command line utility for verifying your code against a set of best practices like, whether the code had disposed all  objects implementing IDisposable interface. It also verifies the code for 'Do No Dispose' patterns like certain objects which are internal to SharePoint should not be disposed, like the ones coming under SPContext.Current context. After executing the SPDisposeCheck.exe against your assembly\executable, a report will be generated listing out the issues identified. Chances are that you might find false positives too in the report. 


You can download this utility from here. A Best Practices documentation is available on MSDN, on how to Dispose SP2010 Server Objects.


Its recommended to integrate SPDisposeCheck.exe with VS.NET so as to analyze your assemblies without switching to an external console window. Here's an article that details on how to get this done.



Conclusion
SharePoint being a vast product, brings in challenges for the developer and administrators on effectively extending and maintaining the product in respect to the business requirements. Here's a resourceful document which I came across while browsing, detailing things like how to get most out of SharePoint features and services and how to fix problems that might arise when using SharePoint 2010. Here's the link to the document.