Hi,
We had a requirement as follows with powershell
-For creating List,adding field in that list,
-Adding,Updating and Deleting all the list items
Finally after googling i found and implemented the task.Code is as below
//Creating and adding field to the list using powershell
$MyAssignment = Start-SPAssignment
$MyWeb = Get-SPWeb http://aaaa -AssignmentColl
ection $MyAssignment
$MyWeb.Lists.Add("My List","This list created with powershe
ll",$MyWeb.ListTemplates["Custom List"])
//Adding Field into the list
$MyList = $MyWeb.Lists["My List"]
$MyList.Fields.Add("TextField","Text",$MyWeb.FieldTypeDefin
itionCollection["Text"])
TextField
$MyWeb.Dispose()
Stop-SPAssignment $MyAssignment
//Adding ListItem
$spAssignment = Start-SPAssignment
$mylist = (Get-SPWeb -identity http://aaa -As
signmentCollection $spAssignment).Lists["My List"]
$newItem = $mylist.Items.Add()
$newItem["Title"] = "Added by Powershell"
$newItem["TextField"] = "My PowerShell Magic"
$newItem.Update()
//Deleting all ListItems
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, V
ersion=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Po
rtal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Pu
blishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0
.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$SITEURL = "http://aa"
$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title
# Enter name of the List below
$oList = $web.Lists["My List"];
"List is :" + $oList.Title + " with item count " + $oList.I
temCount
$collListItems = $oList.Items;
$count = $collListItems.Count - 1
for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
"Deleting : " + $intIndex
$collListItems.Delete($intIndex);
}
No comments:
Post a Comment