Tuesday, February 26, 2008
« PowerShell-SharePoint: Three ways to del... | Main | Silverlight 2 Beta 1 has landed »

Its pretty easy to add list items to SharePoint with Powershell so here's a quick sample

 

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
 
# Add an new item to the announcements list
 
$site    =     new-object Microsoft.SharePoint.SPSite("http://teams.contoso.com")                                                        
$web     =    $site.rootweb                                                                                                
 
$list    =    $web.Lists["Announcements"]                                                                              
 
$newitem=    $list.items.Add()                                                                                                  
                                                                                                  
$newitem["Title"]=    "New CFO Appointed";                                                                                          
$newitem["Body"]=    "Our new CFO is Harry Varden, money still not recovered from previous appointee, legal action is ongoing";                                                                                          
$newitem["Expires"]=     [DateTime]::Now.AddDays(5)
 
$newitem.update() 
 
$web.Dispose()
$site.Dispose()                                                                                                      

This could easily be turned into a function

Here I've referenced the field names by text but I could have used SharePoint's built-in list of field Id's using the SPBuiltInFieldId class.

In that case the fields are referenced by using the syntax [Microsoft.SharePoint.SPBuiltInFieldId]::Title

To get a list of fields in the class you can use this PowerShell command

[Microsoft.SharePoint.SPBuiltInFieldId] | get-member -static | select name | more

get-member is a handy class that reflects over a given object and returns the list of properties, fields and methods.

Tuesday, February 26, 2008 11:44:06 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
Tuesday, February 26, 2008 9:37:39 PM (GMT Standard Time, UTC+00:00)
Speaking of lists, here's what I'm trying to do in Powershell, but can't seem to figure out:

I wanna create a choice field in a list, set it as a radio list and add some choices.
I've got as far as creating the field:

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

$siteUrl = "http://myserver:11112"
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
$spweb=$spsite.openWeb()


$listName = "mylist"
$spList = $spWeb.Lists[$listName]

$spList.Fields.Add("MyChoice","Choice",0)

I can then manually add a choice and get it to write out in Powershell:

write-host $spList.Fields["MyChoice"].Choices[0]

but I can't figure out the syntax to add a choice or to make the field a radio-list instead of a drop-down list

I thought it'd be as easy as:

write-host $spList.Fields["MyChoice"].Choices.Add ("MySecondChoice)

but it doesn't like that.

Any ideas?

TIA
Comments are closed.