Debugging tips for Sharepoint-go

Source: Internet
Author: User
Tags mscorlib reflector
    • Debugging starts with your first line of code
    • Use trace. Write & Debug. Write
    • Use try... catch... finally
    • Check the SharePoint log files
    • Use all the tools available to you
    • Don't try to guess
    • Re-Produce & isolate the problem
    • Debug on the live Server
    • Debug using a crash dump
    • Further reading



Debugging on SharePoint is much the same as debugging any otherasp. NET application... you can use the same techniques. Here are some ofmy tips for diagnosing & solving your problems...

Debugging starts with your first line of code

As you start writing some code you need to bear in mind that at some point youWillNeed to debug it. this may mean attaching a debugger (easy) or lookingat a crash dump (hard ). this means debugging shoshould always be at theback of your mind when writing any code... ask yourself "when this islive how am I going to find out what has gone wrong? "

In practice this means making full use of the features. net... system. diagnostics, try... catch, exception logging, etc. one thing in participates to remember is that if you do ever have tolook at a crash dump your variable & method names will be in theretoo. the more descriptive they are the easier it is going to be toanalyse the dump. also bear in mind that the less code in each methodthe easier It will be to pin-point the location of the Error. Les Smith has some good points to make about refactoring... I wocould add that refactoring making debugging easier!

Use trace. Write & Debug. Write

This is the first thing I wowould recommend. adding these method callsat suitable places in your code can really help to solve problems. usedin conjunction with debugview these methods can give you great insight to what is happening within your code. personally I use tracewhenever something unexpected or 'out of the ordinary 'occurs, normallyexceptions, But it cocould be anything that may help solve a problem. callto the trace methods remain in release code and so will add an overhead to the code execution. I generally use debugto enable me to see code execution paths during development and asthese do nothing in a release build can be used much more liberally.

Using the trace statements canreally help when you cannot attach a debugger (on a live server) togive you more information of what has gone wrong. if you ensure you usetry... catch... finally blocks throughout your code, using traceto log the exception makes it easy to see where the error has occurred (including the call stack) simply by running debugview on the server.

Use try... catch... finally

When developing ASP. net controls and webparts I like to add try... catch blocks when overriding methods like oninit, onload, createchildcontrols and render. I do this because I don't what the whole page to fail to render simplybecause one control has a problem... if the search box control throws anexception, why shouldn't the rest of the page display? Therefore myrender method wocould look like this...

