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 list in the rootweb, and a list in a subweb. I want to update/add when something changes in an item in the subweb. This method is invoked in an ItemUpdated event receiver. The problem is that the item is added four times to the rootlist.

/// <summary>
/// Updates an item on a rootweblist, or adds the item if it doesn't exist.
/// </summary>
/// <param name="leafWeb">The web the updated item belongs to</param>
/// <param name="leafItem">The item being updated</param>
/// <param name="rootWebListName">The name of the list in the root web</param>
/// <param name="contentTypeName">Name of the custom content type</param>
/// <param name="fieldsToUpdate">The fields to update</param>
internal void UpdateItemOnRootwebList(SPWeb leafWeb, SPListItem leafItem, string rootWebListName, string contentTypeName, List<string> fieldsToUpdate)
{
    //Impersonate system account - Not sure if needed
    var systemAccToken = leafWeb.Site.SystemAccount.UserToken;
    using (var kundeSiteCol = new SPSite(leafWeb.Site.ID, systemAccToken))
    {
        using (var rootWeb = kundeSiteCol.OpenWeb())
        {
            var rootList = rootWeb.Lists.TryGetList(rootWebListName);
            if (rootList != null)
            {
                //Find the corresponding item on rootlist, or create a new if it doesn't exist
                var listItems = rootList.Items;
                var rootItem = listItems.Cast<SPItem>().FirstOrDefault(and => and["Title"] == leafItem["Title"]);
                if (rootItem == null) rootItem = listItems.Add();

                //Set content type
                var itemType = rootList.ContentTypes[contentTypeName];
                rootItem["ContentTypeId"] = itemType.Id;

                //Update the relevant fields
                foreach (var field in fieldsToUpdate)
                {
                    rootItem[field] = leafItem[field];
                }
                //Update the item, or add if it doesn't exist
                rootItem.Update();
            }
        }
    }
}

Update: Rephrased the question tremendously.

share|improve this question
    
not it, but still: when feetching system user token its a good idea to catch unauthorized exception solutionizing.net/2009/01/06/elegant-spsite-elevation –  Anders Rask Sep 26 '11 at 7:53
    
Rephrased because I'm an idiot. Something else is the problem now. –  tarjeieo Sep 26 '11 at 8:39
    
UpdateItemOnRootwebList runs 4 times? If yes problem is in ItemUpdated. Maybe you are doing updates on original item inside it? –  Vedran Rasol Sep 26 '11 at 8:43
    
There was a problem with the code. I was comparing the Title of a doc.lib file to the title of an item. This was wrong, I corrected it to compare the item's title to another variable containing the file's item-title (not e.g. default.aspx). Then an item is only added once. Still firing several times though, but now we're only updating the old item. –  tarjeieo Sep 26 '11 at 9:44

1 Answer 1

I anticipate that your root list has the same event receiver assigned to it, and that causes the recursion.

The problem, of course, may be not so obvious, but anyway, to prevent event receiver to fire several times, there is a common practice in SharePoint: just use DisableEventFiring and EnableEventFiring methods.

internal void UpdateItemOnRootwebList(SPWeb leafWeb, SPListItem leafItem, string rootWebListName, string contentTypeName, List<string> fieldsToUpdate)
{
    try
    {
        this.DisableEventFiring();

        // do your work here...

    }
    finally
    {
        this.EnableEventFiring();
    }
}

If this will not help (for example if different instances of the receiver class are involved), you could then create a static variable in your item receiver and check it from within of the method, like this:

static bool Disabled = false;

// ...

internal void UpdateItemOnRootwebList(SPWeb leafWeb, SPListItem leafItem, string rootWebListName, string contentTypeName, List<string> fieldsToUpdate)
{

    try
    {
        if (Disabled)
            return;

        Disabled = true;

        // do your work here...

    }
    finally
    {
        Disabled = false;
    }
}
share|improve this answer
    
Sorry didnt see your almost identical answer :=) –  Anders Rask Sep 26 '11 at 10:58
    
Oh, that's all right, I don't mind. Especially taking into account, that your answer helped me to update my knowledge on the topic :) –  Andrey Markeev Sep 26 '11 at 11:02
    
+1 I added comment to original question about this but your answer covers everything. Too bad that original question is IMO too localized (I voted close). Now I regret it because your answers contains valuable info. –  Vedran Rasol Sep 26 '11 at 11:20
    
@VedranRasol, thanks! Actually, the situation with rephrased questions is desperate, IMO :( We probably need to discuss this on meta. –  Andrey Markeev Sep 26 '11 at 11:55

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.