Create a Provider Hosted SharePoint Add In

By | August 10, 2015

In this article I will be doing a complete walkthrough of creating a simple SharePoint Provider Hosted App using the following frameworks and technologies: Entity Framework 6, Web API, REST and Angular.js. It assumes you are using Visual Studio 2013 and have an Office 365 tenant and Azure account already set up.

To begin with make sure that you have an up to date version of NuGet Package Manager. You can check your version by going into Visual Studio and clicking on Help > About Microsoft Visual Studio.

Machine generated alternative text: Visual Studio  Microsoft Visual Studio Ultimate 2013  Version 12.03072300 Update  2014 Microsoft Corporation.  All rights reserved.  Installed products:  ASP.NET Web Frameworks and Tools 2013  Common Azure Tools I .2  Microsoft Azure Mobile Services Tools I .2  NuGet Package Manager  2.8.50313.46  bout Microsoft Visual Studio  5.2.20703.0  Product license information  Microsoft .NET Framework  Version 4.5151650  2014 Microsoft Corporation.  All rights reserved.  Copy Info  System nfo  Ox Diag  12.03626  Office Developer Tools for Visual Studio 2013 ENU  PowerSheII Tools I .2  PreEmptive Analytics Visualizer I .2  SQL Server Data Tools 12.0.ywg.1  Windows Phone 8.1 SDK Integration 1.0  T..lcln 1 n  Product details:  NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.  Warning: This computer program is protected by copyright law and international treaties. Unauthorized reproduction or  distribution of this program, or any portion of it, may result in severe civil and criminal penalties, and will be prosecuted  to the maximum extent possible under the law.

 

You will need version 2.8.6 or higher. I downloaded the latest version from here:

https://visualstudiogallery.msdn.microsoft.com/4ec1526c-4a8c-4a84-b702-b21a8f5293ca

If you are using Visual Studio 2015 you have a compatible version of NuGet and can skip this step.

Start by creating a new project in Visual Studio 2013

Open Visual Studio (this walkthrough uses 2015 but you can use any version from 2012 onwards)

  • File > New > Project…
  • From the left menu select Templates ‣ Visual C# ‣ Windows
  • Select the Class Library project template
  • Ensure you are targeting .NET 4.5 or later
  • Give the project the Name CustomerOrders.Classes and name the solution CustomerOrders
  • Rename Class1.cs file to Customer.cs

 

Machine generated alternative text: So  lution Explorer  Search Solution Explorer (Ctrl+;)  Solution 'CustomerOrders.CIasses' (I project)  CtstomerOrders.CIasses  Properties  References  Customer.cs  Microsoft Visual Studio  You are renaming a file. Would you also like to perform a rename in this  project of all references to the code element 'Classl

Click Yes

Add the following Code to the class

public class Customer
{
public int Id { get; set;}
public string FirstName { get; set;}
public string LastName { get; set;}
public string Email { get; set; }
public string Phone { get; set; }
public List<Order> CustomerOrders { get; set; }

}

public class Order

{
public int Id { get; set; }
public string ProductName { get; set; }
public string CustomerId { get; set; }
public Customer Customer { get; set; }
}

 

Now create a new project also using the Class library template and call it CustomerOrders.DataModel

Here is where we add the Entity Framework references using NuGet package Manager.

Right click on the new project and click on Manage NuGet Packages…

Machine generated alternative text: Build  Rebuild  Clean  Analyze  Scope to This  New Solution Explorer View  Show on Code Map  Build Dependencies  Manage NuGet Packages...  Set as StartUp Project  Debug  Source Control  Cut  Re move  Rename  Unload Project  Open Folder in File Explorer  Properties  Ctrl+X  Ctrl+!,'  Alt* Enter

Select Online and then search for Entity. Click on Install beside EntityFramework

Machine generated alternative text: D Installed packages  Online  nuget.org  D Updates  Each package is licensed to you by its  cwner. Microsoft is not responsible  for, nor does it grant any licenses to,  third-party packages.  Settings  CustomerOrders.DataModel - Manage NuGet Packages  Stable Only  • Sot by. Most Downloads  Install  entity  Created by: Microsoft  EntityFramework  Version: 6.1 B  Last Published: 3/10/2015  Downloads: 1 1878468  View License  Project Information  Report  Desc ription:  Entity Framework is Microsoft's  recommended data access technology for  new applications.  Microsoft EF Database Data O/RM  ADO.NET  Dependencies:  No Dep endencies  Close  Entity Framework  .NET  Entity Framework is Microsoft's  recommended data access technology for n...  Json.NET  Json.NET is a popular high-performance JSON framework  for .NET  jQueny is a new kind of JavaScript Library.  jQueny is a fast and concise JavaScript Library that simplifies...  Microsoft ASP.NET WC  .NET  This package contains the runtime assemblies for ASP .NET  WebG rease  Web Grease is a suite of tools for optimizingjavascript, css files  and images.  Microsoft HTTP Client Libraries  .NET  This package provides a programming interface for modern  HTTP/REST based applications.  Microsoft ASP.NET Web API 2.2  .NET  This package contains everything you need to host ASP.NET  Web API on IIS.

Now under your project references you should see EntityFramework and EntityFramework.SqlServer

Machine generated alternative text: Solution 'CustomerOrders.CIasses' (2 projects)  CtstomerOrders.CIasses  C*  Properties  References  Customer.cs  Properties  References  EntityFramework  EntityFrameworkSqIServer  Microsoft.CSharp  System  System.ComponentModeI.DataAnnotations  System.Core  System.Data  System.Data.DataSetExtensions  System.XmI  System.XmI.Linq  App.config  C* Classl.cs  packages.config

Rename Class1 to CustomerContext and add the following using statement to the top of the class

using System.Data.Entity;

This class will need to inherit from DbContext so the class definition should look like this:

public class CustomerContext:DbContext
{

}

Now add a reference to our CustomerOrders.Classes project and include a using statement

Machine generated alternative text: D Assemblies  Solution  P rojects  D COM  D Browse  Reference Manager - CustomerOrders.DataModel  Search Solution (Ctrl+E)  CustomerOrders.CIasses  Name  Path  Browse...  Cancel

Here is where we add DbSets that map to the classes we created before.

using System.Data.Entity;

using CustomerOrders.Classes;

namespace CustomerOrders.DataModel
{

public class CustomerContext:DbContext
{

public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }

}

}

 

Install the Entity Framework Power Tools Extension under Tools select Extensions and Updates

Machine generated alternative text: TOOLS TEST ARCHITECTURE  Windows Phone 8.1  Attach to Process...  Connect to Database...  Connect to Server...  Add SharePoint Connection...  Connect to Microsoft Azure...  SQL Server  Code Snippets Manager...  Choose Toolbox Items...  Add-in Manager...  NuGet Package Manager  Extensions and Updates...  Create GUID  Error U•okup  ANALYZE  WINDOW  Ctrl* Alt* p  Ctrl*K, Ctrl*B  PreEmptive Dotfuscator and Analytics  SPF *  SPF* (x64)  WCF Service Configuration Editor  External Tools...  Import and Export Settings...  Customize...  Options...

Search for Entity Framework Power

