Exploring SharePoint with F#

33
Exploring SharePoint with F# Talbott Crowell (MVP) @Talbott SharePoint Saturday New York City July 30, 2011

description

 

Transcript of Exploring SharePoint with F#

Page 1: Exploring SharePoint with F#

Exploring SharePoint with F#

Talbott Crowell (MVP)@Talbott

SharePoint Saturday New York CityJuly 30, 2011

Page 2: Exploring SharePoint with F#

• 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#

Page 3: Exploring SharePoint with 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?

Page 4: Exploring SharePoint with F#

• 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

Page 5: Exploring SharePoint with F#

• 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#

Page 6: Exploring SharePoint with 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#

Page 7: Exploring SharePoint with 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#

Page 8: Exploring SharePoint with 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++

Page 9: Exploring SharePoint with F#

The Power Wall: CPU Clock Speed

From Katherine Yelick’s “Multicore: Fallout of a Hardware Revolution”

Multicore->

Manycore->

Single core->

Page 10: Exploring SharePoint with F#

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

Page 11: Exploring SharePoint with F#

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

Page 12: Exploring SharePoint with F#

• 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

Page 13: Exploring SharePoint with F#

• 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

Page 14: Exploring SharePoint with F#

• Function

• Anonymous functions

F# Functions

let sqr x = x * xsqr 5

(fun x -> x * x) 5

Page 15: Exploring SharePoint with F#

• The |> Combinator “Pipe Forward”

• Example

F# Combinators

x |> f is the same as f x

let sqr x = x * x sqr 5 5 |> sqr

Page 16: Exploring SharePoint with F#

• 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

Page 17: Exploring SharePoint with F#

• Similar to operator overloading

• Example

Symbolic Functions

let (operatorName) left right = <function>

open System.Text.RegularExpressions

let (===) str regex = Regex.Match(str, regex).Success

Page 19: Exploring SharePoint with F#
Page 20: Exploring SharePoint with F#

Adding References

Page 21: Exploring SharePoint with F#

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

Page 22: Exploring SharePoint with F#

• 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

Page 23: Exploring SharePoint with F#

• Create SharePoint Client Context

• Pass Credentials

Client Context and Credentials

let ctx = new ClientContext("http://fsug.org")

ctx.Credentials <- new NetworkCredential(

user, password, domain)

Page 24: Exploring SharePoint with F#

• 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

Page 25: Exploring SharePoint with F#

• 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

Page 26: Exploring SharePoint with F#

• 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

Page 27: Exploring SharePoint with F#

• 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()

Page 28: Exploring SharePoint with F#

• 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

Page 29: Exploring SharePoint with F#

Demo

Page 30: Exploring SharePoint with F#

Future of F#

• Solution for the Data Deluge• Type Providers• Video by Keith Battocchi

Page 31: Exploring SharePoint with F#

Questions?

Page 32: Exploring SharePoint with F#

Thank You

• References:– http://TryFsharp.org• Play with F# on browser• Tutorial• Load and save files

– http://fsharp.net• More information

Page 33: Exploring SharePoint with F#

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