Add items to fields with powershell in sharepoint 2010

Add items to fields with powershell in sharepoint 2010
3 votes, 5.00 avg. rating (97% score)

Introduction

Moving further from “Creating lists”,  “Creating fields to List Part1” and “Creating fields to List Part2” with powershell; this post will cover how to implement SharePoint 2010 list utilities with powershell. We will learn how we can Add list items to various fields (columns)


Adding Items to List

Adding item to SharePoint list “EmpInfo”. Note, field name is case sensitive.

Adding item to Text, Choice, Number, Date, Checkbox, Hyperlink columns

function Get-SPList($webUrl, $lstUrl)
{
    $webObj = Get-SPWeb -identity $webUrl
    $lstObj = $webObj.GetList($lstUrl);
    return $lstObj
}
$spEmpList = Get-SPList -webUrl "http://mysitecollecton/MySite" -lstUrl "http://mysitecollecton/MySite/Lists/EmpInfo"
$spEmpListItem = $spEmpList.AddItem()
$spEmpListItem.Item("Title") = "Test"
$spEmpListItem.Item("ChoiceColName") = "MyChoice2"
$spEmpListItem.Item("NumColName") = 100
$spEmpListItem.Item("DataTimeColName") = Get-Date
$spEmpListItem.Item("chkBoxColName") = $true
$spEmpListItem["HyperlinkColName"] = "http://www.adicodes.com, SharepointCodes"
$spEmpListItem.Update()

In the above code,
Line 3 refers item for ‘Title’ column
Line 4 refers item for ‘Choice’ column. The value ‘MyChoice2’ is one of the existing choice values of the ‘Choice’ column
Line 5 refers item for ‘Number’ column
Line 6 refers item for ‘Date and Time’ column. We have to provide date time value
Line 7 refers item for ‘Yes/No’ column. ‘$true’ value will set ‘Yes’ and ‘$false’ value will set ‘No’
Line 8 refers item for ‘Hyperlink or Picture’ column. Both link value and description for the link should be provided with comma(,) separated

Adding item to Lookup column

function Get-SPList($webUrl, $lstUrl)
{
    $webObj = Get-SPWeb -identity $webUrl
    $lstObj = $webObj.GetList($lstUrl);
    return $lstObj
}
$spEmpList = Get-SPList -webUrl "http://mysitecollecton/MySite" -lstUrl "http://mysitecollecton/MySite/Lists/EmpInfo"

$spEmpListItem = $spEmpList.AddItem()
$spAnnouncementsList = Get-SPList -url "http://mysitecollecton/MySite/Lists/Announcements"
$spLookupListItem = $spAnnouncementsList.GetItemById(1)
$strLookupListItem = [string]$spLookupListItem.ID + ";#" + $spLookupListItem.Title # casting to string is mandatory here
$spEmpListItem["LookupColName"] = $strLookupListItem
$spEmpListItem.Update()

In the above code,
Line 3 refers lookup list. We will get the value from this list and assign it in our lookup column ‘LookupColName’.
Line 4 get the item with Id ‘1’ from lookup list. We have build the lookupvalue string with ID and title as in Line 5,then
assign that lookupvalue string in the lookup column as in Line 6.

or

In SharePoint 2010, you can also use a list item object as value to a lookup field directly, instead of creating a string in a specific format.
Following is the snippet

function Get-SPList($webUrl, $lstUrl)
{
    $webObj = Get-SPWeb -identity $webUrl
    $lstObj = $webObj.GetList($lstUrl);
    return $lstObj
}
$spEmpList = Get-SPList -webUrl "http://mysitecollecton/MySite" -lstUrl "http://mysitecollecton/MySite/Lists/EmpInfo"

$spEmpListItem = $spEmpList.AddItem()
$spEmpListItem["LookupColName"] = (Get-SPList -url $listUrl).GetItemById(1)
$spEmpListItem.Update()

Adding item to Person or Group Column

function Get-SPList($webUrl, $lstUrl)
{
    $webObj = Get-SPWeb -identity $webUrl
    $lstObj = $webObj.GetList($lstUrl);
    return $lstObj
}
$spEmpList = Get-SPList -webUrl "http://mysitecollecton/MySite" -lstUrl "http://mysitecollecton/MySite/Lists/EmpInfo"

$spEmpListItem = $spEmpList.AddItem()
$spUser = Get-SPUser -Web "http://mysitecollection/MySite" -Identity "Local\adicodesadmin"
$spEmpListItem["PersonorGroupColumn"] = $spUser
$spEmpListItem.Update()

For Person or Group column, we have to assign SPUser object as specified in Line 4.

Conclusion

We will further see how we can update and delete items in the sharepoint list with powershell

April 12, 2012 В· Adi В· 4 Comments
Tags: , , , , , , , , ,  В· Posted in: Packaging and Deployment, Powershell, Sharepoint 2010

4 Responses

  1. Syed - July 3, 2012

    Very Good and Helpful post………. I was stuck with the People/Group Field update, this post helped a lot.

  2. Pamita - March 1, 2015

    Very helpful tips.
    Thanks for sharing SP tips.

  3. Pierre - July 7, 2015

    Interesting post, but how do you set a multichoice value? I tried “firstChoice;thirdChoice”, but it will not be “really” there until I open and close interactively the choices in SharePoint …

  4. Alex - August 18, 2015

    Thank you !

Leave a Reply

What is 13 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)