0

I have some simple code that uses CSOM to read the items in a SharePoint list hosted in SharePoint Online.

        List list = ClientContext.Web.Lists.GetById(id);

        ClientContext.Load(list,
            a => a.ItemCount,
            a => a.Title);


        ListItemCollection items = list.GetItems( CamlQuery.CreateAllItemsQuery());

        ClientContext.Load(items);
        ClientContext.ExecuteQuery();

        foreach (ListItem li in items)
        {
        .... // Do some Stuff ...
        }

At the end of the ExecuteQuery call I can access the list Title ( "MyList" ) and the record count ( 7 items in total ) but the ListItemsCollection items always has a count of zero.

No errors are thrown.

What do I need to do to actually fetch the list items? Am I missing another call to the CSOM or could their be a permission issue ( wouldn't SharePoint Online tell me if that were the case? )

Help gratefully received!

user3658298
  • 341
  • 1
  • 5
  • 15

2 Answers2

0

Try to use 'include ' to get the data in client context.

ClientContext.Load(list, a.Include(p => p.ItemCount, p => p.Title); ClientContext.ExecuteQuery(); and then check whether item count and title is there or not.

Please let us know whether this list is with in app or not. If it is on the host web then you have to provide read permission under appmanifest.xml->permission tab

Rahul
  • 154
  • 1
  • 9
  • Yes. The item count and the title are always there .... that's not the problem. The problem is that I cannot fetch the ListItems. – user3658298 Jan 15 '15 at 15:20
  • The above mentioned code only run for the list or lib which are under App not on host web. For hostweb you have to provide permission.Please let us know whether this list is with in app or not. If it is on the host web then you have to provide read permission under appmanifest.xml->permission tab – Rahul Jan 15 '15 at 15:42
  • The list is on the HostWeb. If I add permission to the manifest that allows an end user to choose which list the provider hosted app can read then how does the provider hosted application know which list it has permission to access? – user3658298 Jan 15 '15 at 16:04
  • Ideally when you give permission to web level as read then user get the access to retrieve the data from list. Also you have to specify in your code which list you are referring or iterate web.lists. – Rahul Jan 16 '15 at 05:06
-1

I had the same problem using Sharepoint 2013 On-Premises and STS as truster broker.

Same behaviour, no errors and I'm getting zero items/documents using CamlQuery.CreateAllItemsQuery() to get a "SELECT ALL" CamlQuery

I solved this creating a new app principal through the page:

/layouts/15/appregnew.aspx

and then assigning enough permissions using the page:

 /layouts/15/appinv.aspx 

to the HostWeb (not the AppWeb) containing the list. In my case:

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

There's still a bug ( Sharepoint bug ) because in this case It's not throwing a "access denied" error

MaxRiz
  • 1
  • 2