Thursday, December 29, 2011

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

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>

No comments:

Post a Comment