Question

How to get a calculated field with modify date, when one field or column changes in the share point list.

For example, If I have a field status, i should get a modify field populated with today's date whenever I change the status field. The date column should not change in case of changes in the other column other than status column.

Please help me ...

UPDATE Event Reciever code I tried..

public override void ItemUpdating(SPItemEventProperties properties)
       {

           string currentStatus = properties.ListItem["Status"].ToString();
           string newStatus = properties.AfterProperties["Status"].ToString();

           if (currentStatus != newStatus)
           {
               SPListItem oItem = properties.ListItem;
               oItem["Last Modified"] = "Hi";
               oItem.Update();
           }

       }

Element.xml

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="120">
      <Receiver>
        <Name>EventReceiver1ItemUpdating</Name>
        <Type>ItemUpdating</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>StatChange.EventReceiver1.EventReceiver1</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>

  </Receivers>
</Elements>

ListTemplateId="120" (Because - List is of type DataSheet List).

However getting a conflict like - Rows you changed were previously changed by (My User Name) on ...... Your chnages conflict with that users change.

This I believe because of the update command in the Reciever. How can I get rid of this - Though the column 'Last Modified' is populating correctly ....

Was it helpful?

Solution

I think you can't update Item in ItemUpdating method, that will triger Recursion updating. As described in this question/answer: https://stackoverflow.com/questions/3065881/how-to-change-a-field-value-during-the-itemupdating-event.

So, my solution is: do it at client side. Write some JQuery code in your page to monitor the status field's value. Only when it changes, you sent your hidden "Last Modified" field value with the current date by JQuery like $("#your_field_id").attr("value",now);

You can use SharePoint Designer to customize your list's Edit Form to hide the "Last Modified" field, thus you can prevend from manually changing the date.

OTHER TIPS

if you change this field through code you can try to use SPListItem.SystemUpdate method.

public override void ItemUpdating(SPItemEventProperties properties)
   {

       if (properties.BeforeProperties["Status"]!= properties.AfterProperties["Status"])
       {
           properties.AfterProperties["Modified"] = properties.BeforeProperties["Modified"];
       }

   }

I would write an event handler (SPItemEventReceiver) to do this, and then register it against my list.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top