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


Top 5 Contributors of the Month
david stephan

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.
 

Introduction

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.load(newList);
        context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
}
//Async Success/ Failure

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

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

</script>
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),
                        SP.FieldNumber);
f1.set_title("EmpID");
f1.update();

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

//For Multiline text

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

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.

Summary

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

 Subscribe to Articles

     

Further Readings:

Responses

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