Ron Splinter
Ron Splinter

Reputation: 191

Retrieve list items from sharepoint

I am trying to retrieve some Items from a sharepoint 2013 online list. I know for sure that there are 3 Items in the list. Somehow the code does not return any Items. Does anybody know why not? In debugging listItems is empty.

User spUser = null;
SharePointContextToken contextToken;
string accessToken;
Uri sharepointUrl;

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;
    var clientContext = TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), accessToken);
    Web web = clientContext.Web;
    List list = clientContext.Web.Lists.GetByTitle("CustomListFacturen");

    clientContext.Load(list);

    CamlQuery query = new CamlQuery();
    query.ViewXml = "<View><RowLimit>100</RowLimit></View>";

    ListItemCollection listItems = list.GetItems(query);
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();

    if (listItems.Any())
    {
        ViewBag.Message = "Items are found!!!";
    }
}

Upvotes: 0

Views: 6604

Answers (2)

Max Melcher
Max Melcher

Reputation: 200

Your app does not have enough permissions.

Upvotes: 0

Craig Riter
Craig Riter

Reputation: 106

I didn't try and run your code, but it looks almost exactly like this code, basic CSOM list operations.

You don't need that first .Load(list) and you could try using the CamlQuery.CreateAllItemsQuery(100) instead of the CAML.

Upvotes: 1

Related Questions