Home > .NET, SharePoint > Debug a SharePoint 2007 EventReceiver with Visual Studio 2008

Debug a SharePoint 2007 EventReceiver with Visual Studio 2008

2012/04/17

I was requested for to implement a direct link in a field to the attachment of the same list Item.

By default you can enclose more than an one attachment , so it is necessary to create customs forms for add/edit of the list items, and implement this javascript:

function getField(fieldType,fieldTitle){
    var docTags = window.document.getElementsByTagName(fieldType);
    for (var i = 0; i < docTags.length; i++) {
        if (docTags[i].title == fieldTitle) {
            return docTags[i];
        }
    }
    docTags = null;
 }
 
 function PreSaveAction(){
    // the function automatically called from Moss when is saving
    var intRowCount = $('#idAttachmentsTable tr').length;  //using jQuery
    var objDocType = getField('select','Document Type');
    var index = objDocType.selectedIndex;
    switch(index){
        case 0:
            // document: we need to verify that there is a single document enclosed
            if(intRowCount  ==  0){
                window.alert("Please attach at least one file");
                return false;
            }
            if(intRowCount  >  1){
                window.alert("Please attach only one file");
                return false;                                        
            }                                        
            break;
            .....

Anyway the real problem was to read the http path to the attachment: in PreSaveAction for a add item form i don’t still have the ID of the new item, in a workflow i don’t found how to access to the current base URL as in SharePoint 2010, where you have the “WorkFlow Context” for to build a string.

So the best approach was an EventReceiver; i developed it using vsewss 1.3 (argument of an my previous post).

The steps are: create a new empty Sharepoint project then add an SharePoint EventReceiver.

Done this , at a first time the debug was working (done from tools->attach to process->w3wp.exe).

For a first try i have implemented ItemUpdate

/// <summary>
/// Asynchronous after event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.
/// </summary>
/// <param name="properties">
/// A Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler.
/// </param>n
public override void ItemUpdated(SPItemEventProperties properties)
{
    base.ItemUpdated(properties);
    ManageItem(properties.ListItem, properties.WebUrl);
}
 
 private void ManageItem(SPListItem listRec, string urlbase)
{
    try
    {
        if (listRec["Document Type"].ToString() == "DOCUMENT")
        {
            DisableEventFiring();
            listRec["Link 1"] = urlbase + "/Lists/Documental/Attachments/" + listRec["ID"] + "/" + listRec.Attachments[0];
            listRec.SystemUpdate(false);
            EnableEventFiring();
        }
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.Message);
    }
    finally
    {
    }
}

In this case is mandatory to use DisableEventFiring , otherwise the code enter in a endless loop.

This is the working code, but before there were some errors to fix and at a second debug session i was able to hit the breakpoint , but at the DisableEventFiring instruction the debug session was interrupted , breaking out of Visual Studio 2008.

Even worse an successive debug retry: no breakpoint hit because pressing f9 on the code lines i got the infamous “The breakpoint will not currently be hit. No symbols have been loaded for this document.” error, on the line with the breakpoint:


I have tried to exit from Visual Studio, clear the Internet Explorer 8 cache (i was trying on the same server where i’m developing, no luck from another computer), launch devenv /resetskippkgs , delete all from the .net cache in C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files, delete all from c :\Users\Administrator\AppData\Local\Microsoft\WebsiteCache (except Websites.xml) , sfc /scannow… nothing.

Hint : you can find all .tmp files on your drive with the command (from an administrative prompt) “where /R c:\ *.tmp”.

I have verified in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\FEATURES : there were the solution folders, the xml seems ok, the guid in ItemEventReceiver.xml (…. Version=1.0.0.1, Culture=neutral, PublicKeyToken=7675d7d81126c533</Assembly>) is the same that i can read in c:\windows\assembly


Apparently all ok. Note that while i was not able to debug the deploy was apparently not working, i have made changes to code but the field of my example was always filled with the original content provided by the first version of my code. My environment is a Windows Server 2008 (not r2) virtual machine created with Vmware Workstation 8, there in only SharePoint 2007 server with the designer and Visual Studio 2008: could be a problem since i have installed also WSPBuilder ? I don’t know , but i was knowing that i must compile in debug mode, in the site collection web.config i must have <compilation batch=”false” debug=”true”> , and tipically for debugging is needed the .pdb file , the file with the debugging symbols.

But c:\windows\assembly is giving a “fake” view , so i have found that using the old subst command as “subst g: c:\windows\assembly” i have a new point of view in c:\windows\assembly:


The namespace is “masked” for obvious reasons (the name of the consulting firm + the one of the customer…)

This is the normal situation , but i have copied the pdb file generated from Visual Studio here , iisreset and i have tried tools->attach process.

Still the “breakpoint will not currently be hit…” advice on the debugging line… sigh… i was dejected but the the breakpoint was hit ! and immediately changed to the full red point.

I have then tried to re-publish (the pdb was deleted from the folder where i have copied it) and the debugging is still working.

There is some strange problem caused from the virtualized windows server 2008 ? In origin visual studio 2008 has strange issues with caching , amplified in a virtual environment ? I don’t know but now finally my EventReceiver is working and debuggable:


In the List , after the update:


Note: after ItemUpdate i have tried to implement ItemAdded, but was not firing: going to Site Actions – Site Settings – Site Features , deactivating and activating the features the ItemAdded works. The same for other events.

Advertisements
Categories: .NET, SharePoint
%d bloggers like this: