Pergunta

É possível anexar um receptor de eventos remoto à minha lista existente no meu site e não à lista no meu aplicativo?Acho que não, pois não consigo encontrar nada sobre isso e acho que isso poderia 'prejudicar' o SharePoint.

Se eu precisar fazer isso funcionar, preciso procurar uma solução sandbox ou farm, certo?

TIA

Foi útil?

Solução

Eu diria que não é possível anexar um receptor de evento remoto a uma lista no Host-web, porque na verdade são coleções cruzadas de sites.

No Host web, você só seria capaz de implantar receptores de eventos regulares (direcionando uma classe em um assembly) e os receptores de eventos remotos usam a URL do WCF real como destino, que não existiria no Host-web de qualquer maneira:

<Url>~remoteAppUrl/RemoteEventReceiver1.svc</Url>

ou

<Url>http://apps.mydomain.com:36511/MyReR.svc</Url>

Outras dicas

Sim, é possível.Basta manipular o receptor de eventos durante a instalação do aplicativo, alterando o atributo de propriedade para true conforme mostrado abaixo.

enter image description here

E então basta escrever o seguinte código

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();

            switch (properties.EventType)
            {
                case SPRemoteEventType.AppInstalled:
                    HandleAppInstalled(properties);
                    break;
                case SPRemoteEventType.ItemAdded:
                    HandleItemAdded(properties);
                    break;
            }
            return result;
        }

        private void HandleAppInstalled(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext =
                      TokenHelper.CreateAppEventClientContext(properties, false))
            {
                if (clientContext != null)
                {
                    List myList = clientContext.Web.Lists.GetByTitle("Your List Name");
                    clientContext.Load(myList, p => p.EventReceivers);
                    clientContext.ExecuteQuery();
                    bool rerExists = false;

                    foreach (var rer in myList.EventReceivers)
                    {
                        if (rer.ReceiverName == "ItemAddedEvent")
                        {
                            rerExists = true;
                            System.Diagnostics.Trace.WriteLine("Found existing ItemAdded receiver at "
                               + rer.ReceiverUrl);
                        }
                    }
                    if (!rerExists)
                    {
                        EventReceiverDefinitionCreationInformation receiver =new EventReceiverDefinitionCreationInformation();
                        receiver.EventType = EventReceiverType.ItemAdded;


                        receiver.ReceiverUrl = "Your service url";
                        receiver.ReceiverName = "ItemAddedEvent";
                        receiver.Synchronization = EventReceiverSynchronization.Synchronous;
                        myList.EventReceivers.Add(receiver);
                        clientContext.ExecuteQuery();
                        System.Diagnostics.Trace.WriteLine("Added ItemAdded receiver at " + msg.Headers.To.ToString());
                    }
                }
            }
        }
 private void HandleItemAdded(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext =
              TokenHelper.CreateRemoteEventReceiverClientContext(properties))
            {
                if (clientContext != null)
                {
                    try
                    {
                        List photos = clientContext.Web.Lists.GetByTitle("Your List Name");
                        ListItem item = photos.GetItemById(
                           properties.ItemEventProperties.ListItemId);
                        clientContext.Load(item);
                        clientContext.ExecuteQuery();

                        item["Title"] += "\nUpdated by RER " +
                             System.DateTime.Now.ToLongTimeString();
                        item.Update();
                        clientContext.ExecuteQuery();
                    }
                    catch (Exception oops)
                    {
                        System.Diagnostics.Trace.WriteLine(oops.Message);
                    }
                }
            }
        }

Eu espero que isso te ajude.

A TI só é possível de forma programática, mas não funciona se você implantar.

var eventReceiver = new EventReceiverDefinitionCreationInformation
                     {
                         EventType = EventReceiverType.ItemAdding,
                         ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                         ReceiverClass = "RemoteReciverWeb.Services.ContactAddEventReceiver",
                         ReceiverName = "ContactAddEventReceiver",
                         ReceiverUrl = remoteEventEndPointUrl,
                         SequenceNumber = 1000
                     };

clientContext.Load(eventresevers);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top