Thursday, July 24, 2014

Sample JQuery in SharePoint create List Item, add values to DropDown

Sample JQuery in SharePoint create List Item, add values to DropDown

<script src="../../../_layouts/RKUJqu/jquery.min.js" type="text/javascript"></script>
<SharePoint:ScriptLink ID="ScriptLink1" Name="sp.js" LoadAfterUI="true" Localizable="false" runat="server" ></SharePoint:ScriptLink>
<table>
<tr>
<td>
Sample Jquery and ECMA/Javascript
</td>
</tr>
<tr>
<td>
<label for="txtTitle"> Enter Title</label>
</td>
<td>
    <br />
<input id="txtTitle" type="text" value="enter some text"/>
</td>
</tr>
<tr>
<td>
<input id="Submit1" type="submit" value="submit" onclick="createListItem()" />
</td>
</tr>
<tr>
<td>
    <select id="ddSelect" name="ddSelect">
       
    </select></td>
</tr>
</table>
<script language="javascript">

    $(document).ready(function () {
        //alert('Hi');
        debugger;
        ddfill();
    });
      function ddfill() {
          var clientContext = new SP.ClientContext.get_current();
          var oList = clientContext.get_web().get_lists().getByTitle('RKUTest');
         
          var camlQuery = new SP.CamlQuery();
          camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
          this.collListItem = oList.getItems(camlQuery);
          clientContext.load(collListItem);
          clientContext.executeQueryAsync(Function.createDelegate(this, this.addDDL), Function.createDelegate(this, this.onQueryFailed));

         
      }
//    function SaveListItem() {
//        // alert($("#txtTitle").val());
//        var clientContext = null;
//        var web = null;
//        context = new SP.ClientContext.get_current();
//        web = context.get_web();
//        this.list = web.get_lists().getByTitle('RKUTest');
//        this.oListItem
//    }

   // var siteUrl = 'http://units.mil.intra/sites/DGHR/h/default.aspx';

    function createListItem() {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('RKUTest');
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);

        oListItem.set_item('Title', $("#txtTitle").val());

        oListItem.update();

        clientContext.load(oListItem);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed1));
    }

    function addDDL() { //onQuerySucceeded1
        debugger;
        //alert('Item created: ' + oListItem.get_title());
        var listItemEnumerator = collListItem.getEnumerator();

        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            debugger;
            $('#ddSelect').append("<option>" + oListItem.get_item('Title') + "</option>");
           // listItemInfo += '\nID: ' + oListItem.get_id() +
           // '\nTitle: ' + oListItem.get_item('Title') +
           // '\nBody: ' + oListItem.get_item('Body');
        }
    }

    function onQueryFailed1(sender, args) {

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

    function onQuerySucceeded() {

        alert('Item created: ' + oListItem.get_title());
    }

    function onQueryFailed(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

No comments:

Post a Comment