.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

List and List Fields creation using Client Object Model

Posted By:Nagarajan       Posted Date: December 21, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

This article will show you how to create a list and fields using Client Object Model.


I am going to share my experience about creating a list and list fields using Client Object Model.

Creating List

Let we see how to create a list at first. Add the below javascript for creating list.

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web();

<script type="text/javascript">
// Creating List
function createlist() {
        var listCreationInfo = new SP.ListCreationInformation();
        listCreationInfo.set_title('Your List Name'); // List Name
        listCreationInfo.set_templateType(SP.ListTemplateType.genericList); // List Type
        lists = web.get_lists();
        var newList = lists.add(listCreationInfo);
        context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
//Async Success/ Failure

 function onListCreationSuccess() {
alert('List is created successfully');

 function onListCreationFail(sender, args) {
  alert('Failed to create the list. ' + args.get_message());}

I have called the AddFields() in onListCreationSuccess() which used to create fields after creating a list.

Adding Fields

Then we will see how to add fields for alist
function AddFields()

var oList = context.get_web().get_lists().getByTitle('Your List Name');

var  fldCollection  = oList.get_fields();
// For Number type Column
var f1 = context.castTo(fldCollection.addFieldAsXml('<Field Type="Number" DisplayName="EmpID" Name="EmpID" />', true, SP.AddFieldOptions.addToDefaultContentType),

//For text type column
var f2 = context.castTo(fldCollection.addFieldAsXml('<Field Type="Text" DisplayName="Name" Name="Name" />', true, SP.AddFieldOptions.addToDefaultContentType),

//For Multiline text

var f3 = context.castTo(fldCollectionDC.addFieldAsXml('<Field Type="Note"  DisplayName="Comment" Name="Comment" />', true, SP.AddFieldOptions.addToDefaultContentType),

context.executeQueryAsync(onQuerySucceeded, onQueryFailed);

function onQuerySucceeded() {
alert("List Field Added");}

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

This is it. List and fields has been added on your SharePoint site.


I have gained my knowledge in Client Object Model. Hope you guys also felt the same :)

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend