Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a business requirement to add a new version (revision) of the existing list item. New list item should have this setting:

NewItem.ParentID=ParentItem.ID

NewItem.ProjectID=ParentItem.ProjectID

NewItem.RevisionNumber=ParentItem. RevisionNumber +1

Existing (Parent) list item should be inactivated:

ParentItem.Status=”Inactive”

I have created a custom Ribbon Button (visible on the list Display form) which passes ParentItem.ID to list’s New form so I am able to create a NewItem and set NewItem.ParentID=ParentItem.ID. Next step was to create Event Receiver which should access ParentItem record (ProjectID and RevisionNumber) and set NewItem as stated above. I was trying to get this done using ItemAdding or ItemAdded or combination of both events but no luck.

ItemAdding – Code

  Int32 ParentId = Convert.ToInt32(properties.AfterProperties["RevisionNumber"]);      
       if (ParentId != 0)
       {
           SPListItem NewItem = properties.ListItem;
           SPList List = NewItem.ParentList;
           SPListItem ParentItem = List.GetItemById(ParentId);

           Int32 NewItem_ProjectID = 0;               
           Int32 NewItem_RevisionNo = 0;

           // 1. set new item values using parent item               
           NewItem_ProjectID = Convert.ToInt32(ParentItem["Project"]); ;
           NewItem_RevisionNo = Convert.ToInt32(ParentItem["RevisionNumber"]) + 1;

           properties.AfterProperties["Project"] = NewItem_ProjectID;
           properties.AfterProperties["RevisionNumber"] = NewItem_RevisionNo;
       }

I was able to update Project and Revision Number in AfterProperties using hard-coded values but not with the values from the ParentItem.

ItemAdded – Code

Int32 ParentId = Convert.ToInt32(properties.ListItem["Parent ID"]);
       if (ParentId != 0)
       {               
           Int32 NewItem_ID = 0;
           Int32 NewItem_ProjectID = 0;
           Int32 NewItem_RevisionNo = 0;

           //NewItem_ID = Convert.ToInt32(properties.ListItem["ID"]);
           SPListItem NewItem = properties.ListItem;
           SPList List = NewItem.ParentList;
           SPListItem ParentItem = List.GetItemById(ParentId);

           // 1. set new item values using parent item                
           NewItem_ProjectID = Convert.ToInt32(ParentItem["Project"]);
           NewItem_RevisionNo = Convert.ToInt32(ParentItem["Revision Number"]) + 1;

           this.EventFiringEnabled = false;
           NewItem["Revision Number"] = NewItem_RevisionNo;
           NewItem["Project"] = NewItem_ProjectID;
           NewItem.Update();
           this.EventFiringEnabled = true;  

           // 2.UPDATE PARENT ITEM - SET STATUS TO RESUBMIT 
           this.EventFiringEnabled = false;
           ParentItem["Status"] = "Resubmit";
           ParentItem.Update();
           this.EventFiringEnabled = true;
       }

I am running out of time so I will be very grateful if someone comes with a solution. Thanks in advance.

Dragan

share|improve this question
    
Is there a reason you aren't just using SharePoint versioning? (details: office.microsoft.com/en-ca/sharepoint-server-help/…) –  lgaud Feb 5 '13 at 17:12
    
My list is created from custom Content Type and List Definition and also I am not so familiar with using SP versioning. Is it possible to use it on the list? –  Dragan Petrovic Feb 5 '13 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.