Machine generated alternative text: D Installed  Online  Visual Studio Gallery  D Controls  D Templates  D Tools  Search Results  D Samples Galley  D Updates (8)  Sot by.  Relevance  Entity Framework Power Tools Beta 4  Preview of useful design-time features for DbContext.  Entity Developer for Entity Framework  Extensions and Updates  Download  I entitv framework power  Created by: Microsoft  Version: o. g. 0.0  •k •k •k •k * (180 Votes)  More Information  Report Extension to Microsoft  e open  Open With—  Entity  Entity Data  View Entity Data XML  v,ew Entity Data Su  Mews  Stww on Code M a  Exclude "om  Close  Entity Developer for Entity Framework is a powerful modeling and code  generation tool for ADO.NET Entity Framework. You can use Model-Firs...  Entity Developer Express  Entity Developer Express is a free of charge powerful modeling and code  generation tool for ADO.NET Entity Framework, NHibernate, and LINQ t...  LLBLGen Pro  The entity modeling solution for your .NET data-access needs. Integrates  seamlessly in Visual Studio (2010+).  dotConnect for MySQL  dotConnect for MySQL is a database connectivity solution built over  ADO.NET architecture.With Entity Framework and LINQ to SQL suppot...  dotConnect for Oracle  dotConnect for Oracle is a database connectivity solution built over  ADO.NET architecture.With Entity Framework and LINQ to SQL suppot...  dotConnect for PostgreSQL  dotConnect for PostgreSQL is a database connectivity solution built over  ADO.NET architecture. With Entity Framework, LINQto SQL suppot it i...  Exchange ADO.NET Provider  The ADO.NET Provider for Exchange gives developers the power to easily  connect .NET applications to Microsoft Exchange data, including Folder...

Click on Download

For more information see: https://msdn.microsoft.com/en-us/data/jj593170

This add in was created by Microsoft and they intend to add these features in future releases of EntityFramework
Once installed it will require you to restart Visual Studio. Make sure to save your work!

Now you must set the CustomerOrders.DataModel to be the Startup project
Now you will be able to View the Entity Data Model if you right click on your class and select Entity Framework > View Entity Data Model (Read-only)

Machine generated alternative text: View Entity Data Model (Read-only)  View Entity Data Model XML  View Entity Data Model DDL SQL  Generate Views  O  So  lution Explorer  Search Solution Explorer (Ctrl+;)  Solution 'CustomerOrders.CIasses' (2 projects)  CtstomerOrders.CIasses  Properties  References  Customer.cs  CustomerOrders.DataModeI  Properties  References  App.config  Open  Open With...  Entity Framework  View Code  View Class Diagram  Scope to This  New Solution Explorer View  Show on Code Map  Exclude From Project  Cut  Copy  Delete  Rename  Properties  Ctrl+X  Ctrl*C  Nplorer Class View  Alt* Enter

 

Machine generated alternative text: Properties  FirstName  LastName  Email  Phone  Navigation Properties  CustomerOrders  P roper-ties  ProductName  Customerld  Navigation Properties

Notice the zero or one – to – many relationship between Customer and Order
We want a one – to – many relationship
To do this add a reference to System.ComponentModel.DataAnnotations to the CustomerOrders .Classes project

Machine generated alternative text: Assemblies  Framework  Extensions  Recent  D Solution  D COM  D Browse  Reference Manager  Targeting: .NET Framework 4.5  CustomerOrders.Classes  Search Assemblies (CtrI+E)  System. Component Model. DataA  nnotations  Created by:  Microsoft Corporation  Version  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.010.0  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  4.o.o.o  Brcwse...  Presentation Framework.RoyaIe  ReachFramework  sysglobl  System  System. Activities  System.Activities.Core.Presentation  System.Activities.DurabIeInstancing  System.Activities.Presentation  System.AddIn  System.AddIn.Contract  System.ComponentModeI.Composition  System Compon entModeI. Composition Regist..  System.Configuration  System.Configuration.InstaII  System.Core  System.Data  System. Data. DataSetExtensions  System.Data.Entiň,'  System. Data. Entity.Design  System.Data.Linq  System.Data.OracIeCIient  System.DataServices  System.DataServices.CIient  System.DataServices.Design  System. DataSqIXmI  4.010.0  File Version:  4030319.18020  Cancel

Then add a using statement to include the reference and now we can set the Required attribute to your Customer property of the Orders class. Your code should now look like this

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace CustomerOrders.Classes
{

public class Customer
{
public Customer() {

CustomerOrders = new List<Order>();

}

public int Id { get; set;}
public string FirstName { get; set;}
public string LastName { get; set;}
public string Email { get; set; }
public string Phone { get; set; }
public List<Order> CustomerOrders { get; set; }
}

public class Order
{

public int Id { get; set; }
public string ProductName { get; set; }
public string CustomerId { get; set; }
[Required]
public Customer Customer { get; set; }
}

}

Now when we view the Entity Data model you will see a one – to – many relationship

Machine generated alternative text: Order  P roperties  ProductName  Navigation Properties  Customer  Properties  FirstName  LastName  Email  Phone  Navigation Properties  CustcmerCrders

Creating the database

In Visual Studio open up the NuGet Package Manager Console Window and enable migrations using the following command :

Enable-migrations

Machine generated alternative text: Package Manager Console  Package source: nuget.org  Default project: CustomerOrders.DataModeI  nor doe  Each package is licensed to you by its owner. Microsoft is not responsible for,  licenses. Follow the package source (feed) URL to determlne any dependencies.  Package Manager Console Host Version 2.8.0723.76S  Type 'get-help NuGet' to see all available NuGet commands.  PFD enable-migrations  Checking if the context targets an existing database...  Code First Migrations enabled for project CustomerOrders .DataModeI.

You should now see a migrations folder in solution explorer

Machine generated alternative text: Solution Explorer  Search Solution Explorer (Ctrl+;)  Solution 'CustomerOrders.CIasses' (2 projects)  CustomerOrders.CIasses  Properties  References  Customer.cs  Properties  References  Migrations  Configuration.cs  App.config  C' CustomerContext.Vlews.cs  packages.config

Back in the Package Manager Console type add-migration “Initial”

Machine generated alternative text: Package Manager Console  Package source: nuget.org  Default project: CustomerOrders.DataModeI  nor does it grant any licenses to,  third-party packages.  Some packag  Each package is licensed to you by its owner. Microsoft is not responsible for,  licenses. Follow the package source (feed) URL to determine any dependencies.  Package Manager Console Host Version 2.8.0723.76S  Type 'get-help NuGet' to see all available NuGet commands.  PFD enable-migrations  Checking if the context targets an existing database...  Code First Migrations enabled for project CustomerOrders .DataModeI.  PM) add-migration "Initial'  Scaffolding migration ' Initial  The Designer Cade -Far this migration file includes a snapshot cf your current Cade First model. This snapshot is used to calculate the changes to yc  additional changes to your model that you want tc include in this migration, then yau can re-sca++cld it by running 'Add-Migraticn Initial' again.

Now it’s time to actually create the database in SQL Server
Run the following command in Package Manager Console

update-database -script

This will generate a script file that you can give to your DBA to run

Now run

update-database -verbose

This will actually create the database

To check what it has created add a connection to your localdb in Visual Studio in Server Manager

Machine generated alternative text: Server Explorer  Azure  Data Connections  Servers  SharePoint Connect  Configuration.cs  Add Connection  C usto me rCo ntext .Views  Enter information to connect to the selected data source or click "Change" to  choose a different data source and/or provider.  Data source:  Microsoft SQL Server (SqICIient)  Server name:  (localdb)lvl I .0  Log on to the server  @ Use Windows Authentication  C,  Use SQL Server Authentication  Llser name:  Passuuord:  Save my password  Connect to a database  @ Select or enter a database name:  custcmerOrders.DeteMcdeI.CustcmerCcntext  C) Attach a database file:  Logical name:  Test Connection  Change...  Refresh  Browse...  Advanced...

Check the app.config file to find the server name

You will see now that it has created your tables and constraints:

Machine generated alternative text: Server Explorer  Azure  Data Connections  Tables  _ Migration History  Customers  FirstName  LastName  Email  Phone  orders  Views  Stored Procedures  Functions  Synonyms  Types  Assemblies

