SharePoint Tips

Wednesday, November 22, 2017

SharePoint Online : CSOM : PowerShell : Create List Item

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"

$siteURL = ""
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$ctx.credentials = $creds
try{
    $lists = $ctx.web.Lists
    $list = $lists.GetByTitle("TestList")
    $listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
    $listItem = $list.AddItem($listItemInfo)
    $listItem["Title"] = "c"
    $listItem.Update()   
    $ctx.load($list)   
    $ctx.executeQuery()
    Write-Host "Item Added with ID - " $listItem.Id   
}
catch{
    write-host "$($_.Exception.Message)" -foregroundcolor red
}  

No comments:

Post a Comment