共用方式為


Defining content in Host Web from an App for SharePoint

Here is a scenario. The app model for SharePoint 2013 allows developers to define remote event receivers for their apps. Sometimes though you actually want to create a remote event receiver for the host web as opposed to the app web. Visual Studio 2012 makes it relatively easy to define remote event receivers for the app web but not the host web. So how can you create event receivers?

You can do it using an app event receiver and code. An app event receiver allows you to create events that can execute code when an app is installed, uninstalled and upgraded. In visual studio selecting the app project will allow you to see the properties in the properties window. There are three properties that by default are set to false. "Handle App Install", "Handle App Uninstall" and Handle App Upgrade", selecting true to any one of these properties will create a class in your web project that can receive the event. The class inherits from IRemoteEventService much like a normal event receiver. In the ProcessEvent method you can then execute any code you like. For instance the sample below. I am activating the Push Notification feature which supports managing subscribers for push notifications for mobile devices.

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)

{

      SPRemoteEventResult result = newSPRemoteEventResult();

      using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))

      {

              if (clientContext != null)

               {

                       //Add Push Notification Feature to HostWeb

                      clientContext.Web.Features.Add(

                               new Guid("41e1d4bf-b1a2-47f7-ab80-d5d6cbba3092"),

                               true, FeatureDefinitionScope.None);

                      clientContext.ExecuteQuery();

               }

       }

}

 

In the above sample you will notice the TokenHelper.CreateAppEventClientContext method. The second parameter takes a Boolean value that determines if it should create the ClientContext for the host web or the app web. Be default, its set to false which created the ClientContext for host web. 

You can also create remote event receivers similar to the sample below:

 

var list = clientContext.Web.Lists.GetByTitle("MyAnnouncements");

var requestProperty = (HttpRequestMessageProperty)

      OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name];

var appWebUrl = "https://" + requestProperty.Headers[HttpRequestHeader.Host];

var eventReceiver = new EventReceiverDefinitionCreationInformation();

eventReceiver.EventType = EventReceiverType.ItemAdding;

eventReceiver.ReceiverName = "AddAnnouncement";

eventReceiver.ReceiverUrl = appWebUrl;

eventReceiver.SequenceNumber = 10000;

list.EventReceivers.Add(eventReceiver);

clientContext.ExecuteQuery();

 

Remote event receivers are a pretty powerful addition to SharePoint 2013. You can not only affect changes in the app web, but you can also make affect changes in the host web as well. One thing to note, anything you want to access whether it is read or write in the host web requires you to set the app permissions to perform those actions