0

I am trying to retrieve list data using JavaScript. But something goes wrong. I am trying to debug the code but I am not able to understand that thing.
Following is the JavaScript Code:

ExecuteOrDelayUntilScriptLoaded(PopulateDepartments, "sp.js");
var _ctx = null;
var _web = null;
var _allItems = null;

function PopulateDepartments() {
    debugger;
    _ctx = SP.ClientContext.get_current();
    _web = _ctx.get_web();
    var list = _web.get_lists().getByTitle("ServiceType");
    var query = new SP.CamlQuery();
    query.set_viewXml("<View><Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query></View>");
    _allItems = list.getItems(query);
    _ctx.load(_allItems, 'Include(Title,ID)');
    debugger;
    _ctx.executeQueryAsync(Function.createDelegate(this, this.PopulateDepartmentSuccess),
        Function.createDelegate(this, this.PopulateDepartmentFaild));
}

function PopulateDepartmentSuccess() {
    var ddlEntry = this.document.getElementById("ddl1");
    ddlEntry.options.length = 0;
    var listEnumerator = _allItems.getEnumerator();
    while (listEnumerator.moveNext()) {
        var currentItem = listEnumerator.get_current();
        ddlEntry.options[ddlEntry.options.length] = new Option(currentItem.get_item("Title"), currentItem.get_item("ID"));
    }
}

function PopulateDepartmentFaild() {
    alert("Something went Wrong....!!");
}

Whenever I run this code it shows me alert box.
Please Help..

4
  • did you try to use only <OrderBy><FieldRef Name='Title'/></OrderBy> in your query and set it to the query attribute instead of set_viewXml ? Commented Feb 15, 2013 at 5:04
  • Hey @user1073122 I tried that one but it is not working. Still it display the alert popup. Commented Feb 15, 2013 at 5:24
  • I assumed you fixed the PopulateDepartmentFaild misspelling? Commented Feb 16, 2013 at 15:25
  • @Trikks Yes... It was by mistake I put that thing. It in not the main problem. Commented Feb 18, 2013 at 4:38

2 Answers 2

2

There are times when this doesn't takes the correct reference.Check if it works with removing this reference .so Instead of this _ctx.executeQueryAsync(Function.createDelegate(this, this.PopulateDepartmentSuccess), Function.createDelegate(this, this.PopulateDepartmentFaild));

try using something like this

_ctx.executeQueryAsync(PopulateDepartmentSuccess,PopulateDepartmentFaild);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I got the problem solved by other way.
0

I think.If your Creating Sharepoint App, it is necessary to give permissions to web in AppManifest.xml.

1 Comment

Please explain in details. Otherwise, this should be a comment. Not an answer. If you don't have enough repo, wait, have patient, generate repo positively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.