Friday 10 December 2010

Write to the SharePoint ULS log files from your code

Writing to SharePoint's log files has been made very easy in SharePoint 2010.

All you have to du is call SPDiagnosticsService.Local.WriteTrace (located in Microsoft.SharePoint.Administration). First an example:

Microsoft.SharePoint.Administration.SPDiagnosticsService.Local.WriteTrace(
  0,
  new Microsoft.SharePoint.Administration.SPDiagnosticsCategory("ListTimerJob",
                            Microsoft.SharePoint.Administration.TraceSeverity.Medium,
                            Microsoft.SharePoint.Administration.EventSeverity.Information
                            ),
  Microsoft.SharePoint.Administration.TraceSeverity.Medium,
  "The list {0} found",
  new object[] { "ListTimerJob" }
);


Ordered from the most important/critical to the least the TraceSeverity levels can be set to: Unexpected, Monitorable, High, Medium, Verbose, None.
In the same order the EventSeverity levels is: Critical, Error, Warning, Information, Verbose, None.

A description of the levels can be found here: http://technet.microsoft.com/en-us/library/ee748656.aspx

My own log messages shows up in the SharePoint log file now.



I passed "ListTimerJob" as the first parameter to the SPDiagnosticsCategory above - this shows up in the Category coloumn, which makes it easy to make I filter un only my messages.


See also (links to msdn)
Using the Trace Logging API
SPDiagnosticsServiceBase Class

Thursday 9 December 2010

Creating custom Timer Jobs in SharePoint 2010

Lately I had an assignment where I needed to make my first timer job in the SharePoint.
I had a bit of trouble finding a good and strait forward and up to date tutorial, but found one in the end:

Creating Custom Timer Job in SharePoint 2010

Its a simple example of creating a timer job in SharePoint 2010 that adds items to a list each time it runs. Short, step by step and with screen shots. Just the way I like it!

If you later want a more in depth article this looks promissing: http://msdn.microsoft.com/en-us/library/cc406686(office.12).aspx


Restart the SharePoint Timer service
When working with timer jobs you need to restart the "SharePoint Timer" service everytime you have deployed new changes to your code.


I had a frustrating day at work trying to find this information anywhere on Google, but without any luck. Then I asked a colleage (which I probably should have done from the beginning!), and my day was saved!  :-)

Friday 26 November 2010

How to fix the "Could not load file or assembly" error

Say you want to use a third party dll in your project.
You have made a reference to the dll in your VS project, and mayby even included it in your project in a folder. However you are still getting the error: "Could not load file or assembly .... or one of its dependencies. The system can not find the file specified."


The solution to the problem is relatively simple.
You have to include the dll file in the package so it get deployed to SharePoint with the rest of your solution.



Here it goes..
  1. Double click "Package" in the Solution Explorer
  2. Click "Advanced" in the lower left corner of the package designer.
  3. Click the "Add" button in the upper right corner, and choose "Add Existing Assembly".
  4. In "Source Path" click the "..." button and navigate to your dll assembly.
  5. Lastly choose "Global Assembly Cache" as your "Deployment Target".

Monday 22 November 2010

Using the ULS log viewer to search for a correlation id

In my previous post I showed you how to turn on the more usefull yellow-screen-of-death error page instead of the SharePoints standard custom error pages.

However you can get the same detaĆ­led information about the error by searching for the correlation id in the SharePoint log files using the ULS log viewer.


File -> Open From -> ULS

Download and start the ULS log viewer. Now open the log file(s).
You can choose to open an existing log file (File -> Open From -> File) or to open a stream that updates and displays the logs as they occur right realtime (File -> Open From -> ULS).

Normally you want to search for the correlation id. Strangely you can't du that by using the find command in the edit menu. You will have to make a filter on that particular correlation id. A little cumbersome.

Use a filter to search for a correlation id

Now you can see all logentries made on that particular correlation id, and get almost the same (and more) information as the yellow-screen-of-death can give you. But its a cumbersome process.

Details on a log entry

Turn on the yellow errorpage and callstack in SharePoint

Have you also got the very annoying and rather useless error screen saying "Error  - An unexpected error has occurred".



Here is how you can turn this custom errorpage off and display the regular "yellow-screen-of-death" errorpage with a more explanatory errormessage and callstack:

Edit the web.config of your webapplication located in C:\inetpub\wwwroot\wss\VirtualDirectories\80 (80 is the port of which your webapplication is listening. If you have more than one webapplication running on the same server, this may differ).

You will have to change to lines:

1) Turn on the callstack - set CallStack to true in the SafeMode element
<SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">

2) Turn off custom errors - in the customErrors-element set mode to Off
<customErrors mode="Off" />

Save, reload the page, and be happy to see the yellow-screen-of-death page. :-)

 See also Using the ULS log viewer to search for a correlation id