Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

Create Folder in list/library using Ecmascript/Javascript client object model

12/28/2011  
Category: SharePoint 2010


In response to the Forum post Here I have written a quick example of how to add a folder in a list using Ecmascript/Javascript client object model in SharePoint 2010.
You can add this script in a CEWP for testing purposes. In the below script Replace “ListName” with your list or library name and “NewFolder” with whatever you want to call your folder.

<script type=”text/javascript”>

var currentcontext = null;

var currentweb = null;

ExecuteOrDelayUntilScriptLoaded(AddFolder, “sp.js”);

function AddFolder()
{

currentcontext = new SP.ClientContext.get_current();

currentweb = currentcontext.get_web();

this.list = currentweb.get_lists().getByTitle(“ListName“);

var listItemCreationInfo = new SP.ListItemCreationInformation();

listItemCreationInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);

listItemCreationInfo.set_leafName(‘NewFolder‘);

var newItem = list.addItem(listItemCreationInfo);

newItem.update();

currentcontext.load(currentweb);

currentcontext.load(list);

currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

}

function ExecuteOnSuccess(sender, args) {
SP.UI.Notify.addNotification(‘Folder created successfully’, false);
}

function ExecuteOnFailure(sender, args) {
alert(“Error in Script”);
}
</script>

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

Sharepoint 2010 – Create custom modal dialog using Visual studio 2010
Sharepoint 2010 – Create»

Detailed steps to create a custom modal dialog page and show it when a ribbon button is clicked in a»

Display modal dialog using Object model SharePoint 2010
Display modal dialog using»

In this post we will see a short example of how to invoke a modal dialog from a server side»

.Net 4 with SharePoint Foundation 2010
.Net 4 with SharePoint»

Since this has been asked a million times in various forums. Here is the Final word from Michael Foertsch (secretsofsharepoint.com/cs).»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012