Domanda

Sono stato bloccato su questo problema per un paio di giorni, Sviluppo il modello dell'app su SharePoint online utilizzando il modello ospitato del provider.

Dal clientWebPart, voglio accedere all'elemento di elenco SharePoint. Posso accedere all'oggetto elenco ma non posso ottenere l'elemento dell'elenco (recuperare sempre vuoto).

Seguito già il codice di esempio su "Apps per SharePoint Sample Pack - SharePoint 2013 Eseguire le operazioni di accesso ai dati di base utilizzando CSOM in app", ma ancora non funziona.

Ecco il mio codice:

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

C'è qualche errore di trivia che mi manchi?

Grazie in anticipo

Modifica: creo nuovo nuovo progetto e segui le istruzioni di ComePer: Creare un'applicazione di base con un provider di base per SharePoint e il codice di append per Recupera elemento di elenco, ma ancora restituire 0 elemento.

Qualcuno ha mai avuto successo a raggiungere questo?

È stato utile?

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top