WEBSITE

SharePoint 2013 and Windows Azure (part 2) - Creating a Simple Autohosted SharePoint App - Building Your First Autohosted App

12/25/2013 2:10:44 AM

Creating a Simple Autohosted SharePoint App

In the Autohosted cloud app model, as discussed earlier, part of the application you create is auto-deployed to Windows Azure and the other part is deployed to SharePoint. To help illustrate this process, create an Autohosted application in the following example.


TRY IT OUT: Building Your First Autohosted App (MyFirstAutohostedApp.sln)

To create your first Autohosted app:

1. Open Visual Studio 2012.

2. Select File, and then New Project.

3. Expand the Office/SharePoint node and select Apps, as shown in Figure 3.

FIGURE 3

image

4. Type MyFirstAutohostedApp as the project name.

5. Select the App for SharePoint 2013 project, and click OK.

6. In the New App for SharePoint dialog, enter the name of the app, the SharePoint site you’ll be using for debugging and deployment, and select Autohosted in the drop-down box, as shown in Figure 4. When done, click Finish.

FIGURE 4

image

7. Right-click MyFirstAutohostAppWeb and select Add New, and then select Class.

8. Call the class People, and then click Add.

9. In the newly added People class, insert the following bolded code.
...    
public class People
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }

}
...
10. Press F6 to build the project.

11. Right-click the Default.aspx page and switch to Source view by clicking the tab in the lower part of the main window.

12. Insert the following bolded line of code into the Default.aspx page.
...

<html >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdViewPeople" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
13. Press F6 to build the project.

14. Double-click the Default.aspx.cs file. This is the code-behind for the Default.aspx page.

15. Insert the following bolded code into the Default.aspx.cs file. Leave all the other code that was created by default there.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyFirstAutohostedAppWeb.Pages
{
public partial class Default : System.Web.UI.Page
{
List<People> myPeeps = new List<People>();

protected void Page_Load(object sender, EventArgs e)
{
var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
var hostWeb = Page.Request["SPHostUrl"];

using (var clientContext = TokenHelper.GetClientContextWithContextToken
(hostWeb, contextToken, Request.Url.Authority))
{
clientContext.Load(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
Response.Write(clientContext.Web.Title);
clientContext.ToString();
}

GeneratePeepsData();
DataBindPeepsData();


}
private void GeneratePeepsData()
{
People clsPeep1 = new People();
clsPeep1.Name = "John Doe";
clsPeep1.Age = 24;
clsPeep1.Email = "john.doe@contoso.com";
myPeeps.Add(clsPeep1);

People clsPeep2 = new People();
clsPeep2.Name = "Jane Doe";
clsPeep2.Age = 22;
clsPeep2.Email = "jane.doe@contoso.com";
myPeeps.Add(clsPeep2);
}

private void DataBindPeepsData()
{
grdViewPeople.DataSource = myPeeps;
grdViewPeople.DataBind();
}

}
}
16. Press F6 to build the project.

17. Right-click MyFirstAutohostedApp, and select Publish. In the Publish Office Apps dialog, select Finish. This builds and packages the SharePoint app, but does not deploy it. Windows Explorer automatically opens when the build and package process is complete. Copy the folder path in Windows Explorer; you’ll need this later on in this exercise.

18. Navigate to your SharePoint Online Developer Site home page, and click New apps to deploy.

19. In the Deploy App dialog, select the upload link, click Browse when prompted, and then click Deploy.

20. When prompted, click Trust It, as shown in Figure 5.

FIGURE 5

image
When the app is installed, click the link to the app. Something similar to Figure 6 appears.

FIGURE 6

image
Congratulations! You’ve built your first Autohosted app.

How It Works

In the earlier exercise where you created a Windows Azure MVC4 application, you used the Windows Azure Cloud template to create the Windows Azure project. However, when you create a SharePoint Autohosted app, the deployment and packaging process automatically manages the deployment of part of the project to SharePoint and the other part to Windows Azure. Therefore, in this exercise MyFirstAutohostedApp was packaged and deployed to SharePoint, and MyFirstAutohostedAppWeb was packaged and deployed to Windows Azure automatically.
Within the Default.aspx page (which is deployed to Windows Azure), you added a simple class object to represent a person. The person had three properties: a name, email address, and age.
  public class People
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
To keep things simple, you then created two helper methods that were called when the default pages were loaded. The first one, GeneratePeepsData, created two instances of the People object, populated the properties, and then added the newly created objects to the List<People> collection object. The second method, DataBindPeepsData, data-bound the List<People> collection object to the data grid you added to the Default.aspx page.

You might be curious to understand how SharePoint knows to point off to the page you deployed to Windows Azure, and how it allows the cross-domain access. This is defined automatically for you in the AppManifest configuration file. You can see the following XML snippet that shows the default remoteAppUrl token (which is auto-created by Visual Studio) and a pointer to the Default.aspx page. The StandardTokens token then adds some additional information to the URL such as SharePoint host URL and language.

   ...  
<Properties>
<Title>MyFirstAutohostedApp</Title>
<StartPage>~remoteAppUrl/Pages/Default.aspx?{StandardTokens}</StartPage>
</Properties>
...

Let’s now walk through a second example that more explicitly splits the Windows Azure piece from the SharePoint piece. In this example, you’ll again use the Autohosted template, but this time you’ll first publish the Windows Azure application you created earlier and then use the Client App Web Part to integrate the Windows Azure application with SharePoint. This is the most lightweight way to integrate Windows Azure with SharePoint (and is tantamount to an iframe object that registers the Windows Azure application with SharePoint).

This example is composed of two parts:

  • An application that has been built and deployed to Windows Azure
  • A lightweight configuration application that will be deployed to SharePoint

For the Windows Azure application, you can use the web page you created and published to Windows Azure in the first exercise or create a new one using the method described earlier for creating and publishing an application to Windows Azure. Either way, you should have a web page that is deployed to Windows Azure. When deployed, you can see from Figure 7 that the URL in SharePoint includes both the SharePoint site URL and the URL for the Windows Azure application.

FIGURE 7

image

Other  
  •  Sharepoint 2013 : SharePoint Installation and Configuration - Create a New Subsite
  •  Sharepoint 2013 : Join a Server to the SharePoint Farm, Create a New Web Application, Create a New Site Collection
  •  Sharepoint 2013 : Install Without a Product Key in the Configuration File, Configure a New SharePoint Farm
  •  Sharepoint 2013 : Prepare the Microsoft SharePoint Installation Module , Install SharePoint Unattended
  •  Sharepoint 2010 : Putting Your Site on the Web - Additional Features
  •  Sharepoint 2010 : Putting Your Site on the Web - Key Terms and Architecture , Richer User Experience
  •  Sharepoint 2010 : Putting Your Site on the Web - Web Content Management (part 2) - Web Publishing 101
  •  Sharepoint 2010 : Putting Your Site on the Web - Web Content Management (part 1)
  •  Sharepoint 2010 : Using an External Data Column, Building a Composite Application
  •  ASP.NET 4 in VB 2010 : Page Tracing (part 3) - Application-Level Tracing
  •  
    Top 10
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS