Many a time we have a requirement that we need to create a Sharepoint list without writing any server side code.
This requirement can be achieved by using Sharepoint 2010 ECMA scripts (Sharepoint 2010 Client Object Model).
Please follow the below steps.
Step 0: Create a new site page.
Step 1: Add a content editor webpart.
Step 2: Open the HTML souce for this content editor webpart and add the below code as it is and save it.
// JScript source code
<script type="text/javascript">
//ExecuteOrDelayUntilScriptLoaded(CreateList, "sp.js");
function CreateList()
{
var site;
var context;
var ListName = document.getElementById('listName').value;
var field1 = document.getElementById('field1').value;
var field2 = document.getElementById('field2').value;
var lstDesc = "List Description";
context = SP.ClientContext.get_current();
var site = context.get_web();
var SetNewList = new SP.ListCreationInformation();
SetNewList.set_templateType(SP.ListTemplateType.genericList);
SetNewList.set_title(ListName);
SetNewList.set_description(lstDesc);
//Add the list name in QuickLaunch
SetNewList.set_quickLaunchOption(SP.QuickLaunchOptions.on);
var NewList = site.get_lists().add(SetNewList);
// Add fields to the newly created list.
var NewField1 = NewList.get_fields().addFieldAsXml(
'<Field DisplayName=\''+ field1 +'\' Type=\'Text\' />', true,
SP.AddFieldOptions.defaultValue);
var NewField2 = NewList.get_fields().addFieldAsXml(
'<Field DisplayName=\''+ field2 +'\' Type=\'Text\' />', true,
SP.AddFieldOptions.defaultValue);
context.load(NewField1);
context.load(NewField2);
context.executeQueryAsync(CreateListSucceeded, CreateListFailed);
}
function CreateListSucceeded() {
alert('List has been created Succesfully.');
}
function CreateListFailed(sender, args) {
alert('List Creation Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
//HTML code
<b>List Name</b>
<input id="listName" width="150px"/>
<br/>
<b>Field0</b>
<input id="field1" width="150px"/>
<b>Field1</b>
<input id="field2" width="150px"/>
<br/>
<img onclick="return CreateList();" alt="Create New List"src="/sites/Test/PublishingImages/Create.PNG"/>
After doing this you will see three textbox and one create button. Fill the list name and the field names to be added and click on Create button.
This will create the new list and add the link in the quick launch menu.
No comments:
Post a Comment