0

I'm new to F# and trying to recreate some simple apps that I've built in the past with C# or Powershell. I'm trying to get multiple properties for a web. In F#, I can get a single property, but I'm not sure the syntax that would work for getting multiple properties at once.

As an example:

// F# Example
context.Load(web |> fun w->w.Lists)
context.Load(web |> fun w->w.AssociatedOwnerGroup)
context.ExecuteQuery()

Is there a way in F# to combine that into a single line? In C# I'd call it like so:

// C# Example
context.Load(web, w => w.Lists, w => w.AssociatedOwnerGroup);
context.ExecuteQuery();
  • What is the signature of the `context.Load` function? – Brian Berns Mar 17 '21 at 21:54
  • The signature is: public void Load( T clientObject, params Expression>[] retrievals ) where T : ClientObject – Andrew Kennel Mar 17 '21 at 22:18
  • There's a similar question here, but that also requires breaking what is a single query in C# into multiple lines: https://stackoverflow.com/questions/38198613/is-it-possible-to-use-linq-from-f-and-how – Andrew Kennel Mar 17 '21 at 22:21

1 Answers1

0

It looks like the function you're calling is ClientRuntimeContext.Load<T>, which has the following C# signature:

public void Load<T>(
    T clientObject,
    params Expression<Func<T, Object>>[] retrievals
)
where T : ClientObject

There are two issues here that make this function difficult to call from F#: the params array, and the automatic conversion to LINQ expressions. F# supports these separately, but it looks like the automatic conversion doesn't work when you have multiple parameters. Instead, I think you have to do it manually, something like this:

// automatically quotes a function
type Expr = 
    static member Quote<'t>(f : Expression<Func<'t, obj>>) = f

// quote the functions
let funcs =
    [|
        fun (w : SPWeb) -> w.Lists
        fun (w : SPWeb) -> w.AssociatedOwnerGroup
    |] |> Array.map Expr.Quote

// call the Load function
context.Load(web, funcs)

Sorry, I know that's ugly. There may be a better way to do it, but I don't know what it is.

Brian Berns
  • 15,499
  • 2
  • 30
  • 40