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

Binding SharePoint ListItems to DropDownList using ECMA Script

Note:-
Use Bind() function in the Page Load. Before that Create a TD with ID tdItemID.

function Bind()

{

    ExecuteOrDelayUntilScriptLoaded(BindDropDown, "sp.js");

}


function BindDropDown()
{
                var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('Login');
    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.BindSuccess), Function.createDelegate(this, this.Bindfailed));
}
Bind();

function BindSuccess()
{
                var listEnumerator = this.myColl.getEnumerator();
                var strDropDown="";
                strDropDown+="<Select id='ddlItemID'>";
                strDropDown+="<Option>";
                strDropDown+="Select Username";
                strDropDown+="</Option>";
                while (listEnumerator.moveNext())
                {
                    var item = listEnumerator.get_current();
                    strDropDown+="<Option value='"+item.get_item('ID')+"'>";
                    strDropDown+=item.get_item('Title');
                    strDropDown+="</Option>";               
    }
   strDropDown+="</Select>";
                if(document.getElementById("tdItemID"))
                {
                                document.getElementById("tdItemID").innerHTML=strDropDown;
                }
}

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

No comments:

Post a Comment