exploring sharepoint with f#
DESCRIPTION
TRANSCRIPT
Exploring SharePoint with F#
Talbott Crowell (MVP)@Talbott
SharePoint Saturday New York CityJuly 30, 2011
• Learn something new (exercise for the brain)• Trend in functional programming– Java -> Clojure, Scala– Erlang– LINQ added to VB and C#
• Microsoft’s official (and only) functional programming language
Why F#
• Functional programming has been around a long time– Not new– Long history
• Functional programming is safe– A concern as we head toward manycore and cloud
computing
Why another language?
• 1930’s: lambda calculus (roots)• 1956: IPL (Information Processing Language) “the first
functional language• 1958: LISP “a functional flavored language”• 1962: APL (A Programming Language)• 1973: ML (Meta Language)• 1983: SML (Standard ML)• 1987: Caml (Categorical Abstract Machine Language ) and
Haskell• 1996: OCaml (Objective Caml)
Functional programming has been around a long time
• Functional language developed by Microsoft Research– By Don Syme and his team, who productized Generics– Based on OCaml (influenced by C# and Haskell)
• History– 2002: F# language design started– 2005 January: F# 1.0.1 releases to public
• Not a product. Integration with VS2003• Works in .NET 1.0 through .NET 2.0 beta, Mono
– 2005 November: F# 1.1.5 with VS 2005 RTM support– 2009 October: VS2010 Beta 2, CTP for VS2008 & Non-Windows
users– 2010: F# is “productized” and baked into VS 2010
What is F#
• Interactive Scripting – Uses REPL (Read Evaluate Print Loop)– Good for prototyping
• Succinct = Less code• Type Inference– Strongly typed, strict– Automatic generalization (generics for free)– Few type annotations
• 1st class functions (currying, lazy evaluations)• Pattern matching
Key Characteristics of F#
• Functional first– Mutable keyword– Functions are first class values
• Blend of functional and imperative• Object oriented capabilities• Built on .NET Framework– Practical– Leverage existing code
What I like about F#
What is manycore?
• Lots of processors on one chip– Tens or hundreds
• Intel – Future (Task parallelism)• NVIDIA GPU – Today (Data parallelism)– 500+ cores on chip– Graphics, gaming, 3D rendering– Use CUDA for financial or research computing– Program in C or C++
The Power Wall: CPU Clock Speed
From Katherine Yelick’s “Multicore: Fallout of a Hardware Revolution”
Multicore->
Manycore->
Single core->
Road to manycore• 1970 – 2005– Single core on the “desktop” and laptop
• 2006 – 2011– Single core on the smartphone/tablet– Multi core on the “desktop”– Multi core in the cloud
• 2012 – 2020– Multi core on the smartphone/tablet
• 2021– Manycore probably will be common on many devices and
computers
Multicore for Smartphones/Tablets• Android 2.2 already supports multicore
– NVIDIA dual core test with one core shut off– 1.5 to 1.6x faster with two cores– http://bit.ly/nvidiadualcore
• NVIDIA quad core on its way– Smartphones by holiday season 2011– Faster than 2 GHz notebook Core 2 Duo (T7200)– http://bit.ly/eWMOsu
• Qualcomm quad core SnapDragon– Devices expected in 2013
• Intel announcing entry into Smartphone market
• Declarative programming style– Easier to introduce parallelism into existing code
• Immutability by default– Can’t introduce race conditions– Easier to write lock-free code
Functional Programming
• Type inference
• Expressions
F# Basics
let x = 5let y = 5.0 let files = Directory.GetFiles(@"C:\images\original")
let x = 5 * 5let y = 5.0 / 3.0let width = image.Width / 8
• Function
• Anonymous functions
F# Functions
let sqr x = x * xsqr 5
(fun x -> x * x) 5
• The |> Combinator “Pipe Forward”
• Example
F# Combinators
x |> f is the same as f x
let sqr x = x * x sqr 5 5 |> sqr
• The <| Combinator “Pipe Backward”
• Example
F# Combinators
f <| x is the same as f x
let sqr x = x * x sqr 5 sqr <| 3 + 2 not same as sqr 3 + 2
• Similar to operator overloading
• Example
Symbolic Functions
let (operatorName) left right = <function>
open System.Text.RegularExpressions
let (===) str regex = Regex.Match(str, regex).Success
Client Object ModelClient Server
ClientContext SPContext
Site SPSite
Web SPWeb
List SPList
ListItem SPListItem
Field SPField
Adding References
For more info on Client Object Model
• See “Using the SharePoint Foundation 2010 Managed Client Object Model”
• http://msdn.microsoft.com/en-us/library/ee857094(office.14).aspx
• Load References FSX Script
• Open namespace
Client Object Model Assemblies
#r @"..\ReferenceAssemblies\Microsoft.SharePoint.Client.dll" #r @"..\ReferenceAssemblies\Microsoft.SharePoint.Client.Runtime.dll"
open Microsoft.SharePoint.Client
• Create SharePoint Client Context
• Pass Credentials
Client Context and Credentials
let ctx = new ClientContext("http://fsug.org")
ctx.Credentials <- new NetworkCredential(
user, password, domain)
• Helper function to Load and Query
• Retrieve Site Collection
Load and Query Context
let load(ctx:ClientContext)(item:'a) = ctx.Load(item) ctx.ExecuteQuery()
let site = ctx.Site load ctx site
• Site Collection (Site) and Site (Web)
Site Collection and Site
let site = ctx.Site load ctx site
let web = site.RootWeb load ctx web
let title = web.Title
• Use for loop to iterate through lists
Iterate through Lists
load ctx web.Lists
for list in web.Lists do print <| "List Title: " + list.Title
• Query for list items using CAML• Use for loop to iterate through list items
Iterate through List Items
let fsugMeetings = web.Lists.GetByTitle("FSUG Meetings")let query = new CamlQuery()query.ViewXml <- "<View><Query><OrderBy>...
let listItems = fsugMeetings.GetItems(query)
ctx.Load(fsugMeetings);ctx.Load(listItems);ctx.ExecuteQuery();
for meeting in listItems do print <| "Meeting: " + meeting.["Title"].ToString()
• Use for loop to iterate through lists
Iterate through Lists
load ctx web.Lists
for list in web.Lists do print <| "List Title: " + list.Title
Demo
Future of F#
• Solution for the Data Deluge• Type Providers• Video by Keith Battocchi
Questions?
Thank You
• References:– http://TryFsharp.org• Play with F# on browser• Tutorial• Load and save files
– http://fsharp.net• More information
My Info
• Talbott Crowell– F# MVP– http://fsug.org • New England F# User Group
– http://twitter.com/talbott @talbott– @BASPUG• Boston Area SharePoint User Group
– http://ThirdM.com • Third Millennium, Inc. Chief Architect