Sunday, October 14, 2012

Creating a List in SharePoint using ECMA Script

In this post, I will explain how to create a list in SharePoint using ECMA script. Use the ListCreationInformation object to define list properties and then pass this object to add (listCreationinformation object).
<script type="text/ecmascript">
function createSPList()
{
  var clientContext = new SP.ClientContext.get_current();
  var oWeb=clientContext.get_web();
  var listCreation = new SP.ListCreationInformation();
  listCreation.set_title('My Custom List');
  listCreation.set_templateType(SP.ListTemplateType.genericList);
  listCreation.set_description('List created using ECMA Script');

  this.oList = oWeb.get_lists().add(listCreation);
  clientContext.load(oList);

  clientContext.executeQueryAsync(Function.createDelegate(this,this.OnSuccess), Function.createDelegate(this,this.OnFailure));
  }

function OnSuccess()
{
 alert(oList.get_title() + ' created.');
 }

function OnFailure(sender, args)
{
 alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  }
 </script>

Refer the below link for list template type

No comments:

Post a Comment