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

Retrieving Data from a LookUp and Binding Lookup Field as a Hyperlink using Client Object Model



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 Names');
                                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, Category)');
                                context.executeQueryAsync(Function.createDelegate(this, this.BindLookUpDataSuccess), Function.createDelegate(this, this.BindLookUpDataFailed));
                }
                catch(Ex)
                {
                                alert(Ex);
                }
}

function BindLookUpDataSuccess()
{
                var strBookNames="";
                strBookNames+="<Table>";
                if(myColl.get_count()!=0)
                {
                                var listEnumerator = this.myColl.getEnumerator();
                                strBookNames+="<Tr>";
                                strBookNames+="<Td Colspan='2' Align='Center' class='HeadingClass'>";
                                strBookNames+="Retrieving Data from a Lookup column";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";
                                strBookNames+="<Tr>";
                                strBookNames+="<Td class='HeadingClass'>";
                                strBookNames+="Person Name";
                                strBookNames+="</Td>";
                                strBookNames+="<Td class='HeadingClass'>";
                                strBookNames+="Category";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";
                                while (listEnumerator.moveNext())
                                {
                                                var item = listEnumerator.get_current();
                                                var strItemID=item.get_item('ID');
                                                var strTitle=item.get_item('Title');
                                                var strCategory=item.get_item('Category').get_lookupValue();
                                                var strCategoryID=item.get_item('Category').get_lookupId();
                                                var strUrl=strCurrentSiteUrl+"Lists/Book Names/DispForm.aspx?ID="+strItemID+"&Source="+window.location.href;
                                                var strLookUpUrl=strCurrentSiteUrl+"Lists/Book Category/DispForm.aspx?ID="+strCategoryID+"&Source="+window.location.href;
                                                strBookNames+="<Tr>";
                                                strBookNames+="<Td class='LoopRecordsClass'>";
                                                strBookNames+="<A href='"+strUrl+"' class='LoopRecordsClass'>"+strTitle+"</a>";
                                                strBookNames+="</Td>";
                                                strBookNames+="<Td class='LoopRecordsClass'>";
                                                strBookNames+="<A href='"+strLookUpUrl+"'>"+strCategory+"</A>";
                                                strBookNames+="</Td>";
                                                strBookNames+="</Tr>";

                                }
                }
                else
                {
                                strBookNames+="<Tr>";
                                strBookNames+="<Td Colspan='2' Align='Center' class='HeadingClass'>";
                                strBookNames+="No Books Data Available";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";

                }
               
               
                strBookNames+="</Table>";
                document.getElementById("tdBookNames").innerHTML=strBookNames;
}

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

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

_spBodyOnLoadFunctionNames.push("BindData");

No comments:

Post a Comment