It also creates a Migration table to track the history. As you modify your entities delta updates are made to the schema

Let’s make a small change to our customer class and see how the database gets updated

Open up our Customer.cs class and add a new Fax property

public class Customer
{

public Customer() {
CustomerOrders = new List<Order>();

}

public int Id { get; set;}
public string FirstName { get; set;}
public string LastName { get; set;}
public string Email { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public List<Order> CustomerOrders { get; set; }
}

In Package Manager type add-migration “Add Fax” then
update-database -verbose

Refresh the Customer table in Server explorer and you will see the new column added

Machine generated alternative text: Server Explorer  Azure  Data Connections  cl 102181076XIocaIdb#8fe5cb7b.CustomerOrders.Dat  Tables  _ MigrationHistory  FirstName  LastName  Email  Phone  Fax  orders  Views  Stored Procedures  Functions  Synonyms  Assemblies  Servers  SharePoint Connections

Create a new Console Application Project in the Solution and name it CustomerExe

Machine generated alternative text: Solution Explorer  Search Solution Explorer (Ctrl+;)  Solution 'CustomerOrders.CIasses' (3 projects)  CustomerExe  S' Properties  • CustomerOrders.CIasses  • CustomerOrders.DataModeI  Microsoft.CSharp  System  System.Core  System.Data  System. Data. DataSet Extensions  System.XmI  System.XmI.Linq  App.config  C* Program.cs  CustomerOrders.CIasses  Properties  References  C* Customer.cs  Custo me rde rs. Data  Properties  References  Migrations  201508061917235 Initial.cs  Solution Explorer Team Explorer Class View

And add references to the other two projects and then using Package Manager install the Entity Framework in the Console application by typing

Install-package EntityFramework

Machine generated alternative text: Package Manager Console  Package source: nuget.org  , cauy  PFD Install-package EntityFramework  'EntityFramework 6.1.3' already installed.  Default project:  CustomerExe  CustomerOrders .DataModeI already has a reference to  PFD Install-package EntityFramework  'EntityFramework 6.1.3' already installed.  CustomerOrders .DataModeI already has a reference to  PFD Install-package EntityFramework  'EntityFramework 6.1.3' already installed.  Adding 'EntityFramework 6.1.3' to CustomerExe.  'EntityFramework 6.1.3' .  'EntityFramework 6.1.3' .  Successfully added 'EntityFramework 6.1.3' to CustomerExe.  Type 'get-help EntityFramework' to see all available Entity Framework commands.

You need to switch to the CustomerExe as Default project in the screen above
We can now use the app.config in the CustomerExe project and delete the app.config file in the DataModel project

In the Program.cs file add the following code:

using CustomerOrders.Classes;
using CustomerOrders.DataModel;
using System;
using System.Data.Entity;

namespace CustomerExe
{
class Program
{
static void Main(string[] args)
{
AddCustomer();
Console.ReadLine();
}

private static void AddCustomer()
{
var cust = new Customer {
FirstName = “Patrick”,
LastName = “Clarke”,
Email = “xyz@internet.com”,
Phone = “613 555-5555”,
Fax = “867-5309”
};

using(var context = new CustomerContext())
{
context.Database.Log = Console.WriteLine;
context.Customers.Add(cust);
context.SaveChanges();

}
}
}
}

Run the console application and ensure that the customer has been added

If you get the following error you will need to update the Views
‘System.Data.Entity.Infrastructure.DbUpdateException’ occurred in EntityFramework.dll

Right click the CustomerContext.cs file and then under Entity Framework choose Generate Views

Machine generated alternative text: View Entity Data Model (Read-only)  View Entity Data Model XML  View Entity Data Model DDL SQL  Generate Views  (localdb)XvII.Ol DC14XAdministratorl Custc  ;tomerExeXbinXDebugXCustomerExe.exe'. Sym  ;tomerExeX bi n XDebugXCustomerOrders . CI asse  ;tomerExeX bi n XDebugXCu ers . Dat  ;tomerExeX bi n EntityF 611 '  4.ø.ø  ;tomerExeX bi n EntityF ramework. Sq I Se  O  Open  Open With...  Entity Framework  View Code  View Class Diagram  Scope to This  New Solution Explorer View  Show on Code Map  Exclude From Project  Cut  Copy  Delete  Rename  Properties  C' Configuration.cs  text. V i evi  xplorer  Properti  Ctrl+X  Ctrl+C  Alt* Enter  Do

Check to see that the new Customer has been added to the database

Machine generated alternative text: Server Explorer  Azure  Data Connections  X  dbo.Customers LData) Ha X  Program.cs  Q Max Rows: IDDD  FirstName  Patrick  NULL  C ustomerContext.cs  Customer  Last Name  Clarke  NULL  Email  xy-z@internet.c...  NULL  Phone  613 555-5555  NULL  NULL  cl 102181076XIocaIdb#8fe5cb7b.CustomerOrders.Dat  Tables  _MigrationHistony  Customers  FirstName  LastName

Add the following code to test out the various crud operations using a connected application
using CustomerOrders.Classes;
using CustomerOrders.DataModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace CustomerExe
{
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new NullDatabaseInitializer<CustomerContext>());
AddCustomer();
AddMultipleCustomers();
UpdateCustomers();
GetCustomers();
GetSomeCustomers();
FindCustomer();
DeleteCustomer();
AddCustomerandOrders();
GetCustomerOrders();
Console.ReadLine();
}
private static void AddCustomer()
{
var cust = new Customer {
FirstName = “Patrick”,
LastName = “Clarke”,
Email = “xyz@internet.com”,
Phone = “613 555-5555”,
Fax = “867-5309”

};

 

using(var context = new CustomerContext())
{
context.Database.Log = Console.WriteLine;
context.Customers.Add(cust);
context.SaveChanges();

}
}
private static void AddMultipleCustomers()
{

var cust1 = new Customer
{
FirstName = “Pete”,
LastName = “Townsend”,
Email = “aaa@internet.com”,
Phone = “613 555-2355”,
Fax = “867-598”
};
var cust2 = new Customer
{
FirstName = “John”,
LastName = “Entwistle”,
Email = “ccc@internet.com”,
Phone = “613 444-5555”,
Fax = “345-5909″
};

 

using (var context = new CustomerContext())
{
context.Database.Log = Console.WriteLine;
context.Customers.AddRange(new List<Customer> {cust1, cust2});
context.SaveChanges();

}
}
private static void GetCustomers()
{
using (var context = new CustomerContext())
{
var customers = context.Customers.ToList();
foreach(var cust in customers)
{
Console.WriteLine(cust.FirstName + ” ” + cust.LastName);
}

}

}

private static void GetSomeCustomers()
{
using (var context = new CustomerContext())
{

string fname = “P”;
var customers = context.Customers.Where(c => c.FirstName.StartsWith(fname)).OrderBy(c => c.LastName);
foreach (var cust in customers)
{
Console.WriteLine(cust.FirstName + ” ” + cust.LastName);
}

}

}

