Discussion of Azure web apps, App Insights, "Azure Functions in the real world", ARM templates, queues, BLOB storage and more. Includes a video demo of AAD-secured Azure Function called from a SharePoint Framework (SPFx) web part with SPO cookie auth.
Chris O'Brien - Best bits of Azure for Office 365/SharePoint developers
1. Azure – the best bits (for
Office 365/SharePoint devs)
Chris O’Brien (MVP)
Independent/Content and Code, UK
Add
Speaker
Photo here
2. Top Office 365 + Azure scenarios
What? How?
Do something on a schedule Put code in Azure Web Jobs/Functions
Build apps (Office 365 app/SP
provider-hosted add-in)
Deploy app files to an Azure app
SharePoint site provisioning Deploy PnP Partner Pack to Azure
Run code on a button click Use Azure Functions + JavaScript
Store data not suited to SP lists Use Azure SQL Database
Store files for my app Use Azure BLOB storage (and CDN if
appropriate)
Implement SharePoint web hooks Use Azure Queues and Functions
Implement authentication on a
custom web app
Implement Azure Active Directory (AAD) auth
Or the general case:
HOST MY REMOTE
SHAREPOINT CODE!
3. Some Azure features
Compute
•Virtual machines
•App services/web
apps
•Web Jobs
•Functions
Storage
•BLOB storage
•Table storage
•Queues
•File service
Data
•Azure SQL Database
•Redis cache
•Azure Search
•StorSimple
•DocumentDB
Messaging
•Service Bus
•Event Hubs
•Queues/Topics/Relays
Media
• CDN
• Encoding
• Streaming
Mobile services
• Push
notifications
• Mobile
Engagement
Integration
• Logic Apps
• API
management
• Data Factory
• Data Catalog
Security and
Identity
• Azure Active
Directory
• AAD B2C
• Azure RMS
• Key Vault
• MFA
4. Azure is… Big
> 90%
Fortune 500 using MS cloud
>
Active websites
300k 1,000,000
715Azure AD users
More than
SQL databases in Azure
120kPER
MONTH12Orgs in Azure AD
M
>
>> M
3Requests per second
M
> 30Storage objects
TN
> 2Developers in VS Online
M
New Azure subscriptions
Performance Q2 2017
•93% revenue increase
YoY
•2x compute usage YoY
5.
6. Web Apps
A great hosting location
• Easy to manage, scale up/down
• Auto SSL/load-balancing/backup
Perfect for extending SharePoint
• Office 365 apps
• SharePoint Add-ins
• Standalone web apps
Create Azure
web app
Register app
in AAD/SP
Configure Deploy files
7. Deployment options
Drag and drop in browser (Kudu)
Publish from Visual Studio
WebDeploy
Source control integration (GitHub, Git, VS Online)
FTP
8. Azure Web Apps - deployment slots
Dev/test/prod “instances” of
your site
• Own URL
• Own App Settings/Connection
Strings
http://mysite.azurewebsites.net
http://mysite-dev.azurewebsites.net
http://mysite-test.azurewebsites.net
Allows you to test purely
in production Office 365
environment, BUT with
dev/test/prod code!
9. In Azure
portal:
PowerShell:
“Swapping” deployment slots
Slots can be swapped to
deploy updates
Actually a DNS update, not copy
of content
Process:
Publish updates to dev/test slots
Swap test/production when ready
Switch-AzureWebsiteSlot
–Name ‘COB website’
-Slot1 'Production'
-Slot2 <slotName>
10. More Azure coolness – “Testing in production”
Traffic Routing - send
some traffic to another
slot
Uses:
Testing new functionality on
small number of users
A/B testing
11.
12.
13. What is App Insights?
Azure-based monitoring/reporting of
your app
• Exceptions
• Events within your app
Basic page analytics
• (Not really suitable for site owners)
Free up to 20GB per month
14. Getting started
ASP.NET web app
• NuGet package:
Install-Package
Microsoft.ApplicationInsights
• https://www.nuget.org/
packages/Microsoft.
ApplicationInsights/
Modern web app
• npm package:
npm i applicationinsights
• https://www.npmjs.com/
package/
applicationinsights
16. Log custom events/metrics
How long does a (Graph?) API
call take *for the user*?
How often did a user click
button X?
What are the most popular
file types?
17.
18. Simple code hosting
Scenarios
• Button click (e.g. web part)
• Scheduled process
• Respond to event (e.g. new file in Azure)
Develop in any language
• C#, JavaScript, PowerShell etc.
Simpler than a Web Job or Web API!
19. Functions – pricing plans
App service plan Consumption plan
Runs on dedicated VMs Serverless
Pay for containing VM Pay for what you use (executions)
Great if running at high scale Great for intermittent/quicker jobs
Scale at VM level Scale up automatically
MORE EXPENSIVE CHEAPER
So, generally you want the consumption plan!
20. Functions vs. Web Jobs
Similarities
• Both can be scheduled or use trigger (queue/BLOB
etc.)
• Both support C#, JavaScript, PS
Differences
• Pricing - only Functions have pay-per-use option
• Flexibility - Functions can be triggered from HTTP
call/web hook, OneDrive, Github etc.
• Restrictions – max 10 min timeout on Function
Typically Functions > Web Jobs See http://cob-sp.com/2r1MZe5
21. Authentication options
Function auth
• A simple code passed to function
• Caller must know/store the code
• Auth to Office 365/SP handled separately
Azure AD auth
• Function cannot be called without auth token
• Requires adal.js/msal.js from JavaScript OR cookie/IFrame approach (currently)
• OpenID Connect may help in SPFx in future?
https://cob-pnp-
functions.azurewebsite
s.net//api/CreateModer
nPage?
code=
FniGsXQ43Nf1HYB0JEIRuR
rbLPaTTQnuithMnqtXoLQ5
4Hz6FY/j3g==
22. Options for developing Azure Functions (C#)
Use Azure portal
• Good for playing
around
• No source control
• Little coding support
Use VS Code
• Sync from source
control to Function
• Little coding support
for C# (more for
node.js)
Use Visual Studio 2017
• Full coding support (F5
debug, IntelliSense)
• Publish to Azure
• Requires VS2017 15.3+
with Azure
development workload
.csx files and #r references True C#
23.
24. Azure Functions in the real world
Secured by AAD auth
Identity of current user available if required
Use of NuGet packages
Uses App Insights for monitoring
Uses PnP Core
Callable from SPFx (with adal.js or cookie method)
29. SQL in an Office 365 world
Can now auth with AAD identity
• No need for separate SQL
auth/identity
• Best practice – set AAD Group, not
User to be admin
Code options
• Connect with certificate – app-only
auth
• Connect with user token (using
MSAL or ADAL) – user auth
30. Create a new DB/add item with EF
Entity Framework code: using (var db = new ListDbContext())
{
List list = new List();
list.ListId = Guid.NewGuid();
list.WebId = Guid.NewGuid();
list.SiteId = Guid.NewGuid();
ListItem item = new ListItem();
item.List = list;
item.ItemUniqueId = Guid.NewGuid();
item.Id = 1;
db.Lists.Add(list);
db.SaveChanges();
db.ListItems.Add(item);
db.SaveChanges();
}
31.
32. ARM templates
Easily deploy without button clicks!
Defined in JSON, deployed with
PowerShell (or C# etc.)
• Parameters extracted to separate file
Deploy entire app, and optionally resources (e.g.
code):
• Web app (inc. App Settings, SSL cert etc.)
• SQL Database
• Function app
• etc.
New-
AzureRmResource
GroupDeployment
–TemplateFile
xyz
33. Building an ARM template
• The scope is a Resource Groupod
reason to structure your resources in this way
• Export-AzureRmResourceGroup
• See
https://azure.microsoft.com/en-
us/blog/export-template
35. Advanced ARM scenarios
Auto-deploying files for web app/Function
• Zip file must exist in Azure BLOB storage
• Auto-deploying SSL certs
• Fetch bytes from filesystem, use in parameters object
36. ARM templates – other features
Define
dependencies, for
correct provisioning
sequence
Tags
Role-based security
37.
38. Queues are great for..
Anything that should be picked up by a longer running task
• SharePoint site provisioning
• SharePoint web hooks
• File processing (e.g. my image renditions demo)
QueueTrigger – the key
• Auto-runs your code (when new item added)
• Azure Function
• Azure Web Job
39. Decoupling code with a Queue
Itake thingsfrom queue
andprocessthem e.g.
- AzureFunction
- AzureWebJob
Iput thingson queue
QueueTriggerC#, REST etc.
40. What goes on a Queue?
Answer – a string (i.e.
anything)
{
“SiteUrl”: “/Project12345”,
“Title”: “Project 12345”,
“Template”: “ProjectSite”
“Owners”: {
“Primary”: “chris.obrien@foo.com”,
“Secondary”: “adam.smith@foo.com”
}
}
Create object
Serialize
Add to queue
41. Adding a queue item
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference(“SiteRequestQueue");
// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage(siteInfoObject);
queue.AddMessage(message);
Other ops:
- Peek message
- Dequeue
message
- Amend contents
of existing
message
42.
43. Storage Queues vs. Service Bus Queues
Be aware of the two options..
..but Storage Queues work well for most
Key differences:
Storage Queue Service Bus Queue
7 day max lifetime Unlimited lifetime
Full transaction log No transaction log
Order not guaranteed Order can be guaranteed
Potential duplicates Duplicate detection
Simpler More complex
See
http://cob-sp.com/
AzureQueues
44. And we didn’t even talk about…!
Azure Containers
(Docker)
API management Virtual machines (of course!)
Azure RMS Azure B2C Azure mobile apps (e.g. notification hub)
HDInsight Azure Data Lake Azure virtual networks
Azure media
services
Azure batch (HPC) Azure backup vault
45. Key take-aways
Web apps are cooler than you think!
Write your first Azure Function!
Perfect for timer jobs, perfect for web APIs (e.g. advanced web parts)
Try deploying the PnP Partner Pack as a good exercise
(manual approach)
Deployment slots
App Insights
Testing in production
46. Useful links
Azure Storage Explorer
• https://AzureStorageExplorer.codeplex.com
Azure Functions / SPFx series
• http://cob-sp.com/SPFx-AzureFunc-1
PnP Partner Pack
• https://github.com/SharePoint/PnP-Partner-Pack
47. Thank you!!
Any questions?
www.sharepointnutsandbolts.com
@ChrisO_Brien