.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 >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Creating Columns in SharePoint using Client Object Model

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

This code is for creating columns of SharePoint List using Client Object Model.
 

First we need to create a list / we already having a list in SharePoint site. I am going to give a example for creating a list and their fields too.

Go through the link for creating SharePoint List:

We need to call the field creation method on Async Success method.

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

<script type = "text/javascript">
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 Updated");}

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

This is it. You may get these fields on your SharePoint List. Happy Coding :)


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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