private static void UpdateCustomers()
{
using (var context = new CustomerContext())
{

string fname = “P”;
var customer = context.Customers.Where(c => c.FirstName.StartsWith(fname)).OrderBy(c => c.LastName).FirstOrDefault();
customer.Fax = “613 222-6543″;
context.SaveChanges();

}

}
private static void FindCustomer()
{

using (var context = new CustomerContext())
{
var customer = context.Customers.Find(7);
Console.WriteLine(customer.FirstName + ” ” + customer.LastName);

}
}
private static void DeleteCustomer()
{

using (var context = new CustomerContext())
{
var customer = context.Customers.FirstOrDefault();
context.Customers.Remove(customer);
context.SaveChanges();

}
}
private static void AddCustomerandOrders()
{
var cust = new Customer
{

FirstName = “Paul”,
LastName = “Oakenfeld”,
Email = “yyy@internet.com”,
Phone = “613 876-5555”,
Fax = “867-0987”
};

var order1 = new Order
{
ProductName = “Sunglasses”
};

var order2 = new Order
{
ProductName = “Shoes”
};
using (var context = new CustomerContext())
{
context.Database.Log = Console.WriteLine;
context.Customers.Add(cust);
cust.CustomerOrders.Add(order1);
cust.CustomerOrders.Add(order2);
context.SaveChanges();

}
}
private static void GetCustomerOrders()
{
using (var context = new CustomerContext())
{
string lname = “Oakenfeld”;
var cust = context.Customers.Include(c=>c.CustomerOrders)
.Where(c => c.LastName == lname).FirstOrDefault();

Console.WriteLine(cust.FirstName + ” ” + cust.LastName);
foreach(var o in cust.CustomerOrders)
{
Console.WriteLine(o.ProductName);
}
}
}
}

}

 

In the Classes project created a new folder called Interfaces and a new class called IModificationHistory

Machine generated alternative text: CustomerOrders.Classes  C'  Properties  References  Inteffaces  C* ImodificationHistony.cs  Customer.cs

Add the following code to the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomerOrders.Classes.Interfaces
{
class ImodificationHistory
{
DateTime DateModified { get; set; }
DateTime DateCreated { get; set; }
bool IsDirty { get; set; }
}
}

Open the Customers.cs file and ensure that the classes implement the ImodificationHistory interface
Your code should now look like this

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using CustomerOrders.Classes.Interfaces;
using System;

namespace CustomerOrders.Classes
{
public class Customer : ImodificationHistory
{

public Customer() {
CustomerOrders = new List<Order>();
}

public int Id { get; set;}
public string FirstName { get; set;}
public string LastName { get; set;}
public string Email { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public List<Order> CustomerOrders { get; set; }
public DateTime DateModified { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDirty { get; set; }

 

}

 

public class Order : ImodificationHistory
{
public int Id { get; set; }
public string ProductName { get; set; }
[Required]
public Customer Customer { get; set; }
public DateTime DateModified { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDirty { get; set; }
}
}
In our DataModel project open the CustomerContext class and let’s override the OnModelCreating and SaveChanges method to ignore the IsDirty column from being created.

The class should now have the following code:

using System.Data.Entity;
using CustomerOrders.Classes;
using System.Linq;
using System;

namespace CustomerOrders.DataModel
{
public class CustomerContext:DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(c => c.Ignore(“IsDirty”));

base.OnModelCreating(modelBuilder);
}

public override int SaveChanges()
{
foreach (var history in this.ChangeTracker.Entries()
.Where (e => e.Entity is Classes.Interfaces.ImodificationHistory && (e.State == EntityState.Added ||
e.State == EntityState.Modified ))
.Select (e => e.Entity as Classes.Interfaces.ImodificationHistory)
) {

history.DateModified = DateTime.Now;
if(history.DateCreated == DateTime.MinValue)
{
history.DateCreated = DateTime.Now;
}
}

int result = base.SaveChanges();
foreach(var history in this.ChangeTracker.Entries()
.Where(e => e.Entity is Classes.Interfaces.ImodificationHistory)
.Select (e => e.Entity as Classes.Interfaces.ImodificationHistory)
)
{
history.IsDirty = false;
}
return result;
}

}

}

In Package Manager type add-migration “Add Modified History Columns” then

update-database -verbose

Refresh the Customer table in Server explorer and you will see the new column added

Machine generated alternative text: Server Explorer  Azure  Data Connections  Tables  _MigrationHistony  Customers  Id  First Name  LastName  Email  Phone  Fax  Date Modified  DateCreated  Id  Product Name  Customer Id  DateModified  DateCreated

Notice that the IsDirty column is not included in the tables

Creating the DisconnectedRepository

In the DataModel project add a new class called DisconnectedRepository

Machine generated alternative text: CustomerOrders.DataModeI  C'  C'  C'  Properties  References  Migrations  CustomerContext.cs  CustomerContext.Views.cs  DisconnectedRepositony.cs

Add the following code:

using System.Data.Entity;
using CustomerOrders.Classes;
using System.Linq;
using System;
using System.Collections.Generic;

namespace CustomerOrders.DataModel
{
public class DisconnectedRepository
{
public List<Customer> GetCustomers()
{
using(var context = new CustomerContext ())
{
return context.Customers.AsNoTracking().OrderBy(n => n.LastName).ToList();
}
}

public Customer GetCustomerOrders(int id)
{
using (var context = new CustomerContext())
{
return context.Customers.AsNoTracking().Include(n => n.CustomerOrders).FirstOrDefault(n => n.Id == id);

}
}

public Customer GetCustomerById(int id)
{
using(var context = new CustomerContext ())
{
return context.Customers.AsNoTracking().SingleOrDefault(n => n.Id == id);
}
}

public void SaveUpdatedCustomer(Customer cust)
{
using (var context = new CustomerContext())
{
context.Entry(cust).State = EntityState.Modified;
context.SaveChanges();
}
}

public void SaveNewCustomer(Customer cust)
{
using(var context = new CustomerContext ())
{
context.Customers.Add(cust);
context.SaveChanges();
}
}

public void DeleteCustomer(int id)
{
using (var context = new CustomerContext())
{
var cust = context.Customers .Find (id);
context.Entry(cust).State = EntityState.Deleted;
context.SaveChanges();
}
}

public void SaveNewOrder(Order order, int custId)
{
using (var context = new CustomerContext())
{
var cust = context.Customers.Find(custId);
cust.CustomerOrders.Add(order);
context.SaveChanges();
}
}
public void SaveUpdatedOrder(Order order, int custId)
{
using (var context = new CustomerContext())
{
var orderFromDb =
context.Orders.Include(n => n.Customer).FirstOrDefault(o => o.Id == order.Id);
context.Entry(orderFromDb).CurrentValues.SetValues(order);
context.SaveChanges();

}
}

}

}

Creating the Web API

Select New Project from the Start page. Or, from the File menu, select New and then Project.

In the Templates pane, select Installed Templates and expand the Visual C# node. Under Visual C#, select Web. In the list of project templates, select ASP.NET Web Application. Name the project “CustomerOrders.WebAPI” and click OK.

Machine generated alternative text: D Recent  Installed  Visual Basic  Visual C#  Store Apps  Windows Desktop  Web  Visual Studio 2012  Office'S hare Point  Cloud  LightSwitch  Reporting  Silverlight  Workflow  Visual C++  Visual F#  SQL Server  JavaScript  .NET Framework 4.5  Add New Project  Sot by. Default  Search Installed Templates (Ctrl+E)  Type: Visual C#  A project template for creating ASP.NET  applications. You can create ASP.NET Web  Forms, MVC, or Web API applications and  add many other features in ASP.NET.  Add Application Insights to Project  Microsoft recommends adding  Application Insights telemetry to help you  understand and optimize application  performance.  patrick@12thwave.com  Access to Azure Active Directory •  Use different account  Send telemetry to:  New Application Insights resource  Data will be sent to an Application Insights  resource named after this project in a  resource group named 'Default-  ApplicationInsights-CentraIUS'.  Configure settings  Brc',væ...  Cancel  ASP .NET Web Application  Click here to go online and find templates.  Visual C#  D Online  Locatio n:  CustomerOrders.WebAP  C: .CIasses

Select the Web API project template

Machine generated alternative text: Select a template:  New ASP.NET Project  Web API  CustomerOrders.WebAPl  A project template for creating RESTfuI HTTP services that  can reach a broad range of clients including browsers and  mobile devices.  Learn more  Change Authentication  Authentication: Individual User Accounts  Microsoft Azure  VI Host in the cloud  We bsite  Signed in as patrick@12thwave.com  Manage Subscriptions  Empty  Single Page  Application  Web Forms  Azure Mobile  Service  Add folders and core references for:  Web Forms MVC Web API  Add unit tests  Test project name:  CustomerOrders.WebAPI.Tests

I unchecked the option to Host it in the cloud and to Add unit tests

If all goes well you Azure will tell you that the website is ready

Add references to our Classes and Datamodel projects to the new WebAPI project

Machine generated alternative text: D Assemblies  Solution  P rojects  D COM  D Browse  Reference Manager - CustomerOrders.WebAPl  Search Solution (Ctrl+E)  Name  CustomerExe  CustomerOrders. Classes  CustomerOrders.WebAP..  Path  CAWorkXCustomerOrders.CIassesXCustomer CustomerOrders.DataModeI  C:lWorklCustomerOrders. Classes', Customer  C:lWorklCustomerOrders.CIasseslCustomer  Browse...  Cancel

Update the web.config file connection string to the following:

<connectionStrings>

<add name=”CustomerContext” providerName=”System.Data.SqlClient” connectionString=”yourconnectionstring”/>

</connectionStrings>

If you select the database in Server explorer you can find the connection string in the properties window

Machine generated alternative text: Properties  CestomerContext (CustomerOrders.WebAPI) Connection  Data I .D;InitiaI  Provider  State  .NET Framework Data Provider for SQL Server  Closed

Set the WebAPI project to be the startup project

Add a new controller in the Controllers folder called CustomersController selecting the Web API 2 Controller with actions, using Entity Framework template

Machine generated alternative text: Installed  D Common  Controller  Add Scaffold  MVC 5 Controller - Empty  MVC 5 Controller with read/write actions  MVC 5 Controller with views, using Entity Framework  Web API 2 Controller with actions, using  Entity Framework  by Microsoft  v2.o.o.o  A Web API controller with REST actions to  create, read, update, delete, and list entities  from an Entity Framework data context.  Id: ApiControIIerWithContextScaffoIder  Web API 2 Controller — Empty  Web API 2 Controller with actions, usi Ent•  Web API 2 Controller with read/write actions  Framework  Add  Cancel  Web API 2 OData Controller with actions, using Entity Framework  Web API 2 OData Controller with read/write actions  Click here to go online and find more scaffolding extensions.

Machine generated alternative text: Model class:  Add Controller  Customer (CustomerOrders.CIasses)  Data class: CustomerContext (CustomerOrders.DataModeI)l  Use async controller actions  Controller name:  CustomersControIIer  Cancel

Run the Project to see if the API works as is using Chrome as IE by default does not show the data in the browser

Machine generated alternative text: OE8UG там TOOLS  Goog|e Chrome •

You will need to add /api/Customers to the URL when the browser opens

You should see something like this in your browser

Machine generated alternative text: localhost:37613/api/custc X  —2' C localhost:37613/api/customers  This n•1L file does not appear to have any style irlformation associated "ith it_ The document tree is shoum below _  "ArrayCfCustcmer xmlns : i—"http://www.wS . arg/ 2001/XMLSchema—instance" xmlns—"http://schemas . datacantract . arg/ 2004/07/ CustcmerCrders  CDateCreated : / / schemas . datacantract . arg/ 2004/07 /CustcmerCrders . Classes . Interfaces" TOO : 00 : OOC/DateCreatecB  CDateMcdified : / / schemas . dataccntract . arg/ 2004/07 /CustcmezCrders . Classes . : 00 :  -Z I —Dirty xmlns—"http://schemas . datacantract . arg/ 2004/07/ CustcmerCrders . Classes . Interfaces»faIsec/IsDirt'D  Gus t /  cDatecreatedM900-02-02T00 : OO :  cDateMcdifiedM900-02-02TOO : OO :  -'Emar»xyzunternet . correc/EmaiD  -'Is Dirt 'DfaIsec/IsDirt'D  Cas / Las  / Phone  SSS-SSSS  Gus t  CDateCreated : / / schemas . datacantract . arg/ 2004/07 /CustcmerCrders . Classes . Interfaces" TOO : 00 : OOC/DateCreatecB  CDateMcdified : / / schemas . dataccntract . arg/ 2004/07 /CustcmezCrders . Classes . : 00 :  -Z I —Dirty xmlns—"http://schemas . datacantract . arg/ 2004/07/ CustcmerCrders . Classes . Interfaces»faIsec/IsDirt'D  Gus t /

I like to see the output in JSON format so in a post on StackExchange someone noted that if you add the following to your Global.asax file you will get JSON by default

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

}

