javascriptc#sharepoint-2010code-behindevent-receiver

Why can I not call a public method in my Web Part (User Control) from an Event Receiver?


I've added this Event Receiver (based on what I found here):

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace PostTravelWizard.PostTravelItemEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class PostTravelItemEventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           //GeneratePDF(); <= "The name 'GeneratePDF' does not exist in the current context
       }

       /// <summary>
       /// The list received a context event.
       /// </summary>
       public override void ContextEvent(SPItemEventProperties properties)
       {
           base.ContextEvent(properties);
           // TODO: What "context event" occurs here? To what event should I respond?
       }

    }
}

I'm hoping to, when a List is updated (from the client side/Javascript), then retrieve those values in the code-behind to generate a PDF file. I have this code in my *.ascx.cs file:

public partial class PostTravelWizardWebPartUserControl : UserControl
{
    . . .

    public void GeneratePDF(PostTravelData ptd)
    {
        ;//bla
    }

...but it's not letting my call GeneratePDF() from the Event Receiver - it fails with

The name 'GeneratePDF' does not exist in the current context

Why? How can I rectify this asunto?


Solution

  • GeneratePDF is an instance method of PostTravelWizardWebPartUserControl class. In order to call it from PostTravelItemEventReceiver you have to create an instance of PostTravelWizardWebPartUserControl or let the method(GeneratePDF) be static.

    Case 1

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        new PostTravelWizardWebPartUserControl().GeneratePDF();
    }
    

    Case 2

    public partial class PostTravelWizardWebPartUserControl : UserControl
    {
        public static void GeneratePDF(PostTravelData ptd)
        {
            ;//bla
        }
    }
    
    
    public class PostTravelItemEventReceiver : SPItemEventReceiver
    {
        base.ItemAdded(properties);
        PostTravelWizardWebPartUserControl.GeneratePDF();
    }