I'm trying to retrieve list from Sharepoint using C# and get that list using JavaScript from client.

Can someone help me?

This is my C# code.

        SPWeb web = SPContext.Current.Web;
        SPList maListe = web.Lists["Lycee"];
        SPListItemCollection mesItem = maListe.Items;         

        foreach (SPListItem monItem in mesItem)
        {
            //get Liste but i don't kwnow what to do?
        }

2 Answers

0
Ahmed Yousif On

you can use javascript to access the list directly as following code sample

$(function () {
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
});
var siteUrl = '/sites/MySiteCollection';

function retrieveListItems() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Lycee');

    this.collListItem = oList.getItems();

    clientContext.load(collListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') 
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

to read more documentation check following link

0
Community On

it's working now.

i just call my function like this.

_spBodyOnLoadFunctionNames.push("getListLycee");

tanks for your help.