2

I have an list item called Department and I need to keep tack if an employee changes Departament I.E if he is in sales department and if he moves to warehouse department, there should be a list column to describe this move.

4

This goal could be done in many ways. One manner to do this is as following.

  • Create a new list column named Departmentlog
  • Create another list column named worklog this should be of type multi lines of text and enable append changes to existing text in bottom of settings
  • Open visual studio and create a new project and select empty SharePoint project.
  • On solution click add and add new Item, then select event receivers.

  • You need 2 event receivers for your desired list. First event receiver must be On Item is being updated and Second event receiver must be On item was updated.

For the first event receiver you must have the following code:

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

        properties.AfterProperties["Departmentlog"] = properties.ListItem["Department"].ToString(); //this one saves old deparment.

        base.EventFiringEnabled = false;
        properties.ListItem.SystemUpdate(false);
        base.EventFiringEnabled = true;
    }

For the second event receiver you must have the following code:

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

        string dep = properties.ListItem["Department"].ToString();
        string loguDep = properties.ListItem["Departmentlog"].ToString();

        if (loguDep == dep) { }

        else if (loguDep != dep) // in this point we make sure that old department is not same as new department, because every updates that are applied same changes will be in log too this condition prevents from this mistake. 
        {
            properties.ListItem["worklog"] = "Department was changed from: " + properties.ListItem["Departmentlog"].ToString() + " To: " + properties.ListItem["Department"].ToString();
        }

        base.EventFiringEnabled = false;
        properties.ListItem.SystemUpdate(false);
        base.EventFiringEnabled = true;        
    }

This should work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.