In an old fashion way, provisioning of site columns, content types, lists... was done with declarative coding. Unfortunately, declarative host web provisioning isn't possible. Luckily for us developers, the OfficeAMS contributors are working hard on building nice extension methods to speed up SharePoint development.
in the following example I'm provisioning the host web during the AppInstalled event.
- creating a new list
- binding the remote event receiver to the newly created list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//do not forget to reference officeams.core (http://officeams.codeplex.com) dll to call extension methods. | |
//create settingslist | |
hostWebClientContext.Web.AddList(100, new Guid("00bfea71-de22-43b2-a848-c05709900100"), "Settings", false); | |
//get settings list | |
var settingsList = hostWebClientContext.Web.GetListByTitle("Settings"); | |
//prepare eventreceiver binding | |
var eventReceivers = settingsList.EventReceivers; | |
hostWebClientContext.Load(eventReceivers); | |
hostWebClientContext.ExecuteQuery(); | |
//since this code example is running in a web service class during AppInstalled event, | |
//we can get the remoteweb url from the WebOperationContext | |
//remoteWebUrl will result in: "yourServiceBusName.servicebus.windows.net" | |
var remoteWebUrl = string.Empty; | |
if(null != WebOperationContext.Current) | |
remoteWebUrl = WebOperationContext.Current.IncomingRequest.Headers["Host"]; | |
//little trick to be able to debug our event receiver | |
#if DEBUG | |
remoteWebUrl = String.Format("https://{0}/3458491647/3050950848/obj/f44563bb-02a0-4a5b-b2da-737023b5a03e", remoteWebUrl); | |
#endif | |
var settingsRerAdded = new EventReceiverDefinitionCreationInformation | |
{ | |
ReceiverUrl = String.Format("{0}{1}", remoteWebUrl, "SettingsEvents.svc"), | |
ReceiverName = "SettingsEventReceiverAdded", | |
Synchronization = EventReceiverSynchronization.Synchronous, | |
SequenceNumber = 10000, | |
EventType = EventReceiverType.ItemAdded | |
}; | |
settingsList.EventReceivers.Add(settingsRerAdded); | |
settingsList.Update(); | |
hostWebClientContext.Load(settingsList.EventReceivers); | |
hostWebClientContext.ExecuteQuery(); |
This is why I'm using the #if DEBUG directives of visual studio. If in debug mode, I'm overwriting the absolute .svc url in the following format:
https://server.servicebus.windows.net/computer/account/obj/guid/EventReceiver.svc
This raises the question, what are the computer, account and guid parameters in this URL? I figured out these values by opening the .debugapp package that is generated by visual studio during debugging.
While debugging you can find this package in the debug folder of your project.
Extract this .debugapp package and open the AppManifest.xml file in a text editor.
The InstalledEventEndpoint contains all the values your are looking for.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!--Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9--> | |
<App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest" Name="YourAppName" ProductID="{6b1a2b10-c511-4484-a0f2-d8d247e1c3e7}" Version="1.6.0.8" SharePointMinVersion="15.0.0.0"> | |
<Properties> | |
<Title>YourAppName</Title> | |
<StartPage>https://localhost:44303/Pages/Default.aspx?{StandardTokens}&SPHostTitle={HostTitle}</StartPage> | |
<UpgradedEventEndpoint>https://YourServiceBus.servicebus.windows.net/3458491647/3050950848/obj/f44563bb-02a0-4a5b-b2da-737023b5a03e/Services/AppEventReceiver.svc</UpgradedEventEndpoint> | |
<InstalledEventEndpoint>https://YourServiceBus.servicebus.windows.net/3458491647/3050950848/obj/f44563bb-02a0-4a5b-b2da-737023b5a03e/Services/AppEventReceiver.svc</InstalledEventEndpoint> | |
</Properties> | |
<AppPrincipal> | |
<AutoDeployedWebApplication> | |
<DebugInfo ClientSecret="" AppUrl="" /> | |
</AutoDeployedWebApplication> | |
</AppPrincipal> | |
<AppPermissionRequests AllowAppOnlyPolicy="true"> | |
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" /> | |
<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="FullControl" /> | |
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" /> | |
<AppPermissionRequest Scope="http://sharepoint/taxonomy" Right="Write" /> | |
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" /> | |
</AppPermissionRequests> | |
</App> |
Thanks Sander!
ReplyDeleteFinding out the service bus url like this really helped me!