Retrieve list item using CSOM on SharePoint online isn't working
Question
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;
TokenHelper.TrustAllCertificates();
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
clientContext.ExecuteQuery();
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
clientContext.ExecuteQuery();
}
}
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 ?
Solution 2
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"
OTHER TIPS
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).
context.Load(items);
context.ExecuteQuery();
foreach (ListItem listItem in items)
{
// We have all the list item data. For example, Title.
label1.Text = label1.Text + ", " + listItem["Title"];
}
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..