Machine generated alternative text: 59 B" "CustamerOrders"  , • "łgoo-oł-ołzoo : OO : ,  : 7 , "FźrstName" • , "LastName" • "Zawnsend"  59 B" "CustamerOrders"  , : "łgoo-oł-ołzoo : OO : ,  localhost:37613/api/custc X  localhost: 37613/api/custc  C localhost:37613/api/customers  : 10, : • "C1arke", "Emai1": . cam", : 555-5555", • "B67-  5309"  5309"  "Cus rs "  , • "łgoo-oł-ołzoo : OO : 00" , • "łgoo-oł-ołzoo : OO : 00" ,  : falsey,  . cam", 444-5555", "345-  • B, "üahn" • "EntwźstIe", "EmaźI"•"  "Cus r s "  , • "łgoo-oł-ołzoo : oo : 00" , • "łgoo-oł-ołzoo : oo  12, • "üahn" "LastName" • "EntwźstIe", . cam  "Cus r s "  , • "łgoo-oł-ołzoo : oo : 00" , • "łgoo-oł-ołzoo : oo  . cam ,  13, "FźrstName" • "PauI" , "LastName" • "OakenfeId" , "EmaźI" • "  "Cus r s "  , : "łgoo-oł-ołzoo : oo : 00" , • "łgoo-oł-ołzoo : oo  • 9, "PauI", "LastName" • "Rudd" . cam  " 555-09B7"  "Cus r s "  , • "łgoo-oł-ołzoo : oo : 00" , • "łgoo-oł-ołzoo : oo  II, • "Zawnsend", "EmaźI"•"  . 555-2355",  : falsey,  : falsey,  : falsey,  • "666-  : falsey,  : falsey,  : falseYJ  . cam"

To help troubleshoot and inspect the JSON I installed the free tool Fiddler http://www.telerik.com/download/fiddler

Machine generated alternative text: Statstcs Inspectors  AutoResponder Composer Log  xv w Auth Cookes Raw  Headers TextVlevv WebForms He •e  Request Headers  GET /api/customers/13 HTTP/I  Accept: text/html, applicaton/xhtml+xml, *l*  Accept-Encoding: gap, deflate  Accept-Language: en-LlS  User -Agent: MoziIIa/5. O (Windows FIT 6.3; WOW64; Trident/7. O; rv:ll. O) like Gecko  T ransport  Connecton: Keep-Alive  Host: localhost:37613  Filters  _ISON  w  XML  View  Get SyntaxVlevv  Tr an sformer  Headers  TextVlevv  ImageVlevv  HexVlevv  Caching  Cook es  Raw  _ISON  XML  Customer Or der s  Fax-8€7-og87  FirstName —Paul  Id-13  IsDirty—FaIse  LastName —Oakenfeld  Phone -613 876-5555

Using Package Manager run Install-Package Microsoft.AspNet.WebApi.Cors make sure to select the WebAPI project

Machine generated alternative text: Package Manager Console  Package source: nuget.org  Default project: CustomerOrders.WebAPI  Package Manager Console Host Version 2.8.0723.76S  Type 'get-help NuGet' to see all available NuGet commands.  PFD Install-package Microsoft .AspNet.WebApi .Cors

Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.

