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, November 29, 2012

Search LookUp Column and select the value in the Dropdownlist using ECMA Script and JQuery

var myColl="";
var myColl1="";
var strCurrentWeb="";
var strCurrentSiteUrl="";
function SearchLookUpData()
{
    try
    {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        strCurrentWeb=context.get_url();
        strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
        var list = web.get_lists().getByTitle('Book Names');
        var sQuery="<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>4</Value></Eq></Where></Query></View>";
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(sQuery);
        this.myColl1 = list.getItems(camlQuery);
        context.load(this.myColl1, 'Include(Title, ID, Category)');
        context.executeQueryAsync(Function.createDelegate(this, this.SearchLookUpDataSuccess), Function.createDelegate(this, this.SearchLookUpDataFailed));
    }
    catch(Ex)
    {
        alert(Ex);
    }
}

function SearchLookUpDataSuccess()
{
    var strBookNames="";
    strBookNames+="<Table>";
    if(myColl1.get_count()!=0)
    {
        var listEnumerator = this.myColl1.getEnumerator();
        while (listEnumerator.moveNext())
        {
            var item = listEnumerator.get_current();
            var strCategoryID=item.get_item('Category').get_lookupId();
            $('select#ddlCategory').val(strCategoryID);
            break;
        }
    }
    else
    {
       

    }   
}

function SearchLookUpDataFailed(sender, args)
{
    alert('failed. Message:' + args.get_message());
}

function BindData()
{
    ExecuteOrDelayUntilScriptLoaded(BindLookUpData, "sp.js");
    ExecuteOrDelayUntilScriptLoaded(SearchLookUpData, "sp.js");
}


function BindLookUpData()
{
    try
    {
        //debugger;
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        strCurrentWeb=context.get_url();
        strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
        var list = web.get_lists().getByTitle('Book Category');
        var sQuery="<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(sQuery);
        this.myColl = list.getItems(camlQuery);
        context.load(this.myColl, 'Include(Title, ID)');
        context.executeQueryAsync(Function.createDelegate(this, this.BindLookUpDataSuccess), Function.createDelegate(this, this.BindLookUpDataFailed));
    }
    catch(Ex)
    {
        alert(Ex);
    }
}

function BindLookUpDataSuccess()
{
    var strBookNames="";
    if(myColl.get_count()!=0)
    {
        var listEnumerator = this.myColl.getEnumerator();
        strBookNames+="<Select id='ddlCategory' width='158px'>";
        strBookNames+="<Option>Select Category</Option>";
        while (listEnumerator.moveNext())
        {
            var item = listEnumerator.get_current();
            var strItemID=item.get_item('ID');
            var strTitle=item.get_item('Title');
            strBookNames+="<Option Value='"+strItemID+"'>"+strTitle+"</Option>";
        }
        strBookNames+="</Select>";
    }
    document.getElementById("tdCategory").innerHTML=strBookNames;
}

function BindLookUpDataFailed(sender, args)
{
    alert('failed. Message:' + args.get_message());
}

_spBodyOnLoadFunctionNames.push("BindData");

No comments:

Post a Comment