locked
Updating lists RRS feed

  • Question

  • Hi!

    I'm trying to update my list, because now it only writes out the correct level and xp, but it doesn't change it in the list. I've written a function updateListItem(). Im trying to take the data from listItemLevel, which is set in onQuerySucceeded and put it into the column 'jg9d' which is the internal name. How do i do that? Here's my code:

    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"> </script><script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" type="text/javascript"> </script> 
    <table _moz_resizing="true"> 
       <!-- <caption>rubrik</caption> --> 
          <tbody>
             <tr>  
                <td style="font-size: 25px;">Name</td>
                <td style="font-size: 25px;">&#160;:&#160;</td>
                <td id="messageName" style="font-size: 25px; color: orange;">Oscar Salomonsson</td>
             </tr> 
             <tr>  
                <td style="font-size: 25px;">Level</td>
                <td style="font-size: 25px;">&#160;:&#160;</td>
                <td id="messageLevel" style="font-size: 25px; color: orange;">1</td>
             </tr> 
             <tr>  
                <td style="font-size: 25px;">XP </td> 
                <td style="font-size: 25px;">&#160;:&#160;</td>
                <td id="messageXP" style="font-size: 25px; color: orange;">70</td>
             </tr> 
          </tbody>
    </table> 
    <script type="text/javascript">

    var currentUser;

    $(document).ready(function ()
    {
    ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
    }); //End Document.ready

    function init()
    {
    var context = new SP.ClientContext.get_current();
    this.website = context.get_web();
    this.currentUser = website.get_currentUser();
    context.load(currentUser);
    context.executeQueryAsync(Function.createDelegate(this, this.onNickQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onNickQuerySucceeded() 
    {
    //alert(currentUser.get_loginName());
        //alert(currentUser.get_id());
        //alert(currentUser.get_title());
        //alert(currentUser.get_email());
    retrieveListItems();
    }

    function retrieveListItems() 
    {
    //$('#messageDEBUG').text('begin retrival');    //debug
        var clientContext = new SP.ClientContext('https://miunsims.sharepoint.com');
    var oList = clientContext.get_web().get_lists().getByTitle('DB_Person');
        var camlQuery = new SP.CamlQuery();
        
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
            '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
            
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    }

    function updateListItem() {
    var clientContext = new SP.ClientContext('https://miunsims.sharepoint.com');
    var oList = clientContext.get_web().get_lists().getByTitle('DB_Person');
    this.oListItem = getItemById(2);

    oListItem.set_item('jg9d', listItemLevel);

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

    function onQuerySucceeded(sender, args) 
    {
    //$('#messageDEBUG').text('success');   //debug
        var listItemID;
    var listItemTitle;
    var listItemName;
    var listItemLevel;
    var listItemAchievement;
    var listItemXP;

    var listItemEnumerator = collListItem.getEnumerator();

        while (listItemEnumerator.moveNext()) 
    {
            var oListItem = listItemEnumerator.get_current();
    if(currentUser.get_title() == oListItem.get_item('Name').get_lookupValue())  //if my username equals DB_Person name row
    {
    //listItemID = oListItem.get_id();
                //listItemTitle = oListItem.get_item('Title');
    listItemName = oListItem.get_item('Name').get_lookupValue();  //for lookup fields
    listItemXP = oListItem.get_item('rwub'); //using internal name of column
    listItemLevel = Math.floor(listItemXP/50);
    break;
    }//END if user match
        }

        $('#messageName').text(listItemName.toString()   );
    $('#messageLevel').text(listItemLevel.toString() );
    $('#messageXP').text(listItemXP.toString()       );



    }

    function onQueryFailed(sender, args) 
    {
        alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
    }

    </script> 

    Thursday, November 5, 2015 11:21 AM

Answers

  • Hi Salle123,

    According to your description, you want to update the list item by coding. But in your code, there is no place to call the updateListItem() method. Please Add this method into your onQuerySucceeded(sender,args) method.

    By the way, have you overridden the “getItemById(int id)” method? If not, it should be “oList.getItemId(2)”

    Best regards,

    Linda Zhang


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

    • Proposed as answer by Victoria Xia Friday, November 13, 2015 9:27 AM
    • Marked as answer by Victoria Xia Monday, November 23, 2015 9:27 AM
    Friday, November 6, 2015 8:22 AM