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.

Tuesday, October 09, 2012

Search and Update using Client Object Model

Note:-
Use Edit() Function in Search Button Onclick event  and Update() Function in Update Button Onclick Event.

var strItemID="";
var myColl="";

 function Edit()

{

    ExecuteOrDelayUntilScriptLoaded(SearchRecord, "sp.js");

}

function Update()

{

    ExecuteOrDelayUntilScriptLoaded(UpdateRecord, "sp.js");

}
function SearchRecord()
{
                try
                {
                var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('Login');
                var sQuery = '<View Scope=\'RecursiveAll\'>'+
                        '<Query>'+
                            '<Where>'+
                            '<Eq>'+
                                '<FieldRef Name=\'Title\'/>' +
                                '<Value Type=\'Text\'>' + document.getElementById("txtUserName").value +'</Value>'+
                            '</Eq>'+
                            '</Where>'+
                        '</Query>'+
                             '</View>';
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(sQuery);
        this.myColl = list.getItems(camlQuery);
        context.load(this.myColl, 'Include(Title, Password, ID)');
        context.executeQueryAsync(Function.createDelegate(this, this.SearchSuccess), Function.createDelegate(this, this.failed));
                }
                catch(ex)
                {
                                alert(ex.message);
                }
}

function SearchSuccess()
{
                document.getElementById("divItemID").innerHTML="";
                var listEnumerator = this.myColl.getEnumerator();
                while (listEnumerator.moveNext())
                {
                    var item = listEnumerator.get_current();               
                    document.getElementById("txtPassWord").value = item.get_item('Password');
                    document.getElementById("divItemID").innerHTML=item.get_item('ID');
                    break;
    }
}
function failed(sender, args) {
    alert('failed. Message:' + args.get_message());
}

function UpdateRecord()
{
                var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('Login');
    var LoginRecord = list.getItemById(strItemID);
    LoginRecord.set_item('Password', document.getElementById("txtPassWord").value);
    LoginRecord.update();
    context.executeQueryAsync(Function.createDelegate(this, this.Updatesuccess), Function.createDelegate(this, this.Updatefailed));
}

function Updatesuccess()
{
                alert("Record Updated Successfully");
                document.getElementById("txtUserName").value="";
                document.getElementById("txtPassWord").value="";
               
}

function Updatefailed()
{
                alert('Update Failed');
}

 

No comments:

Post a Comment