Office365-rest-python-client: Can't Access Any Information About My SharePoint Lists, List Resource URL Looks Incorrect

Created on 28 Aug 2020  ·  5Comments  ·  Source: vgrem/Office365-REST-Python-Client

Here's what I'm using.

site_url = "https://mycompany.sharepoint.com/sites/mysite/"
clientid = "12345"
clientsecret = "67890"
clientcredentials = ClientCredential(clientid, clientsecret)
ctx = ClientContext(site_url).with_credentials(clientcredentials)
target_list = "ListIWant"
list_object = ctx.web.lists.get_by_title(target_list)
items = list_object.get_items()
ctx.load(items)
ctx.execute_query()

Everything runs without errors, but in the end my list_object doesn't have any items or fields... When I look at the resource_url in the list_object, it's not the correct URL. There are extra back slashes in front of and behind the list name. Removing the slashes and copying into the Edge address bar downloads an XML file with information about the list, but as-is, it won't work... so I'm assuming that's what's causing the error... but I don't how to fix it.

I feel like I'm so close... can anyone help?
example

question

All 5 comments

You're almost there.
Your items object is of type ListItemCollection.
If you try len(items), it should return the amount of items in the collection
And you can now enumerate those items:

for item in items:
    print(item.properties)

each item is now of type ListItem that you can manipulate

also, the extra backslashes are for escaping the ' in the python string, those are totally normal

You're almost there.
Your items object is of type ListItemCollection.
If you try len(item), it should return the amount of items in the collection
And you can now enumerate those items:

for item in items:
    print(item.properties)

each item is now of type ListItem that you can manipulate

Thank you your help but print(len(items)) returns 0

If it's not a problem with the resource_url. could it possibly be something wrong with the App Permissions I set for the SharePoint site? I followed the directions from https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs (including setting the permission request XML), but when I return to the _layouts/15/appinv.aspx page and look my client ID, the permission request XML field is always empty... This is the first time I've done this so I'm not sure if that's how It's supposed to work or not.

I've been using these settings for the xml (and I select trust the application obviously).

   <AppPermissionRequests AllowAppOnlyPolicy="true">
   <AppPermissionRequest Scope="http://mycompany.sharepoint.com/sites/mysite" Right="FullControl" />
   </AppPermissionRequests>

This is a little different than what was in the instructions I found on docs.microsoft.com, but I only have the ability to grant access to my site, not the tenant. The app is listed in the 'Site collection app permissions' afterwards, so I assume it's working... but the fact I don't see any xml permissions when i return to appinv.aspx.

Good question, in my memory, when I was dealing with permission issues, it was very clear.
Your actually approved app would be at _layouts/15/AppPrincipals.aspx
And it is normal that you don't see the xml at appinv.

If my memory serves right, the scope I used was http://sharepoint/site/sitecollection though
(Edit:)Here's a cheat sheet : https://medium.com/ng-sp/sharepoint-add-in-permission-xml-cheat-sheet-64b87d8d7600

Have you tried to connect to the api through postman?

Here's what I get from the interpreter with my current connection.

>>> items = ctx.web.lists.get_by_title('Events').items
>>> items
<office365.sharepoint.listitems.listItem_collection.ListItemCollection object at 0x10d064af0>
>>> len(items)
0
>>> ctx.load(items)
>>> ctx.execute_query()
>>> len(items)
2

Greetings,

You're almost there.
Your items object is of type ListItemCollection.
If you try len(items), it should return the amount of items in the collection
And you can now enumerate those items:

for item in items:
    print(item.properties)

each item is now of type ListItem that you can manipulate

also, the extra backslashes are for escaping the ' in the python string, those are totally normal

agree and indeed extra backslashes are mandatory for escaping apostrophe character to make OData URL valid

This comment seems shed the light on root cause:

I've been using these settings for the xml (and I select trust the application obviously).

   <AppPermissionRequests AllowAppOnlyPolicy="true">
   <AppPermissionRequest Scope="http://mycompany.sharepoint.com/sites/mysite" Right="FullControl" />
   </AppPermissionRequests>

This is a little different than what was in the instructions I found on docs.microsoft.com, but I only have the ability to grant access to my site, not the tenant. The app is listed in the 'Site collection app permissions' afterwards, so I assume it's working... but the fact I don't see any xml permissions when i return to appinv.aspx.

the provided scope looks _invalid_ : http://mycompany.sharepoint.com/sites/mysite

  • mycompany: needs to be sharepoint predefined placeholder instead
  • sites/mysite same, needs to be a _valid_ placeholder, e.g. content/sitecollection or content/sitecollection/web

So, the solution would be to grant app permissions by providing a _valid_ permissions request, e.g.:

per site collection:

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>

per web:

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />
</AppPermissionRequests>

The list of _permission scopes_ could be found at Add-in permissions in SharePoint page

And last but not least, there is a wiki page which also provides step by step instruction how to setup app principal and grant permissions.

Thank you! Yes. The XML permission scope appears to have been the root cause of the issue!
From my initial testing it appears to be working now.

This is great. I've been using VBA scripts in MS Access for the past year to update my SharePoint lists. Having this part figured out puts me one step closer to porting everything to Python.

Was this page helpful?
0 / 5 - 0 ratings