Friday, August 1, 2014

SharePoint Online: Add New List Item using Client Side Object Model (CSOM) PowerShell

When working with SharePoint Online, had a requirement to add new list items to a list from PowerShell script. Here is my example:

SharePoint online: PowerShell to add list item
Here is the SharePoint Online PowerShell to create list item

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables for Processing
$WebUrl = ""
$ListName ="Tasks" 
$AdminName =""
$AdminPassword ="Password goes here" 

#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) 
$Context.Credentials = $Credentials

#Get the Web and List
$Web = $Context.Web
$List = $web.get_lists().getByTitle($ListName)
#Add new List Item
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
$listItem = $list.addItem($itemCreateInfo) 

#Set Fields - Internal Name
$listItem.set_item('Title', 'PMO Dashboard Enhancements')  #Task title

#Set Date Field
$listItem.set_item('StartDate', '01/07/2015')
$listItem.set_item('DueDate', '01/15/2015')

#Set People Picker Field value
$AssignedToUser = $web.EnsureUser("")
$listItem.set_item('AssignedTo', $AssignedToUser)



write-host "New Item has been Added!"  
Result PowerShell for sharepoint online to add item to list:
sharepoint online powershell add item to list
SharePoint Online PowerShell to Create List Item
Lets add some error handling code and get the credentials at run time in the script.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Parameters

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)

    #Create List Items with Random Data
    For($i=1; $i -le $ItemsToCreate; $i++)
        #Create list item
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $ListItem = $List.AddItem($ListItemInfo)
        $ListItem["Title"] = $I

        Write-host "New Item $I Added to the List!" -ForegroundColor Green  
Catch {
    write-host -f Red "Error Adding Items to List!" $_.Exception.Message

BTW, You need SharePoint Server 2013 Client Components SDK installed in your machine to use Client side assemblies. Download it from:

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...