Question

J'ai été coincé sur ce problème pendant quelques jours, Je développe un modèle d'application sur SharePoint Online à l'aide du modèle hébergé fournisseur.

de clientwebPart, je souhaite accéder à l'élément de liste SharePoint. Je peux accéder à l'objet de la liste mais je ne peux pas obtenir l'élément de liste (récupérez toujours vide).

Je suis déjà suivi du code exemple sur "Apps pour SharePoint Sample Pack - SharePoint 2013 Effectuer des opérations d'accès aux données de base à l'aide de CSOM dans les applications", mais ne fonctionne toujours pas.

Voici mon 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();
    }
}

Y a-t-il une erreur de trivialité qui me manque?

Merci d'avance

EDIT: Je crée un nouveau projet nouveau et suivez les instructions de CommentPour: créer une application de base au fournisseur-hébergé pour SharePoint et Ajout du code pour récupérer l'élément de liste, mais retournez toujours 0 article.

Quelqu'un a-t-il déjà réussi à atteindre cela?

Était-ce utile?

La 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"

Autres conseils

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"]; 
} 

Reference

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..

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top