title: How do I test an EventReceiver?
body: <p>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) </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <pre><code>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;
     }
  }
  /// &lt;summary&gt; 
  /// Builds an arbitrary SPQuery which filters by a single column value. 
  /// &lt;/summary&gt; 
  /// &lt;param name="list"&gt;The list you will run the query against.&lt;/param&gt; 
  /// &lt;param name="columnDisplayName"&gt;The Display Name of the column you want to filter.&lt;/param&gt; 
  /// &lt;param name="value"&gt;The value to filter against.&lt;/param&gt; 
  /// &lt;returns&gt;A new SPQuery object ready to run against the list.&lt;/returns&gt; 
  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 = "&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=\"" + internalName + "\"/&gt;&lt;Value Type=\"Text\"&gt;" + value + "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;";
  
     if (deepSearch)
         query.ViewAttributes += "Scope='RecursiveAll'";
  
     return query;
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>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?</p>
  
  <p><em>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)</em></p>
  
  <pre><code>       SPListItem currentItem = properties.ListItem;
         currentItem["Title"] = DateTime.Now.ToString();
         currentItem.Update();
  </code></pre>