How to update field value in current item via event receiver

event-receiversharepoint

EDIT: I've realized that my approach in the second code block was unnecessary. I could accomplish the same thing by doing the following in ItemUpdated:

SPListItem thisItem = properties.ListItem;

thisItem.File.CheckOut();

thisItem["Facility Number"] = "12345";
thisItem.Update();

thisItem.File.CheckIn("force check in");

Unfortunately, I'm still getting the same error message when "thisItem.Update();" is executed: he sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request

I actually was receiving the error above when deploying my sandbox solution originally and used this link (http://blogs.msdn.com/b/sharepointdev/archive/2011/02/08/error-the-sandboxed-code-execution-request-was-refused-because-the-sandboxed-code-host-service-was-too-busy-to-handle-the-request.aspx) to fix it.


I am trying to write a C# event receiver that changes the value of a field when a document is added/changed in a library. I have tried using the following code:

public override void ItemUpdating(SPItemEventProperties properties)
{
    base.ItemUpdating(properties);
    string fieldInternalName = properties.List.Fields["Facility Number"].InternalName;
    properties.AfterProperties[fieldInternalName] = "12345";
}

Unfortunately, this is only working for certain fields. For example, if I replaced "Facility Number" with "Source", the code will execute properly. This may be the fact that we are using a third party software (called KnowledgeLake) that replaces the default edit form in SharePoint with a Silverlight form. Anyway, because I was having challenges with the code above (again, because I think the Silverlight form may be overriding the field after the ItemUpdating event fires), I have tried the following code:

 public override void ItemUpdated(SPItemEventProperties properties)
 {

       base.ItemUpdated(properties);

       //get the current item
       SPListItem thisItem = properties.ListItem;

       string fieldName = "Facility Number";
       string fieldInternalName = properties.List.Fields[fieldName].InternalName;
       string fieldValue = (string)thisItem["Facility Number"];

       if (!String.IsNullOrEmpty(fieldValue))
       {
           //properties.AfterProperties[fieldInternalName] = "123456789";

           SPWeb oWebsite = properties.Web as SPWeb;
           SPListItemCollection oList = oWebsite.Lists[properties.ListTitle].Items;

           SPListItem newItem = oList.GetItemById(thisItem.ID);

           newItem.File.CheckOut();

           thisItem[fieldInternalName] = "12345";
           thisItem.Update();

           newItem.File.CheckIn("force");
       }
   }

First off, the above seems a little klunky to me as I would love to just use the AfterProperties method. Additionally, I am getting the following error when "newItem.Update()" is executed: he sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request

Am I missing something here? I would love to utilize the first code block. Any help would be appreciated.

Best Answer

Josh was able to answer his own question, which helped me fix my problem as well. Here is a working code snippit.

public override void ItemUpdated(SPItemEventProperties properties)
{
 string internalName = properties.ListItem.Fields[columnToUpdate].InternalName;

 //Turn off event firing during item update
 base.EventFiringEnabled = false;

 SPListItem item = properties.ListItem;
 item[internalName] = newVal;
 item.Update();

 //Turn back on event firing
 base.EventFiringEnabled = true;

 base.ItemUpdated(properties);
}
Related Topic