2014-03-08

I was looking into the option of auto deploying an custom developed app at the time of creating a site in O365 / SP2013 environment.

Scenario:

1. Custom app (MVC – Azure hosted) – Need to deploy it automatically so that it can customise the site created using a site template. (i.e. attaches the remote event receiver to a list, site permission configuration, view changes etc).

2. Register and Deploy the app on the azure site using the Client Id and Client Secret. More details can be found at Chris O Brien’s blog – “Deploying SP2013 provider-hosted apps/Remote Event Receivers to Azure Websites (for Office 365 apps)”. It helped me lot in setting up my app and debugging as well.

Available API’s:  After a quick search, found the SPWeb.LoadAndInstallApp method – which is fine in my case as I have the .app for our custom developed app.

What happens when we click an installed app: SharePoint uses the appredirect.aspx page to call the app with the right access token, so that the app can validate and allows the user to access the app. We can also use the appredirect page as “https://SharePointServerName/_layouts/15/appredirect.aspx?client_id=<the app client Id>&redirect_uri=URL you want to redirect to” and more details can be found here.

Step1:

In my custom app, created a view to install the app (.app – published as a part of the app web project).

The above code initiates the deployment and redirects back to the site (SPHostUrl). This endpoint can be invoked using the appredirect page mechanism with the Apps Client Id.

Step2:  After creating the site, call the subsite page containing the below JS to install the app.

Javascript:

var url = String.format(
"{0}/_layouts/15/appredirect.aspx?client_id={1}&redirect_uri=https://***.azurewebsites.net/<<controller>>/<<view method containing the code to install the app>>?{{StandardTokens}}",
<<subsite url>>, "<<client id>>");

window.location.href = url;

The above javascript calls the appredirect.aspx page which then calls the app site as below,

https://***.azurewebsites.net/home?SPHostUrl=<<subsiteurl>>&SPLanguage=en-US&SPClientTag=1&SPProductNumber=**&SPAppWebUrl=<<weburl>>

 Note: The above can be extended to check for the successful installation of the app and display an error message accordingly.

SPWeb.LoadAndInstallWeb equivalent REST / Javascript CSOM is also available here.

 

About the author 

Balamurugan Kailasam