I've been stuck on this problem for couple days, I develop app model on SharePoint online using provider hosted model.

From clientwebpart, i want to access sharepoint list item. I can access list object but cant get the list item (always retrieve empty).

I already follow the sample code at "Apps for SharePoint sample pack - SharePoint 2013 Perform basic data access operations by using CSOM in apps", but still does not work.

Here are my code :

SharePointContextToken contextToken;
Uri sharepointUrl;
string accessToken;

string contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);

if (contextTokenString != null)
    contextToken = TokenHelper.ReadAndValidateContextToken(contextTokenString, Request.Url.Authority);

    sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
    accessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;

    using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken))
        Web web = clientContext.Web;
        ListCollection lists = web.Lists;

        List selectedList = lists.GetByTitle("LeaveCategory");

        clientContext.Load<ListCollection>(lists); // this lists object is loaded successfully
        clientContext.Load<List>(selectedList);  // this list object is loaded successfully


        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = @"<View><Query><Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where></Query><ViewFields><FieldRef Name='ID' /></ViewFields></View>";

        Microsoft.SharePoint.Client.ListItemCollection listItems = selectedList.GetItems(camlQuery);

        clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(listItems); // problem here, this list items is return empty 


Is there any trivia mistake that i miss ?

thank you in advance

Edit : I create fresh new project and follow instruction of How to: Create a basic provider-hosted app for SharePoint, and append code for retrieve list item, but still return 0 item.

Did anyone have ever succeed achieving this ?

  • Please post specific error you are encountering on specific line. – Kunal Valecha Dec 24 '13 at 6:03
  • there is no error, it just return empty list (listItems.Count = 0) – adhitya febrian Dec 24 '13 at 6:19
  • Did you try excluding the query from the CamlQuery object and see if it returns any items? However, I don't see any issues in your query. – Nadeem Yousuf-AIS Dec 24 '13 at 6:31
  • yes, i have try waqas sarwar suggestion with CamlQuery.CreateAllItemsQuery(100);, but still no luck – adhitya febrian Dec 24 '13 at 8:08

you can use this code to retrive the items from MSDN:

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http:SiteUrl"); 

// Assume the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
// so that it grabs all list items, regardless of the folder they are in. 
CamlQuery query = CamlQuery.CreateAllItemsQuery(100); 
ListItemCollection items = announcementsList.GetItems(query); 

// Retrieve all items in the ListItemCollection from List.GetItems(Query). 
foreach (ListItem listItem in items) 
 // We have all the list item data. For example, Title. 
  label1.Text = label1.Text + ", " + listItem["Title"]; 


| improve this answer | |
  • i think it is more likely the same with my code – adhitya febrian Dec 24 '13 at 6:20
  • yes, but it is simple than yours. i think give it try. – Waqas Sarwar MVP Dec 24 '13 at 6:27
  • 1
    i try your code (except for initialize client context, because need token to authenticate to sharepoint online), but still return 0 item.. – adhitya febrian Dec 24 '13 at 8:14

Its working now,

its because we must add specific permission for the app at file AppManifest.xml

I add "List" at scope and give it permission "FullControl"

| improve this answer | |
  • 1
    You never mentioned that you are trying to get items from a list which is in host web and not in app web – Nadeem Yousuf-AIS Dec 27 '13 at 4:48

I think issue is in your Caml Query, Just check removing Every condition from it i.e Where and also , if data is coming. After that you could cross-check your Query..

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.