2

I want to retrieve data from SharePoint lists (office 365) in a client web part created in SharePoint provider hosted app. The majority of tutorials i found were about SP hosted app. Can someone help me please?

1 Answer 1

2

You can use below code to read lists that are available in host web. Make sure you app has read permission

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
    clientContext.Load(clientContext.Web, web => web.Title);
    clientContext.ExecuteQuery();

    ListCollection lists = clientContext.Web.Lists;
    List list = lists.GetByTitle("ListTitle");

    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";
    Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(camlQuery);
    clientContext.Load<ListCollection>(lists);
    clientContext.Load<List>(list);
    clientContext.Load<Microsoft.SharePoint.Client.ListItemCollection>(items);

    clientContext.ExecuteQuery();
    foreach (Microsoft.SharePoint.Client.ListItem item in items)
    {
        Response.Write("<br />" + item.FieldValues["Title"]);
    }
}
3
  • When I publish my app only the first field is retrieved,how can I read all the fields of the SP list please?
    – N.Sahraoui
    Apr 16, 2015 at 16:33
  • can you more explain for me please it doesn't work :/
    – N.Sahraoui
    Apr 17, 2015 at 10:17
  • Take a look at this link.. it shows how to use clientContext.Load... msdn.microsoft.com/en-us/library/office/… Apr 17, 2015 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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