protected override void render (htmltextwriter writer)
{< br> try
{< br> // do something to render the control, which may cause an exception
}< br> catch (exception ex)
{< br> trace. write (Ex);
trace. writeline (httpcontext. current. request. URL. tostring ();
// maybe render an error message
}< BR >}

This way if an exception is thrown the page will still render... theuser may not get the full functionality of the page, but at least theyget something. essentially wrapping your code in a try... catchblock has a negligible hit in terms of performance and so you shouldlook to using them wherever something may throw, even if you just traceand re-Throw the exception. also you shoshould bear in mind that throwingan exception is a big overhead and shoshould only be used when areal exception has occurred... something you really did not found CT andcannot recover from.

Check the SharePoint log files

These will be located in the/12/logsfolder. the log files contain errors & Messages generated bysharepoint. I have to say that generally they don't give you as muchinformation as you wocould like (sometimes nothing), but occasionallythey point you straight to the problem. either way you shoshould alwayscheck them just in case.

You shoshould also know that You can increase or decrease the amount oflogging SharePoint does in central administration. here you can changeThe verbosity of the logging within different parts of the application. its not the easiest interface to use, but you can increase the numberof messages logged... doing this has solved problems for me in the past.

You change the settings under 'logging and report' in the Operations tab...


Clicking on diagnostic logging shows the following page...

Under 'event throttling 'You can change the verbosity. I have to saythat I normally just set all of the categories to verbose when I have aproblem as it can be difficult to identify the categories which you mayneed to change.

Here you also see the 'trace log' settings with the defaultsettings... generally I like to make these smaller, say 30 files and 5minutes. this makes the log files smaller and easier to view.

Use all the tools available to you

There are a lot of tools out there to help you resolve yourproblems, get to know them, know what they can do and when they canhelp. this is a list of tools I use on a regular basis...

    • Reflector
    • Fiddler
    • Debugview (in fact all of the sysinternals tools)
    • IE Dev Toolbar
    • Xsltmajic
    • Windbg

There are more tools avaiable, what you use depends on the problem you are trying to solve, in particle tools like u2u's caml builder can help you to replicate problems.

Don't try to guess

Don't try guessing what the problem might be and randomly changingyour code to try and fix it (although this maybe a last resort !!!). Attach a debugger, look at the code, trace through the code and find the cause. if youneed to, you can even break the debugger in SharePoint code, it maybeil, but it is better than nothing. reflector can really help in this situation as it gives you a chance to relate the Il code back to C #.

I had an example of this recently, the authoring console was giving me the following error...

No errors were logged anywhere and I couldn't see what cocould becausing the problem, but it was preventing us from authoring. to solvethe problem I attached the. net debugger to the w3wp process, disabled 'just my Code' and set the debugger to break on allexceptions...

I then refreshed the page and broke into the SharePoint code when the exception occurred...

Once in the debugger we can see that we have a nullreference exception thrown in a method called configurationxml () in the consolexmlutilitiesclass. none of these details were logged, but now I know exactly wherethe problem is manifesting itself. now we have the class and the methodwe can use reflector to see what is going on...

Looking at the configurationxml method we can see where a null reference 'may' occur. reading through the Code shows that the most likely suspect is that file2 is being used without checking to see if it is null (digging into the configfilemethod shows us this is a valid return value ). using reflector shows usthat this class is dealing with the configuration files for thepolicles located in the MAS TER pages gallery. A quick check showed thatnone of the XML files in the editing menu folder had un-publishedversions (I still don't know how that happened). publishing those filessolved the problem!

This shows how simple it can be to resolve a problem, even when it is occurring within the SharePoint code base.

Re-Produce & isolate the problem

Most of the time your problem will occur within the complex sequenceof events of a webpage request. you can try to narrow down thepossibilities by executing the same code in a different context. sharePoint actually gives you a really quick and easy way of doing thiswithout having to deploy binaries... in the form of the _ layouts folder.

You can easily create a quick. ASPX page containing the Code whichis causing problems. this you can place in the _ layouts folder to test. in. ASPX page you can add whatever trace, debug or response. write callsyou like to see what is going on... you can see exactly what is going onwithout having to attach a debugger. this technique can work reallywell on live servers where you haven't got. net and notepad is theonly tool available.

If. aspx doesn't give you what you want, a console application or snippet compilercan also work well. the general idea here is to re-produce the problemin a different environment which shoshould help to narrow down the cause.

Debug on the live Server

Sometimes, no matter how hard you try, you cannot reproduce an errorwithin a test environment... it only happens on the live server. if yourtrace statements are not giving you the details you require then youare going to have to debug the problem on the server causing theproblem. fortunately there are a number of solutions to this problem.

If you have a connection for which the firewall allows remote debuggingyou can copy some files into ss to the live server and attach thedebugger from your development machine... this will be obviusly work bestwith a debug build deployed. debugging remotely actually works verywell, but it will prevent the server from processing requests, thismeans the server will be unavailable whilst you perform yourde Bugging... which might be early Sunday morning... the only time you canmake the server unavailable!

If you can't get through the firewall then you can use windbgto Debug on the server. this will allow you to attach to processes andstep through code. it is actually more powerful than. net, but isharder to use as, even though it has a UI, it relies on a cryptic setof commands to get it to do what you want. even so, it is well worthusing as it can give you access to valuable information.

To give you a comparison, the following is the same debugging process in windbg as the previous example in vs. net.

Firstly attach to the w3wp process...

Once attached you will need to load the SOS. DLL to give you access to the debugging functions you'll need. You can use the following command to achieve this...

. Load c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ SOS. dll

You now need to tell the bugger to stop on. NET exceptions. Do this with the following command...


You can now continue the debugging by entering go (or click on the icon) and wait for the exception. when the exceptionis thrown the debugger will break and you will see the following in thecommand window...


As you can see we have hit an exception on the same line of code aswe did in vs. net. We can now look at the call stack using the command! Clrstack, which produces the following results...


This again shows us the WSS class and method in which the error occurred. Here you cocould also enter! Clrstack-P, which wocould show you the parameters and the memory addresses. if youwant you can look at the method parameters using the command! Dumpobject...

0: 015>! Clrstack-P
OS thread ID: 0x50d4 (15)
01c7ebe4 0bd81a4b Microsoft. Sharepoint. Publishing. webcontrols. consolexmlutilities. configurationxml (system. String, Boolean)
Configprovider = 0x0e843368
Isbuiltinconfigfile = 0x00000000

0: 015>! Dumpobj 0x0e843368
Name: system. String
Methodtable: 790fa3e0
Eeclass: 790fa340
Size: 52 (0x34) bytes
(C: \ WINDOWS \ Assembly \ gac_32 \ mscorlib \ _ b77a5c561934e089 \ mscorlib. dll)
String: customeditingmenu
MT field offset type vt attr Value Name
790fed1c 4000096 4 system. int32 0 instance 18 m_arraylength
790fed1c 4000097 8 system. int32 0 instance 17 m_stringlength
790 fbefc 4000098 C system. Char 0 instance 43 m_firstchar
790fa3e0 4000099 10 system. String 0 shared static empty
> Domain: Value 000db050: 790d6584 000fe470: 790d6584 <
79124670 400009a 14 system. Char [] 0 shared static whitespacechars
> Domain: Value 000db050: 01d61438 000fe470: 01d65140 <

By using! Dumpobj (or! Dofor short) You can look at all the objects in memory... providing youcan find their memory address. Other Useful Commands include...

! Dumpstackobjects (shows all objects currently on the stack)
! Printexception (! PE)
! Dumpallexceptions (! DAE)

Now we know the Class & method we can look at the code using the command! U...

! U 0bd81a4b

0bd81a0e e9b2000000 JMP 0bd81ac5
0bd81a13 8b8d54ffffff mov ECx, dword ptr [ebp-0ACh]
0bd81a19 ba02000000 mov edX, 2
0bd81a1e ff150c7ee20b call dword ptr ds: [0be27e0ch]
0bd81a24 8bf0 mov ESI, eax
0bd81a26 89b540ffffff mov dword ptr [ebp-0C0h], ESI
0bd81a2c 8b8d54ffffff mov ECx, dword ptr [ebp-0ACh]
0bd81a32 ba0000000 mov edX, 1
0bd81a37 ff150c7ee20b call dword ptr ds: [0be27e0ch]
0bd81a3d 8bf0 mov ESI, eax
0bd81a3f 89b53cffffff mov dword ptr [ebp-0C4h], ESI
0bd81a45 8b8d3cffffff mov ECx, dword ptr [ebp-0C4h]
>>> 0bd81a4b 3909 cmp dword ptr [ECx], ECx
0bd81a4d ff15a891b00a call dword ptr ds: [0ab091a8h]
0bd81a53 8bf0 mov ESI, eax
0bd81a55 8bce mov ECx, ESI
0bd81a57 3909 cmp dword ptr [ECx], ECx
0bd81a59 e8a24e2900 call 0c016900 (Microsoft. Sharepoint. splistitem. get_listitems (), mdtoken: 060035ef)

This is an abbreviated version, but you get the same Il as you do InvS. net, actually its better as you get some method names. We can nowuse reflector as before to solve the problem.

Debug using a crash dump

Most of the time your not really going to be able to attach adebugger to a live server, but that still doesn't mean you can't debugexceptions in Sharepoint. microsoft provide a utility called adplus, which will create mini dumps of the exceptions within your pointapplication. these dumps can then be opened in windbg to look at thedump in exactly the same way as you wocould using windbg live.


Adplus is a console application which attaches to the process andwaits for dumps to occur, taking a dump when they do. once captured thedumps can be transferred back to your development machine diagnosed foras long as you want without tying up the live server. this isparticipant ularly useful when your SharePoint site is being managed by ahosting company and you do not have RDP access to the live server. youcan easily script the commands so a Support Engineer at the hostingprovider can create the dumps and email them to you.

Useful Commands include

Adplus-Hang-PN w3wp.exe


Adplus-crash-PN w3wp.exe

When debugging SharePoint I have only ever got-crash to give me anything useful, but I am sure-Hang will be useful one day.

Note:By default you do not get a memory dump forfirst chance tions (because they can occur frequently), howeveradplus can be configured to do this (see ). not having a memory dump only means you can't see the contents of Parameters & objects... you may not always need them.

More on adplus can be found within these articles...


Further reading

The links below give you some things to do which can make your SharePoint debugging life easier.

A solution to "An Unexpected error has occurred" in WSS v3
Using iisapp to get the process ID of a Sharepoint Application
Test your spsitedataquery Parameters Using snippet Compiler
Anonymous SharePoint publishing site forcing Login
Make your SharePoint debugging experience a little less painful

Personally I have found if broken it is, fix it You shouldby Tess ferrandez to be one of the most interesting blogs arounddebugging code and. net in particle. I wowould recommend heading overthere if you ever need to track down a difficult problem, you will getsome good ideas and maybe a solution.

These are some links which I find useful when using windbg...


Hopefully this has given you some ideas as to how to approachdebugging your problem, I'm sure there are more, but these showould giveyou a good start.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.