0

I setup new Sharepoint list, customized the form using Power App and able to view it and add new data. Now trying to setup a connection to this list to extract items that were added and unable to see the list. Tried using Visual Basic code - libraries - Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll - got error 'The remote server returned an error: (400) Bad request'. Please note that I used similar script to connect to a different Sharepoint list (different organization though) and it worked just fine. That tells me that some settings might be blocking me from accessing the list? The user account I'm using to connect - has Sharepoint owner full rights - so should be able to connect and see all lists. Tried to connect using Powershell code, using actual GUID for the list - got error: Exception calling "ExecuteQuery" with "0" argument(s): "List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user." List does exist and I can easily pull it up by URL.

The Powershell code:

    #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://xxx.sharepoint.com"
    $ListGUID = [System.Guid]("38xxxxxx-xxxx-xxxx-xxxx-aef6bcxxxxx")
     
    #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 List by id
    $List=$Ctx.Web.Lists.GetById($ListGUID)
    $Ctx.load($List)
    $Ctx.ExecuteQuery()
     
    Write-host "Total Number of List Items:"$List.ItemCount

Thank you!

Alla
  • 43
  • 1
  • 9
  • You are trying to create a GUID **object** where you already have a guid value as string. Just try `$List=$Ctx.Web.Lists.GetById("38xxxxxx-xxxx-xxxx-xxxx-aef6bcxxxxx")` – Theo Feb 10 '23 at 16:30
  • Thank you Theo. Tried to call it the way you suggested - same error that List doesn't exist. This sharepoint site is pretty basic - no customization, just out of the box, as it came with Office 365. Maybe we are missing some settings to expose the List? I do have correct permissions set for the list itself. Thanks! – Alla Feb 10 '23 at 18:13
  • Please check whether the account has permission to the list. Because if the list has unique permission, but the current account has no permission, even the site owner can only view, but not edit the list. – Amy Jiang_MSFT Feb 14 '23 at 10:01
  • I had mentioned that account has full rights to the whole site and to the list. In the code I'm not editing the list - as you can see, I can't even view, get count etc. – Alla Feb 14 '23 at 14:22

2 Answers2

0

The error you're getting:

Exception calling "ExecuteQuery" with "0" argument(s): "List does not exist.

indicates that the GUID value is probably incorrect for the list you are targeting. Try getting the list's root folder by its URL and then using the GUID from that to get the list itself like this:

$listRelativeUrl = "/sites/yoursite/Documents"
$web = $context.Web;
$folder = $web.GetFolderByServerRelativeUrl($listRelativeURL);
$context.Load($folder.Properties);
$context.ExecuteQuery();
$listId = [System.guid]::New($folder.Properties["vti_listname"].ToString());
$list = $Web.Lists.GetById($listId);
$context.Load($list);
$context.ExecuteQuery();
0

I resolved the issue, by loading web first, and pulling list by name instead. Added code:

$web = $Ctx.Web;
$Ctx.load($web)
$Ctx.ExecuteQuery()
$list = $Ctx.Web.Lists.GetByTitle("ListName")

Thank you!

Alla
  • 43
  • 1
  • 9