2

I'm pretty new to powershell & sharepoint and i'm having hard time trying to create a function.

I'm trying to make a generic function to add new items into a SPList but i can't pass the SPList to the function.

Here is my function prototype:

function Add-IntoList([Microsoft.SharePoint.SPList] $List,[hashtable] $Columns)
{
... some code
}

And here is my call to the function:

$web = Get-Web("http://some_url/sandbox1") #It returns the Get-SPWeb
$test = @{"Title" = "Olympia"; "Body" = "Salem"}
Add-IntoList($web.Lists["Announcements"], $test)

And it doesn't work, i can't see why. Here is the error powershell tells me:

Add-IntoList : Cannot process argument transformation on parameter 'List'. Cannnot convert the "System.Object[]" value of type "System.Object[]" to type "Microsoft.Sharepoint.SPList".

What am i doing wrong ?

Thanks in advance, Nicolas

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Nicolas G.
  • 33
  • 1
  • 4

1 Answers1

3

When you call the function, instead of calling it like:

Add-IntoList($web.Lists["Announcements"], $test)

call it like:

Add-IntoList $web.Lists["Announcements"] $test
Greg
  • 18,111
  • 5
  • 46
  • 68
  • Just wanted to explicitly call out that as Ben wrote, you have to remove both the parenthesis AND the commas in the function call. This thread has additional details: http://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell – Eugene Rosenfeld Jan 01 '15 at 05:35