Create an item in a Sharepoint list from a Powershell script

%3CLINGO-SUB%20id%3D%22lingo-sub-3226975%22%20slang%3D%22en-US%22%3ECreate%20an%20item%20in%20a%20Sharepoint%20list%20from%20a%20Powershell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3226975%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3CBR%20%2F%3EI%20have%20a%20Sharepoint%20listing%20called%20%22Membres%22%20on%20one%20of%20my%20sites.%3CBR%20%2F%3EI%20would%20like%20to%20create%20a%20new%20item%20in%20this%20list%20from%20a%20PowerShell%20script%20but%20I%20can't.%20This%20tells%20me%20an%20error.%3CBR%20%2F%3EHere%20is%20the%20script%20I%20am%20using%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23Load%20SharePoint%20CSOM%20Assemblies%0AAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%0AAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%0A%20%0A%23Set%20Config%20Parameters%0A%24SiteURL%3D%22https%3A%2F%2Fcgigodev.sharepoint.com%2Fsites%2FTeletravailCGIGO%22%0A%24ListName%3D%22Membres%22%0A%20%0ATry%20%7B%20%0A%20%20%20%20%23Get%20Credentials%20to%20connect%0A%20%20%20%20%24Cred%3D%20Get-Credential%0A%20%20%0A%20%20%20%20%23Setup%20the%20context%0A%20%20%20%20%24Ctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteURL)%0A%20%20%20%20%24Ctx.Credentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24Cred.Username%2C%20%24Cred.Password)%0A%20%20%0A%20%20%20%20%23Get%20the%20List%0A%20%20%20%20%24List%3D%24Ctx.Web.Lists.GetByTitle(%24ListName)%0A%20%20%0A%20%20%20%20%23sharepoint%20online%20powershell%20add%20list%20item%0A%20%20%20%20%24ListItemInfo%20%3D%20New-Object%20Microsoft.SharePoint.Client.ListItemCreationInformation%20%20%0A%20%20%20%20%24ListItem%20%3D%20%24List.AddItem(%24ListItemInfo)%0A%20%20%20%20%20%0A%20%20%20%20%23Set%20Column%20Values%0A%20%20%20%20%24ListItem%5B%22Title%22%5D%20%3D%20%22A%22%0A%20%20%20%20%24ListItem%5B%22Manager%22%5D%20%3D%20%22A%22%0A%20%20%20%20%24ListItem%5B%22Lundi%22%5D%20%3D%20%22T%C3%A9l%C3%A9travail%22%0A%20%20%20%20%24ListItem%5B%22Mardi%22%5D%20%3D%20%22T%C3%A9l%C3%A9travail%22%0A%20%20%20%20%24ListItem%5B%22Mercredi%22%5D%20%3D%20%22T%C3%A9l%C3%A9travail%22%0A%20%20%20%20%24ListItem%5B%22Jeudi%22%5D%20%3D%20%22T%C3%A9l%C3%A9travail%22%0A%20%20%20%20%24ListItem%5B%22Vendredi%22%5D%20%3D%20%22T%C3%A9l%C3%A9travail%22%0A%20%0A%20%0A%20%20%20%20%23Apply%20changes%20to%20list%0A%20%20%20%20%24ListItem.Update()%0A%20%20%20%20%24Ctx.ExecuteQuery()%0A%20%20%0A%20%20%20%20Write-host%20-f%20Green%20%22New%20Item%20has%20been%20added%20to%20the%20List!%22%0A%7D%0ACatch%20%7B%0A%20%20%20%20write-host%20-f%20Red%20%22Error%3A%22%20%24_.Exception.Message%0A%7D%20%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20here%20is%20the%20error%20it%20gives%20me%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Eapplet%20de%20commande%20Get-Credential%20%C3%A0%20la%20position%201%20du%20pipeline%20de%20la%20commande%0AFournissez%20des%20valeurs%20pour%20les%20param%C3%A8tres%20suivants%20%3A%0AError%3A%20Exception%20lors%20de%20l'appel%20de%20%C2%AB%20ExecuteQuery%20%C2%BB%20avec%20%C2%AB%200%20%C2%BB%20argument(s)%20%3A%20%C2%AB%20Des%20donn%C3%A9es%20non%20valid%0Aes%20ont%20%C3%A9t%C3%A9%20utilis%C3%A9es%20pour%20mettre%20%C3%A0%20jour%20l'%C3%A9l%C3%A9ment%20de%20liste.%20Le%20champ%20que%20vous%20essayez%20de%20mettre%20%C3%A0%20jou%0Ar%20est%20peut-%C3%AAtre%20en%20lecture%20seule.%20%C2%BB%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECould%20you%20help%20me%20please%20%3F%20%3A(%3C%2Fimg%3E%20Thank%20you%20in%20advance%20!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3226975%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Contributor

Hello,
I have a Sharepoint listing called "Membres" on one of my sites.
I would like to create a new item in this list from a PowerShell script but I can't. This tells me an error.
Here is the script I am using:

 

#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"
 
#Set Config Parameters
$SiteURL="https://cgigodev.sharepoint.com/sites/TeletravailCGIGO"
$ListName="Membres"
 
Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
  
    #sharepoint online powershell add list item
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation  
    $ListItem = $List.AddItem($ListItemInfo)
     
    #Set Column Values
    $ListItem["Title"] = "A"
    $ListItem["Manager"] = "A"
    $ListItem["Lundi"] = "Télétravail"
    $ListItem["Mardi"] = "Télétravail"
    $ListItem["Mercredi"] = "Télétravail"
    $ListItem["Jeudi"] = "Télétravail"
    $ListItem["Vendredi"] = "Télétravail"
 
 
    #Apply changes to list
    $ListItem.Update()
    $Ctx.ExecuteQuery()
  
    Write-host -f Green "New Item has been added to the List!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}  

 

 

And here is the error it gives me:

 

applet de commande Get-Credential à la position 1 du pipeline de la commande
Fournissez des valeurs pour les paramètres suivants :
Error: Exception lors de l'appel de « ExecuteQuery » avec « 0 » argument(s) : « Des données non valid
es ont été utilisées pour mettre à jour l'élément de liste. Le champ que vous essayez de mettre à jou
r est peut-être en lecture seule. »

 

Could you help me please ? :( Thank you in advance !

2 Replies
You posted your question twice? https://techcommunity.microsoft.com/t5/windows-powershell/create-an-item-in-a-sharepoint-list-from-a... is the other one ...

Could it be, I used Google Translate for the error message, that the list is Read-Only / Does your account have enough permissions?
Did you manage to fix your issue?