0
votes

m developing an app model on SharePoint online using the provider hosted model.

From clientwebpart, I want to access a Sharepoint list item. I can access list a object but can't get the list item (always empty). I already followed 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 is 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 trivial mistake that I am making?

Also, I am trying to create fresh new project and follow instruction of How to: Create a basic provider-hosted app for SharePoint, and appending code toretrieve list item, but it still returns 0 item.

Did anyone ever succeeded achieving this?

2

2 Answers

1
votes

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"

0
votes

After doing only below along with List scope full permissions, its worked for me:

In the Scope cell, choose Web from the drop down list.

In the Permission cell, choose Read from the drop down list.