kwizcom banner advertisement

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 = "https://crescent.sharepoint.com/Sites/Sales"
$ListName ="Tasks" 
$AdminName ="Salaudeen@crescent.OnMicrosoft.com"
$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("Salaudeen@Salaudeen.OnMicrosoft.com")
$listItem.set_item('AssignedTo', $AssignedToUser)

$listItem.update(); 

#Execute
$Context.Load($listItem) 
$Context.ExecuteQuery()  

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
$SiteURL="https://crescent.sharepoint.com"
$ListName="Random"
$ItemsToCreate="1000"

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)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #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
        $ListItem.Update()
        $Ctx.ExecuteQuery()

        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: http://www.microsoft.com/en-us/download/details.aspx?id=35585
SharePoint Online: Add New List Item using Client Side Object Model (CSOM) PowerShell SharePoint Online: Add New List Item using Client Side Object Model (CSOM) PowerShell Reviewed by Salaudeen Rajack on August 01, 2014 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.