1

I recently created an EventReceiver that should add a certain web part to new pages that are created. However, the EventReceiver's behavior seem very unreliable.

For example, the first page I create doesn't have the web part on it. The next one does. Then the third has TWO of them, which shouldn't be happening.

Is this a common issue? If so, how can I resolve this?

Thanks in advance!

    public class PageCreationEventReceiver : SPItemEventReceiver
{        
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);

        AddWikiPageWebPart(properties.ListItem);
    }

    private void AddWikiPageWebPart(SPListItem item)
    {
        SPFile file = item.File;
        SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
        EditLabelsButton.EditLabelsButton wp = new EditLabelsButton.EditLabelsButton("Labels");

        if (!item.Web.GetLimitedWebPartManager(item.Url, PersonalizationScope.Shared).WebParts.Contains(wp))
        {                
            wpm.AddWebPart(wp, "Side", 0);
            wpm.SaveChanges(wp);
        }
    }
}
  • without code we can only guess im afraid! sounds like a bug in code! as im going to guess that the 4th page you create will have 3 webparts and so on ;) – Ali Jafer Jun 28 '13 at 8:11
  • I've added my code. There isn't any loop present that would explain this behavior. Sadly, checking whether the webpart exists doesn't seem to work either. – David Jun 28 '13 at 8:16
2

how are you checking if the webpart exists?

i presume its the if statment?

    if (!item.Web.GetLimitedWebPartManager(item.Url, PersonalizationScope.Shared).WebParts.Contains(wp))

how about this method? you can call it in your code! you can change the method so that it returns a bool

public bool isWebPartOnPage(SPWeb web, string pageUrl, string webPartName)
{
    using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
    {
        SPLimitedWebPartCollection coll = webPartManager.WebParts;
        foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in coll)
        {
            if (webPartName == webpart.GetType().Name)
            {
                return true;
            }
        }
    }
    return false;
}

or have the code above in your code directly!

private void AddWikiPageWebPart(SPListItem item)
{
    SPFile file = item.File;
    EditLabelsButton.EditLabelsButton wp = new EditLabelsButton.EditLabelsButton("Labels");

    usSPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared)

    bool webex = false;

    SPLimitedWebPartCollection coll = webPartManager.WebParts;
    foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in coll)
    {
         if (wp.Name == webpart.GetType().Name)
         {
            webex = true;
            break;
         }
    }

    if (webex == false)
    {
            wpm.AddWebPart(wp, "Side", 0);
            wpm.SaveChanges(wp);
    }

}

what is strange is that as you noted there is no looping what so ever! yet every time you create a new page its remembering the last action and incrementing by 1 every time! so your starting at 0 and working your way up! hopefully that would stop adding the webpart to the page if it already exist but the issue is the looping. have you debugged the code? put a break point in the first method to see exactly what goes on in the first instance!

the origional code from above was taken from here:

How to find where a web part has been placed

EDIT

Had a look and found this, its doing what you want it to do! if it does the same again than its nothing todo with the code and could be a bug in your version of sharepoint!

    public override void ItemAdded(SPItemEventProperties properties)
    {     
        //we check if event fired from page library and page was created base on correct page layout (with web part zone).
        if (properties.ListTitle.Equals("Pages") && properties.ListItem.ContentType.Name.Equals("Page Layout Name")) 
        {
            SPFile thisFile = properties.ListItem.File;
            SPLimitedWebPartManager webPartManager = thisFile.GetLimitedWebPartManager(PersonalizationScope.Shared); 
            EditLabelsButton.EditLabelsButton wp = new EditLabelsButton.EditLabelsButton("Labels");

            /**web part settings ommited here**/ 
            webPartManager.AddWebPart(wp, "Side", o);
        }
        base.ItemAdded(properties); 
    }

there is suttle differences between yours and the one above! also like to note base.ItemAdded(properties); is at the bottom of the stack! that could be a possibility!

taken from here:

http://www.jamestsai.net/Blog/post/How-to-use-event-handler-to-add-web-part-to-web-part-page.aspx

  • Just to be clear, the webpart doesn't get added more then twice on any page. After being adding twice, the next page might have the webpart as it should be, or not at all. It seems to be fairly random. – David Jun 28 '13 at 9:26
  • just ammended my answer – Ali Jafer Jun 28 '13 at 10:52

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.