0

Here's my code. It validates, but btn on click doesn't do anything.

<button onclick="createList()">Click here to create a list</button>
<script>
function createList() {

    var siteUrl="https://www.sharepoint.aspx";
    var clientCtx= new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    alert(oWebsite);
    var listCreationInfo=new SP.ListCreationInformation();
    listCreationInfo.set_title('CustomList');
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements);

    this.oList = oWebsite.get_lists().add(listCreationInfo);

    clientCtx.load(oList);
    clientCtx.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    );
  }

function onQuerySucceeded() {
alert("result");
  var result=oList.get_title()+ ' created.':
  alert(result);
}

function onQueryFailed(sender, args) {
  alert('Request failed. ' + args.get_messge() +
    '\n' + args.get_stackTrace());
}

</script>             
1
  • Can you add a breakpoint in your browser debug console in there and see where it's failing or if it is getting called at all?
    – Graham
    Sep 28, 2018 at 18:09

1 Answer 1

0

You have below typing mistakes in your code also.

  • you have written var oWebsite = clientContext.get_web(); but your context variable name is clientCtx. So this line should be var oWebsite = clientCtx.get_web();
  • you have written : instead of ; in success function (before the alert)

Refer below code it should work :

Note: Please replace siteUrl with your site url.

function createList() {
  var siteUrl = "https://abc.sharepoint.com/sites/mySite"; //use your site url here
  var clientCtx = new SP.ClientContext(siteUrl);
  var oWebsite = clientCtx.get_web();
  alert(oWebsite);
  var listCreationInfo = new SP.ListCreationInformation();
  listCreationInfo.set_title('CustomList');
  listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
  this.oList = oWebsite.get_lists().add(listCreationInfo);
  clientCtx.load(oList);
  clientCtx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
  alert("result");
  var result = oList.get_title() + ' created.'; alert(result);
}

function onQueryFailed(sender, args) {
  alert('Request failed. ' + args.get_messge() + '\n' + args.get_stackTrace());
}
3
  • I'm very new to JS, still lots of learning ahead. I made those changes, but the site URL is that the URL of where I create a new list or where I want to view the new list in a page? Sep 28, 2018 at 19:01
  • No problem ...just tell me if you are creating list in the same site or any sub site. Sep 28, 2018 at 19:33
  • If you are creating list in the same site and not the sub site then just write "var clientCtx = new SP.ClientContext.get_current();" . You do not have to pass the URL here...it will be useful to get context of current site. Sep 28, 2018 at 19:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.