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 two libraries and I would like to achieve the following functionality by using event receiver:

When I delete itemA in Library1, I would like to update another item, ItemZ in library2. That update will be removing the permissions which I have inserted in itemZ when it was selected in library1 ItemA.

I hope my explanation is clear.

What I am doing so far is this:

public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
              string pUser = properties.ListItem["PUser"].ToString();
            ReApplyPermissions(pUser);
        }

The problem is that at this stage the item in Library1 has not been deleted yet and when the method ReApplyPermissions is called, the permissions of ItemA are inserted back, eventhough later on the item is deleted ater the execution of the code.

Is there a way to pass the value of itemA to ItemDeleted event? Because in ItemDeleting event, the item is not deleted yet, so it still exists and that causes some problem with the clean up in library2, ItemZ

share|improve this question

2 Answers 2

Try to use already existing properties bags in SharePoint, not to the local variables, because SharePoint not only could run the code in different threads but on different processes and machines too, if it is a Web Farm. As an example SPWeb.AllProperties

public override void ItemDeleting(SPItemEventProperties properties)
{
     properties.Web.AllProperties[properties.ListId + properties.ListItemId] = properties.ListItem["PUser"].ToString();
     properties.Web.Update()
 }

public override void ItemDeleted(SPItemEventProperties properties)
{
     var user = properties.Web.AllProperties[properties.ListId + properties.ListItemId];
     .....
 }
share|improve this answer

Although properties.ListItem is null in the ItemDeleted event, properties.ListItemID is still set, and it is also available in the ItemDeleting event.

So to pass data from ItemDeleting to ItemDeleted, you can create a data structure to maintain the data.

As a simple example, you could create a private Dictionary variable in your event receiver that stores whatever data you need to keep, indexed on the ListItemID. Add the data in ItemDeleting, and consume it in ItemDeleted.

(For some background on ListItemID: List Item ID is an integer that SharePoint creates automatically for each list item. It behaves like an autoincrement i.e. it should never change or be reused.)

Code example:

// deletedStore is a Dictionary<int, string> member variable.
public override void ItemDeleting(SPItemEventProperties properties)
    {
        base.ItemDeleting(properties);
        deletedStore[properties.ListItemId] = properties.ListItem["PUser"].ToString();
    }

public override void ItemDeleted(SPItemEventProperties properties)
    {
        base.ItemDeleting(properties);
        ReApplyPermissions(deletedStore[properties.ListItemId]);
    }

(Edited to add code example)

share|improve this answer
    
Hi Hutch, thanks for your reply. I tried to create a dictionary to hold the data that I assign a valut to it in itemDeleting, but it returns null in itemdeleted. Have you tried this before? Did it work for you? –  Imir Hoxha Apr 8 '13 at 17:52
    
Sorry for the delayed response - yes I was able to do this on my test box. See code example, maybe it shows better what it was getting at. –  Hutch Apr 23 '13 at 18:15

Your Answer

 
discard

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

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