public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

// Web API routes
config.MapHttpAttributeRoutes();

config.EnableCors();

config.Routes.MapHttpRoute(
name: “DefaultApi”,
routeTemplate: “api/{controller}/{id}”,
defaults: new { id = RouteParameter.Optional }
);

}

I have updated the controller to use our DisconnectedRepository class:

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
using CustomerOrders.Classes;
using CustomerOrders.DataModel;
using Newtonsoft.Json;

namespace CustomerOrders.WebAPI.Controllers
{
public class CustomersController : ApiController
{

DisconnectedRepository db = new DisconnectedRepository();

// GET: api/Customers
public IEnumerable<Customer> Get()
{
return db.GetCustomers();
}

// GET: api/Customers/5
[ResponseType(typeof(Customer))]
public IHttpActionResult GetCustomer(int id)
{
Customer customer = db.GetCustomerById(id);
if (customer == null)
{
return NotFound();
}

return Ok(customer);
}

public void PutCustomer([FromBody] object customer)
{
var asCustomer = JsonConvert.DeserializeObject<Customer>(customer.ToString());
db.SaveUpdatedCustomer(asCustomer);
}

public void PutCustomer(int id, [FromBody] Customer customer)
{
db.SaveNewCustomer(customer);
}

// DELETE: api/Customers/5
[ResponseType(typeof(Customer))]
public IHttpActionResult DeleteCustomer(int id)
{
db.DeleteCustomer(id);

return Ok();
}
private bool CustomerExists(int id)
{
return db.GetCustomers().Count(e => e.Id == id) > 0;
}
}
}

Publishing the WebAPI to Windows Azure

Right click on the CustomerOrders.WebAPI project and then click on Publish

Select Microsoft Azure Websites

Machine generated alternative text: Publish Web  Profile  Connection  Settings  Preview  ublish We  Select a publish target  Microsoft Azure Webs ites  I mport  Custom  v More Options  Find other hosting options at our web hosting gallery  Publish  Prev  Next

Create a new Web Application and a new database server if one does not already exist

Machine generated alternative text: Create site on Microsoft Azur  Create a site on Microsoft Azure  Learn more  Sign Out  Site name:  Subscription:  Signed in as patrick@12thwave.com  .azurewebsites.net  Pay - As- You- Go  Create new server  Patrick  Region:  Database server:  Database username:  Database password:  Confirm password:  If you have removed your spending limit or you are using Pay As You Go, there  may be monetary impact if you provision additional resources. legal terms

Review the various settings and then Click on Publish

Once finished you should see the new website and database in Azure

Install Download Latest SQL Server Data Tools to connect to SQL Server

https://msdn.microsoft.com/en-us/library/mt204009.aspx

Once installed you will be able to manage your Azure SQL Server database from within Visual Studio

Machine generated alternative text: Server Explorer  Azure  Mobile Services  191 Notification Hubs  SQL Databases  Websites  1

Connect to the database. The system will prompt you for your database credentials that you specified earlier.

Using SQL Server Object Explorer expand the Customers table and add some dummy data. Republish the WebAPI project and check that you are seeing the data in JSON format

Machine generated alternative text: dbo.Customers LData) Ha X  Default.aspx.cs  Max Rows: IODO  Default.aspx  CustomersControIIer  Solution Explorer  Search Solution Explore  Solution 'Custome  PublishScripts  CustomerExe  CustomerOrd'  CustomerOrde  CustomerOrde  CustomerOrde  CustomerOr  S' Properties  4567", "Fax" • "999 234-  : false),  "Fax" • "432  : false))  FirstName  Gath  Elton  NULL  NULL  Phone  777 345-4567  234 345-4567  NULL  egg 234-3456  432 345-4567  NULL  DateCreated  1/1/1900 120&...  1/1/1900 12CO...  NULL  Last Name  Brooks  John  NULL  Email  gbrooks@broo...  ejohn@gmail.c...  NULL  DateModified  1/1/1900 120&...  1/1/1900 12CO...  NULL  customersrest.azurewebsit X  C customersrest.azurewebsites.net/api/customers  : 2, "FirstName" : "Garth", "LastName" : "Brocks", "Email" : "gbraakstbraaks . cam", "Phone" : "777 345—  34 S E" "CustomerCrders"  t) , • "2900-02-02100 : OO : 00" , "Datecreated" • "2900-02-02100 : OO  : 2, • "Elton", "John" "Email": . cam", "Phone": "234 345-4567"  4 S €7" "CustomerCrders"  t) , : "2900-02-02100 : OO : 00" , "Datecreated" : "2900-02-02100 : OO

Creating the Provider Hosted SharePoint Add In

Log into Office 365 and create a new Developer SharePoint site

Under Admin click on SharePoint

Machine generated alternative text: ADMIN  Exchange  Skype for Business  Yammer

Under Site Collections Click on New -> Private Site Collection

Machine generated alternative text: Site Collections  New Delete Properties Cn•mers Sharing Sto e Buy Server Rescurce Upgrade  Storage  Manage  Private Site Collection  Rename Add  Website Domain  Public Website  Recyde  Restore  Public Website  Ching the maximum resource usage limit.  Private Site Collection  Sea Create a new Site Collection to collaborate with  partners and people within your organization.  C) URL

Give the Site Collection a the title Customer Orders

Machine generated alternative text: new site collection  Title  Web Site Address  Template Selection  Time Zone  Administrator  Storage Quota  Server Resource Quota  Custom  Customer Orders  https://12thwave.sharepoint.com  'sites/  V co  2013 experience version will be used  Select a language:  English  Select a template:  Collaboration  Enterprise  Team Site  Develo er Site  Project Site  Community Site  Publishing  A site for developers to build, test and publish apps for Office  (UTC-08:OO) Pac-fic Time (I-IS and Canada)  resources of 100 resources available  OK  Cancel

Click on OK

Create a new project in the solution. Select Office/SharePoint -> Apps -> App for SharePoint

Give it a name of CustomerOrders.App

Machine generated alternative text: D Recent  Installed  Visual Basic  Visual C#  Store Apps  Windows Desktop  Web  Visual Studio 2012  Office'S hare Point  Apps  Office Add- ins  SharePoint Solutions  Cloud  LightSwitch  Reporting  Silverlight  Workflow  Visual C++  .NET Framework 4.5  App for Office  Add New Project  • Sort by. Default  Search Installed Templates (Ctrl+E)  Type: Visual C#  A project to create an app for SharePoint  2013.  Brc',væ...  Cancel  Visual C#  Visual C#  Visual C#  App for SharePoint  Cloud Business App  Click here to go online and find templates.  D Online  Locatio n:  CustomerOrders.A  C: IWorkXCustomerO rders .CIasses

Click OK

Specify your new Site Collection URL

Machine generated alternative text: New app for SharePoint  Specify the app for SharePoint settings  What SharePoint site do you want to use for debugging your app?  htt cint.ccm/sites/cc  Don't have a developer site?  Sign up for an Office 365 Developer site to test and deploy apps for Office and SharePcint  How do you want to host your app for SharePoint?  @ Provider- hosted  C,  SharePoint- hosted  Learn more about this choice  Previous

Choose Provider-hosted then click on Next

Choose ASP.Net Web Forms Application

Machine generated alternative text: New a  Specify the web project type  for SharePoint  A cloud app for SharePoint consists of an app for SharePoint that is deployed directly to a SharePoint site and a  separately deployed web application.  Which type of web application project do you want to create?  @ ASP.NET Web Forms Application  O ASP.NET WC web Application  Previous  Finish  Cancel

Click Next

Select Use Windows Azure Access Control Service (for SharePoint cloud apps)

