Tuesday, July 17, 2012

Create Folder in library using ECMA client object model

Here is a simple way to create folder inside library using ECMA client object model



<script type="text/javascript">
var clientcontext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(AddFolder, “sp.js”);
function AddFolder()
{
clientcontext = new SP.ClientContext.get_current();
web = clientcontext.get_web();
this.list = web.get_lists().getByTitle("LibraryName");
var listItemCreationInfo = new SP.ListItemCreationInformation();
listItemCreationInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
listItemCreationInfo.set_leafName('NameofTheFolder');
var newItem = list.addItem(listItemCreationInfo);
newItem.update();
clientcontext.load(web);
clientcontext.load(list);
clientcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));
}
function ExecuteOnSuccess(sender, args) {
SP.UI.Notify.addNotification('Folder has been successfully created', false);
}
function ExecuteOnFailure(sender, args) {
alert("Some error");
}
</script>

No comments:

Post a Comment