Share via


How can I connect to SPOnline with pwoershell and add item to list. Connect-PnPOnline: The remote server returned an error: (400) Bad Request.

Question

Thursday, April 2, 2020 3:27 PM

Hello,

I am trying to connect using PNPOnline or using:

Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll

Here my script:

Install-Module SharePointPnPPowerShellOnline
$username = "[email protected]"
$password = (ConvertTo-SecureString  "P@ssw0rd" -AsPlainText)
$Credential = New-Object -TypeName pscredential -ArgumentList $username, $password

Connect-PnPOnline -Url "https://mysite.sharepoint.com/sites/subsite" -Credentials  $Credential     

Answer is  (400) Bad Request.

"Connect-PnPOnline: The remote server returned an error: (400) Bad Request."

Also I made this

/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

And try connect like this:

Connect-PnPOnline -Url "https://myssite.sharepoint.com/sites/subsite" -AppId "ae1we-b9wefb457" -AppSecret "4uveeem7duAxdwwqewqewqeeLoMsJp5axer8duu8o3fzJM="

If I try get Get-PnPList, revive error:

**Get-PnPList : The remote server returned an error: (403) Forbidden. **

If I try this:

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'
$SiteURL = "https://mysite.sharepoint.com/sites/subsite"
$ListName="Active Directory: User logon activity"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("[email protected]", $securePassword)
$Web = $Context.Web
$List = $web.get_lists().getByTitle($ListName)
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$listItem = $list.addItem($itemCreateInfo)
$listItem.set_item('Computer Name', 'computer')
$listItem.set_item('User Name', 'User')
$listItem.set_item('Date and Time', '2/05/2020')
$listItem.set_item('Action', 'Logon')
$listItem.update();
$Context.Load($listItem)
$Context.ExecuteQuery()

Answer is  (400) Bad Request.

Even if I using 

Connect-PnPOnline -Url "https://mysite.sharepoint.com/sites/subsitee" -SPOManagementShell     

 

Answer is  (400) Bad Request.

I am not using 2FA, in ADAzure 2FA i disabled.

How can I connect to SPOnline with pwoershell and add item to list :)

All replies (7)

Friday, April 3, 2020 1:55 AM ✅Answered

Hi Denis, 

If there is anything update, feel free to let us know.

Besides, SharePoint Online: Add New List Item using PowerShell for your reference:

https://www.sharepointdiary.com/2014/08/sharepoint-online-add-new-list-item-using-csom-powershell.html

Best Regards,

Lisa Chen 

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.


Saturday, April 4, 2020 11:30 AM ✅Answered

This is just fantastic!!!

URL mast ends with "/"

For example: 

Connect-PnPOnline -Url "https://abc.sharepoint.com/sites/someSite" -Credentials $Credential  - this is wrong, but no error will be displayed and you can add items, but they will not be added correct!

This is how it works:

Connect-PnPOnline -Url "https://abc.sharepoint.com/sites/someSite**/**" - it`s OK


Saturday, April 4, 2020 12:32 PM ✅Answered

1. Column mast not contain spaces. Or script will return error.

2. Column name is script is case sensitive. Or script will return error.

3. Title column mast be with "Title" name. Rename will return error.


Thursday, April 2, 2020 8:48 PM

I try this:

will  take up to 24 hours for the change to take effect.


Friday, April 3, 2020 10:30 AM

Hi Denis, 

If there is anything update, feel free to let us know.

Besides, SharePoint Online: Add New List Item using PowerShell for your reference:

https://www.sharepointdiary.com/2014/08/sharepoint-online-add-new-list-item-using-csom-powershell.html

Best Regards,

Lisa Chen 

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

unfortunately did not work, the result is the same

Here is the SharePoint Online PowerShell to create list item

Result:

Error: Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."

PnP PowerShell to Add List Item in SharePoint Online

Result:
"Connect-PnPOnline: The remote server returned an error: (400) Bad Request."


Friday, April 3, 2020 10:52 PM

You know, this is not funny.
The problem is only with Visual Studio code, this is not a problem.
But another magical problem arose.

In list I renamed column "Titel" with new name "ComputerName" + added column "UserName".

In sharepoint it looks like:

If I tried add data using powershell:

Add-PnPListItem -List "testlist"-Values @{"computername" = "computer";"username" = "username"}

I got error

Add-PnPListItem : Column 'ComputerName' does not exist. It may have been deleted by another user.  /sites/abc/Lists/TestList

**OK, try do this **

Get-PnPListItem -List "testlist"

Return answer like this:

 

This looks like a joke.

Add-PnPListItem -List "testlist"-Values @{"Title" = "computer"} - works fine. But at Sharepoint web page there is not "Title" column.

What does it`s mean?


Saturday, April 4, 2020 7:35 AM

Lisa,

I think the problem is resolved. This problem is happens only if I use Visual Studio Code. There is no problem with Powershell ISE.

Does it need to create new question for issues with different information between web list and powershell Get-PnPListItem.

In web view I see two columns with name like "columnA", "columnB", but  Get-PnPListItem returns "Title" column only. 

For example: I rename "Title" with new name  "columnA" and add column "columnB". If I try add item 

Add-PnPListItem -List $ListName -Values @{"columnA" = "someData"} - I will receive error

Add-PnPListItem : Column 'columnA' does not exist. It may have been deleted by another user.  /sites/LocalITo3652/Lists/TestList

Add-PnPListItem -List $ListName -Values @{"Title" = "someData"} - data will be added with no error. In this case I will see data in "columnA" in web page.