Machine generated alternative text: ew app f  Configure authentication settings  How do you want your app to authenticate?  harePoi  Browse...  @ Use Windows Azure Access Control Service (for SharePoint cloud apps)  C,  Use a certificate (for SharePoint on-premises apps using high-trust)  Certificate location:  Password:  Issuer ID:  Previous  Next

Click Finish

Once the Project is created (VS might as you to login to Office 365) add references to the following projects

Machine generated alternative text: D Assemblies  Solution  P rojects  D COM  D Browse  Reference Manager - CustomerOrders.AppWeb  Path  Search Solution (Ctrl+E)  CustomerExe  CustomerOrders.App  CustomerOrders.DataM..  CustomerOrders.WebAPI  CAWorkXCustomerOrders.CIassesXCustomer CustomerOrders.CIasses  C:lWorklCustomerOrders. Classes', Customer  C: IWorklCustomerOrders.CIasseslCustomer  C: rders .CIassesXCustomer  Browse...  Cancel

The Visual Studio Engineering team have developed AngularJS intellisense. Download the js file here : https://raw.githubusercontent.com/jmbledsoe/angularjs-visualstudio-intellisense/master/src/Scripts/angular.intellisense.js

And then copy it to

(x86)\Microsoft Visual Studio 12.0\JavaScript\References folder

Using Package Manager type Install-Package AngularJS.Core to download the various angular scripts. Make sure to select the CustomerOrders.AppWeb project. Install Bootstrap by typing Install-Package bootstrap. Install the latest version of JQuery by typing Install-Package jquery

Machine generated alternative text: Package Manager Console  Package source: nuget.org  Save- Help  Update- Help  PFD Install-package AngularJS.Corel  Default project: CustomerOrders.App

Open the AppManifest.xml file. On the Permissions tab, specify the Site Collection scope and the Read permission level.

