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

Binding Top Five Announcements to a simple HTML Table using ECMA Script

var myColl="";
var strCurrentWeb="";
var strCurrentSiteUrl="";
function BindAnnouncementsData()
{
                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('Announcements');
                                var sQuery="<View><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Geq><FieldRef Name='Expires' /><Value Type='DateTime'><Today /></Value></Geq></Where></Query><RowLimit>5</RowLimit></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.BindAnouncementsDataSuccess), Function.createDelegate(this, this.BindAnnouncementsDataFailed));
                }
                catch(Ex)
                {
                                alert(Ex);
                }
}

function BindAnouncementsDataSuccess()
{
                var strBookNames="";
                strBookNames+="<Table>";
                if(myColl.get_count()!=0)
                {
                                var listEnumerator = this.myColl.getEnumerator();
                                strBookNames+="<Tr>";
                                strBookNames+="<Td Align='Center' class='HeadingClass'>";
                                strBookNames+="Top Five Announcements";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";
                                strBookNames+="<Tr>";
                                strBookNames+="<Td class='HeadingClass'>";
                                strBookNames+="Title";
                                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 strUrl=strCurrentSiteUrl+"Lists/Announcements/DispForm.aspx?ID="+strItemID+"&Source="+window.location.href;
                                                strBookNames+="<Tr>";
                                                strBookNames+="<Td class='LoopRecordsClass'>";
                                                strBookNames+="<A href='"+strUrl+"' class='LoopRecordsClass'>"+strTitle+"</a>";
                                                strBookNames+="</Td>";
                                                strBookNames+="</Tr>";

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

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

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

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

_spBodyOnLoadFunctionNames.push("BindData");

No comments:

Post a Comment