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.

Wednesday, November 28, 2012

Insertion into a LookUp Column using ECMA Script

var myColl="";
var strCurrentWeb="";
var strCurrentSiteUrl="";
function BindLookUpData()
{
    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 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());
}

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

function SaveLookUpColumn()
{
        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 Names');
        var listItemCreationInfo = new SP.ListItemCreationInformation();
        var newItem = list.addItem(listItemCreationInfo);
        newItem.set_item('Title', document.getElementById("txtBookName").value);
        var _newLookupField = new SP.FieldLookupValue();
        _newLookupField.set_lookupId($("#ddlCategory").val());
        newItem.set_item('Category', _newLookupField);
        newItem.update();
        context.executeQueryAsync(Function.createDelegate(this, this.LookUpInsertSuccess), Function.createDelegate(this, this.LookUpInsertFailed));
}

function LookUpInsertSuccess()
{
    alert('LookUp Column Successfuly Inserted');
    document.getElementById("txtBookName").value="";
    $("select#ddlCategory").get(0).selectedIndex=0;
}

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

function InsertLookUpColumn()
{
    ExecuteOrDelayUntilScriptLoaded(SaveLookUpColumn, "sp.js");
}

_spBodyOnLoadFunctionNames.push("BindData");

No comments:

Post a Comment