none
get values of field of a list RRS feed

  • Question

  • i want to retrive the value of the field 'employeeName' from my list'employeeDetails'. i have the code below.i got error as-

    uncaught syntax error : unexpected identifier.how to fix this?if you have any other method then plz suggest.

    <!DOCTYPE html>
    <head>
    <script type="text/javascript" src="/sites/practice/Shared%20Documents/jquery-3.1.0.min.js"></script>
     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>
    <script>
    string server = "https://mogembo1.sharepoint.com/sites/Practice";
    var ctx = new ClientContext(server);
    var web = ctx.Web;
    var list = web.Lists.GetByTitle("employeeDetails");
    var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());

    // always use QueryTrimming to minimize size of 
    // data that has to be transfered

    ctx.Load(listItemCollection,
               eachItem => eachItem.Include(
                item => item,
                item => item["employeeName"]));
    // ExecuteQuery will pull all data from SharePoint
    // which has been staged to Load()
    ctx.ExecuteQuery();

    foreach(ListItem listItem in listItemCollection)
    {
       Console.WriteLine(listItem["employeeName"]);
    }
    }
    </script>
    </head>
    </html>

    Wednesday, December 28, 2016 11:46 AM

Answers

  • Hi laxmikant,

    We can use SharePoint JavaScript Client Object Model to get list item fields like below:

    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(retrieveListItemsInclude, "sp.js");
    
    var siteUrl = '/sites/teams';
    
    function retrieveListItemsInclude() {
    
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('List1');
    
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);
    
        clientContext.load(collListItem, 'Include(Id,Title)');
    
        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();
            console.log(oListItem.get_item('Title'));
                
        }
    }
    
    function onQueryFailed(sender, args) 
    {
    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>

    More information:

    How to: Retrieve List Items Using JavaScript

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by laxmikant.b2 Thursday, December 29, 2016 5:37 AM
    Thursday, December 29, 2016 2:58 AM

All replies

  • Hi laxmikant,

    We can use SharePoint JavaScript Client Object Model to get list item fields like below:

    <script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(retrieveListItemsInclude, "sp.js");
    
    var siteUrl = '/sites/teams';
    
    function retrieveListItemsInclude() {
    
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('List1');
    
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);
    
        clientContext.load(collListItem, 'Include(Id,Title)');
    
        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();
            console.log(oListItem.get_item('Title'));
                
        }
    }
    
    function onQueryFailed(sender, args) 
    {
    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>

    More information:

    How to: Retrieve List Items Using JavaScript

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by laxmikant.b2 Thursday, December 29, 2016 5:37 AM
    Thursday, December 29, 2016 2:58 AM
  • Hi,

    I think you messed up your code base by mixing the SPServices,jQuery, Console Application with Managed code (CSOM). Please follow the Jerry's code.

    In html page, Console.WriteLine and foreach won't work.


    Murugesa Pandian | MCPD | MCTS | SharePoint 2010 |


    Thursday, December 29, 2016 5:13 AM