Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, January 31, 2013

Insertion into a SharePoint List using JQuery and SharePoint Web Services

Please go to this post to understand what  JS files you need to download to achieve the required functionality.


Note:-
1.       Shown how to insert a data into a Lookup Column
2.       Shown how to insert data into a Single Lines of Text
3.       Shown how to insert data into a Choice column.
var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);
var strGender="";
function InsertRecord()
{
                if($("#txtTitle").val()=="")
                {
                                alert('Please Select Title');
                                return false;
                }
               
                if($("#ddlCountry").get(0).selectedIndex==0)
                {
                                alert('Please Select Country');
                                return false;
                }
                var strTitle=$("#txtTitle").val();
                var strCountry=$("#ddlCountry").val();
                if($('#rbMale:checked'))
                {
                                strGender="Male";
                }
                if($('#rbFemale:checked'))
                {
                                strGender="Female"
                }
                var oWs = objList.quickAddListItem('JWS Practice', [{Title:(strTitle),Country:(strCountry),Gender:(strGender)}]);
                if(oWs.status==200)
                {
                                alert('Insert Saved Successfully');
                                $("#txtTitle").val("");
                                $("#ddlCountry").get(0).selectedIndex=0;
                                $("#rbMale").attr("checked",true);
                }
}

$(document).ready(function()
{             
                $("#btnSave").click(function()
                {
                                InsertRecord();
                });
               
                BindDropDown();

});

function BindDropDown()
{

    var sQuery = "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";
    var strViewFields = "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /></ViewFields>";
    var result = objList.getListItems("Country", "", sQuery, strViewFields, "", "", "");
   
    var strBindDropDown = "";
    strBindDropDown += "<Option>";
    strBindDropDown += "--Select Country--";
    var ItemRows = '';
    if ($.browser.webkit)
    {
        ItemRows = "row";
    }
    else
    {
        ItemRows = "z\\:row";
    }
    if ($(result.responseXML).find(ItemRows).length > 0)
    {
        $(result.responseXML).find(ItemRows).each(function ()
        {
            strBindDropDown += "<Option value=" + $(this).attr('ows_ID') + ">";
            strBindDropDown += $(this).attr('ows_Title')
            strBindDropDown += "</Option>";
        });
    }
    strBindDropDown += "</Option>";
    $("#ddlCountry").html(strBindDropDown);
}

No comments:

Post a Comment