Machine generated alternative text: AppManifest.xmI* -E X Default.aspx  dbo.Orders (Data)  CustomersControIIer.cs*  The properties of the deployment package for your app are contained in the app manifest file. You can use the Manifest Designer to set (  General  Permissions  Prereq uisites  Supported Locales  Remote Endpoints  Specify the permissions that your app for SharePoint will request from the user at installation time.  Allow the app to make app-only calls to SharePoint. Learn more about app authentication policy.  Scope  Site Collection  Permission  Pro perties

Set the CustomerOrders.App project to be the start up project and then hit F5

When prompted click on Yes to trust the self-signed certificate

Machine generated alternative text: Security Alert  Do you want to trust the self -signed Localhost certificate?  This app will be hosted on https (https://localhost.r) during debugging and will use the following certificate:  Certificate details:  Issued By: CNzIocaIhost  Issued To: CNzIocaIhost  Valid from: 10/14/2014 AM to 10/13/2019 PM  View Certificate  Click Yes to trust the certificate on this machine and unblock the app for debugging.  What are the risks?

You will be prompted to log into your Office 365 account and then asked whether or not you trust the new Add In

Machine generated alternative text: Do you trust CustomerOrders.App?  Let t read items in this site collection.  Let t access basic information about the users of this site.  CustomerOrders.App  Trust It  Cancel

Click on Trust It

You should see the title of your Add-In in the page.

Click on Deploy your web project

Machine generated alternative text: Publish your app  Hosting type: Provider- hosted  Publish  Current profile: Customer Orders Prod  Deploy your web p roJect  Perform this step if:  • You are publishing the app for the first time.  • You have made changes to the web project, but have not deployed those changes to the live site yet.  If your app is already in use, be sure that existing app packages continue to work with the new web  content. Learn more.  ac the a  Perform this step if you've made changes to the app proJect.  Learn more about deployment options for your app.  Office Store  WISIt the Seller Dashboard  Submit your app for listing to the Office Store, manage Client IDs, or view customer usage metrics.

Create a new app in Azure. Give the Site name CustomersApp and this time we do not need a database

Machine generated alternative text: Create site on Microsoft Azure  Create a site on Microsoft Azure  Learn more  Sign Out  Site name:  Region:  Signed in as patrick@12thwave.com  .azurewebsites.net  No database  Database server:  Database username:  Database password:  If you have removed your spending limit or you are using Pay As You Go, there  may be monetary impact if you provision additional resources. legal terms

Click on Create

Machine generated alternative text: Publish Web  Profile  Connection  Settings  Preview  CustomersApp  Publish method:  Server:  Site name:  User name:  Passwo rd:  ublish We  Web Deploy  customersapp.scm.azurewebsites.net:443  CustomersApp  SCustomersApp  Save password  Publish  Destination URL: http://customersapp.azurewebsites.net  Validate Connection  Prev

Click Publish

Open a browser and navigate to the SharePoint Developer Site Collection and add the following to the site URL:

/_layouts/15/AppRegNew.aspx

http://sitecollection/_layouts/15/AppRegNew.aspxMachine generated alternative text: Client 'd:  Generate  Client Secret:  Generate  App Domain:  Example: "www.contoso.com"  Redirect URI:  Example: "https•//www.contoso.com/default.aspx"

Generate the GUIDs and paste the values in the corresponding fields in the publishing profile

For the domain you are entering the domain of your azure web i.e http://yourapp.azurewebsites.net/

Machine generated alternative text: Publish apps for Office and SharePoint  Set app identity  Profile: Customer Orders Prod  To prepare your app for SharePoint, please answer the following questions. If ycnu need to get Client  ID and Client Secret values, see this help topic.  What is the identity of your app?  Client ID:  Client Secret:  Previous  Next

Click on Finish

Right click on the CustomerOrders.App project and click on publish

Machine generated alternative text: Publish your app  Hosting type: Provider- hosted  Publish  Current profile:  CustomersApp  Deploy your web proJect  Perform this step if:  • You are publishing the app for the first time.  • You have made changes to the web project, but have not deployed those changes to the live site yet.  If your app is already in use, be sure that existing app packages continue to work with the new web  content. Learn more.  ackage the a  Perform this step if you've made changes to the app proJect.  Learn more about deployment options for your app.  Office Store  the Seller Dash  Submit your app for listing to the Office Store, manage Client IDs, or view customer usage metrics.

Click on Package the app

Machine generated alternative text: ublish apps  Package the app  Where is your website hosted?  Offi  SharePoi  The URL is used as an entry point into your live site.  htt s://custcmerse  What is the app's Client ID?  This value must match the Client ID of the web project deployed to the live site. Learn more.

Add the Client ID you created above

Click on Finish

This will created an .app file

Machine generated alternative text: Share  View  Work  CustomerOrd  Home  Favorites  Desktop  Recent places  Downloads  ThisPC Local Disk  Name  CustomerOrders.App.app

Open your app catalog site collection and upload the .app file you just created

Machine generated alternative text: Office 365  Sites  Home  Apps for SharePoint  More  Find a file  App Version  BROWSE  Recent  FILES  LIBRARY  New  All Apps  Upload  Featured Apps  Title  Sync Share  Unavailable Apps  Name  Documents  Workflow Tasks  Nintex Workflow for  Office 365  Apps for SharePoint  Apps for Office  App Requests  Site Contents  p  Edit Product ID  167  Product ID : f67AOB662-58D7-4EAO-9E7E-46DF2EBA36DF) (1)  CustomerOrders.App CustomerOrders.App  I.o.o.o

Add the app to the developer site collection you created earlier.

Machine generated alternative text: Customer Orders  EDIT LINKS  Site Contents  Lists, Libraries, and other Apps  a  a  CustomerOrders.App  Site Pages  3 items  Modified 21 hours ago

Now if you click on the CusomterOrders.App you should see that the URL now points to your Azure site rather than local host

Machine generated alternative text: https:i'i'cLEtc:rnEÆapp.azureweb5ite5.net;'Pagäi'DEfauIt.B43x?SPHc:ftUrIzhttpfg3A%2 p •

Log into your Azure Portal and select your CustomersREST web app

Machine generated alternative text: Microsoft Azure  ALL ITEMS  WEB APPS  VIRTUAL MACHINES  MOBILE SERVICES  CLOUD SERVICES  BATCH SERVICES  all items  NAME  12thwaveTestSite  CustomersApp  CustomersREST  CustomersREST db  12thwave

Click on Configure

Configure your authentication if you have not done so already

Machine generated alternative text: authent•cation / author•zation  AZURE ACTIVE DIRECTORY  AZURE ACTIVE DIRECTORY APPLICATION  LOGOUT PATH ENVIRONMENT VARIABLE  remove  12thwave  CustomersREST (https://customersrestazurewebsites.net)  WEBSITE AUTH LOGOUT PATH

Go back and click on the CustomersApp web app and perform the same steps as above

Click on Configure

Machine generated alternative text: permiss•ons to other applicat•ons  Delegated Permissions: 1  Windows Azure Active Directory  Add application  Application Permissions: 0

Click on Add application

Machine generated alternative text: Permissions to other applications  p  SELECTED  CustomersR EST  SHOW  All Apps  STARTING WITH  APPLICATION PERMISSIONS  x  x  DELEGATED PERMISSIONS  API Explorer  CustomersAp p  Cu stomersR EST  Microsoft Visual Studio O...  Office 365 Exchange Online  Office 365 Management„.  Office 365 SharePoint Onl „  Office 365 Yammer

Show All Apps and then select the CustomerREST application

Select the Access CustomersREST Delegated Permission

Machine generated alternative text: permiss•ons to other applicat•ons  Delegated Permissions: 1  Delegated Permissions: 1  Access CustomersREST  Windows Azure Active Directory  CustomersREST  Application Permissions: 0  Application Permissions: 0

Add your account as an Owner of the App

Machine generated alternative text: DASHBOARD  NAME  USERS  CONFIGURE  OWNERS  USER NAME

Add the solution to GitHub

Right click on the Solution file and click on

Machine generated alternative text: LEI  Build Solution  Rebuild Solution  Deploy Solution  Clean Solution  Run Code Analysis on Solution  Batch Build...  Configuration Manager...  Manage NuGet Packages for Solution...  Enable NuGet Package Restore  New Solution Explorer View  Show on Code Map  Calculate Code Metrics  Project Dependencies...  Project Build Order...  Set StartUp Projects...  Add Solution to Source Control...  Rename  Open Folder in File Explorer  Pro perties  Ctrl+Shift+B  Ctrl+!,'  Alt* Enter

Select Git and then click on OK

Machine generated alternative text: Choose Source Control  Choose a source control system for the new project:  O  Team Foundation Version Control  Team Foundation Version Control uses a single centralized server repository to track and version files. Local  changes are always checked in to the central server where other developers can get the latest changes.  @ Git  Git is a distributed version control system that uses a local repository to track and version files. Changes are  shared with other developers by pushing and pulling changes through a remote, shared repository.  use the selected system when creating new projects in the future

Machine generated alternative text: Changes  I CustomerOrders.CIasses (Local)  Ccnfigureyour user name and email address before committing changes.  Unwncecl  Branch:  Commit Actions  Included Changes (523)  Exclude All Options  'l C:lWorklCustomerOrders.CIasses  .nuget  NuGet.Config (add)  NuGet.exe (addl  NuGet.targets (add)  CustomerExe  Properties  c* Assemblylnfo.cs (add)  App.config (add)  CustomerExe.csproj (add)  packages.config (add)  c* Program.cs (addl  'l CustomerOrders.App  Applcon.png (add)

Install the third party Git commands

Machine generated alternative text: Team Explorer - Home  Search i'%.icrk Itenv, (Ctrl* '3  Home  CustomerOrders.CIasses (Local)  Install 3rd-party Git command prompt tools.  Help Don't prompt again  Project  Changes  Unsynced Commits  Branch: master New... Open...  CtstomerOrders.CIasses.sIn  Branches  Settings  p

Click Install

Machine generated alternative text: Team Explorer - Install 3rd-party tools  Search I'%.icrk Item', (Ctrl* '3  Install 3rd-party tools  I CustomerOrders.CIasses (Local)  p  Third-party tools will be installed using Web Platform Installer (WebPI). Please  read the WebPI license terms below:  By downloading and using the Web Platform Installer (WebPI), you agree to the  licen-,E and privacy statement for WebPI. This installer will contact  Microsoft over the Internet to retrieve product information. Some of the Microsoft  software obtained through WebPI may use Microsoft's Visual Studio Experience  Improvement Program (VSEIP). To view which software uses VSEIP, see here.  Install

Click Install

Machine generated alternative text: eb Platform Installer 5.  Git for Windows (x86)  Git is a powerful distributed Source Code Management tool.  More information  Publisher: msysgit  Download Size: 16.99 MB  Version: 19.5  Release date: Thursday, December 18, 2014  Items to be installed  Install  1  Options

Click Install

Create the Angular.js App

Add the Angular JS Resource NuGet Package to the CustomerOrders.AppWebProject

Machine generated alternative text: D Installed packages  Online  nuget.o rg  Microsoft and .NET  Search Results  D Updates  Each package is licensed to you by its  cwner. Microsoft is not responsible  for, nor does it grant any licenses to,  third-party packages.  Settings  CustomerOrders.AppWeb - Manage NuGet Packages  Stable Only  • Sort by. Relevance  angular  Created by: The AngularJS Team  AngularJS.Resource  Version: I .43  Last Published: 7/21/2015  77348  Project Information  Report  Desc ription:  See the AngularJS.* packages for other  Angular modules  angular angularjs  AngularJS.Core (2 1.43)  Each item above may have sub-  dependencies subject to additional license  agreements.  Close  AngularJS U I Bootstrap  Native AngularJS (Angular) directives for Bootstrap. Small  footprint (5kB gzipped!), no 3rd paty JS dependencies (jQ..  AngularJS Core  AngularJS. HTML enhanced for web apps!  AngularJS Route  AngularJS. HTML enhanced for web apps!  Angula rJS  AngularJS. HTML enhanced for web apps!  AngularJS Animate  AngularJS. HTML enhanced for web apps!  AngularJS. HTML enhanced for web apps!  Angular UI -Router  Install  AngularUI Router is a routing framework for AngularJS, which  allows you to organize the pats of your interface Into a state.„  1 23

 

Create a new folder in the project and call it Common

 

Add a new Javascript file to the Common folder and name it commmon.services.js

Add the following code to it
(function () {
“use strict”;

angular
.module(“common.services”, [“ngResource”])
.constant(“appSettings”,
{
serverPath: “https://customersrest.azurewebsites.net
});
})();

Use the URL to your WebAPI application in the serverPath listed above

Add a new Javascript file to the Common folder and name it customerResource.js

Add the following code to it

(function () {
“use strict”;

angular
.module(“common.services”)
.factory(“customerResource”,
[“$resource”,
“appsettings”,
customerResource])

function customerResource($resource, appSettings) {
return $resource(appSettings.serverPath + “/api/customers/:id”);
}

}());

Create a new folder called app

Create a Javascript file and name it app.js

(function () {
“use strict”;

var app = angular.module(“customerManagement”, [“common.services”]);

}());

Create a folder called customers in the app folder

Create a Javascript file called productListCtrl.js in the customers folder

Create an HTML file called productsListView.html in the customers folder

To be continued…

One thought on “Create a Provider Hosted SharePoint Add In

Leave a Reply

Your email address will not be published. Required fields are marked *