Question

I am pretty much going crazy trying to get a SharePoint List Event Receiver to work. I have created an EventReceiver Project in VS and can debug it however, the break points don't work. Basically its the same issue lots of people have had but none of their solutions seem to work. I posted this before and I think my event receiver code should work but I can't seem to get it working on the list itself. (my code is pasted below)

Basically all I need is for the Event Receiver to rename a document that is uploaded. Consider this scenario, if a document is uploaded called Client A Document and is the first document it should be called Client A Document 1. If the next document uploaded is called Client A Document, it should be renamed to Client A Document 2 and so on. Now if another document is uploaded, named Client B Document, it should just be Client B Document 1 since there are no others with the same name. Now I think my code below accomplishes this behaviour (code was written thanks to Robert Christs help!) but I don't know how to test it.

Do I upload a document or create a new one? I have tried both and nothing works, anyone have any ideas how to accomplish this? I'm starting to lose my mind with this requirement.

public override void ItemAdding(SPItemEventProperties properties)
{
   base.ItemAdding(properties);

   SPListItem item = properties.ListItem;

   if (item == null || item["Name"] == null) //item["Name"] == null)
       return; //or better yet, log 

   string oldFileName = item["Name"].ToString();

   int positionOfPeriod = oldFileName.LastIndexOf(".");
   string tempFileName = oldFileName.Substring(0, positionOfPeriod);

   SPQuery query = BuildArbitraryQuery(properties.List, "Name", tempFileName, true);
   int count = properties.List.GetItems(query).Count;
   String fileName, fileExtension;

   if (positionOfPeriod == -1)
   {
       fileName = oldFileName;
       fileExtension = "";
   }
   else
   {
       fileName = oldFileName.Substring(0, positionOfPeriod);
       fileExtension = oldFileName.Substring(positionOfPeriod);
   }

   string newFileName = fileName + "-xx" + count.ToString() + fileExtension;

   item["Name"] = newFileName;

   Console.WriteLine("New File Name: " + newFileName);

   try
   {
       properties.Web.AllowUnsafeUpdates = true;
       EventFiringEnabled = false;

       item.Update();
   }
   finally
   {
       properties.Web.AllowUnsafeUpdates = false;
       EventFiringEnabled = true;
   }
}
/// <summary> 
/// Builds an arbitrary SPQuery which filters by a single column value. 
/// </summary> 
/// <param name="list">The list you will run the query against.</param> 
/// <param name="columnDisplayName">The Display Name of the column you want to filter.</param> 
/// <param name="value">The value to filter against.</param> 
/// <returns>A new SPQuery object ready to run against the list.</returns> 
public static SPQuery BuildArbitraryQuery(SPList list, string columnDocumentName, string value, bool deepSearch)
{
   if (list == null)
       throw new ArgumentNullException("You cannot pass a null list to Helper.BuildArbitraryQuery.");

   if (!list.Fields.ContainsField(columnDocumentName))
       throw new ArgumentException("The SharePoint List \"" + list.Title + "\" does not contain the Field \"" + columnDocumentName + "\".");

   string internalName = list.Fields[columnDocumentName].InternalName;
   SPQuery query = new SPQuery();
   query.Query = "<Where><Eq><FieldRef Name=\"" + internalName + "\"/><Value Type=\"Text\">" + value + "</Value></Eq></Where>";

   if (deepSearch)
       query.ViewAttributes += "Scope='RecursiveAll'";

   return query;
}

EDIT:-------------------------------------------------- Ok so I did a little test started with the same project type (event receiver) and created a very simple ItemAdded method to change the name of a list item to the current date. Now this works on a custom list but I can't seem to get this working with a document library.

So from this little test I know that I can register an event recevier to a custom list (sandboxed solution) using F5 and debugging it, but what is different about document libraries? And is my code that I pasted not ok for what I am trying to do on a document library?

This is the code I used for the small test but it doesnt work on document libraries, even if I create a new project type for document libraries rather than custom lists (this is in ItemAdded)

       SPListItem currentItem = properties.ListItem;
       currentItem["Title"] = DateTime.Now.ToString();
       currentItem.Update();
Was it helpful?

Solution

Here is what you should do:

  1. Restart IIS "to unload the DLLs if they are used"
  2. Put your DLL and its pdb file in the GAC folder that is appropriate for this DLL
  3. open SharePoint web site such that an new w3wp process is started.
  4. Attached your VS project to the w3wp make sure you choose managed code in the attach to process dialog.
  5. Try to upload a file 6- you should be able to catch break point now.

You can make Shell Script that can be called in post-build event to automate all this steps.

OTHER TIPS

How exactly you were trying to debug ? You need to attach to the appropriate w3wp.exe process and add an item, if the breakpoints doesn't work it's usually because the version of the last deployed dll is newer than your code in visual studio(even the slightest change can cause this difference like new line etc)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top