Hi All,
In a following
post we will see how to create Folder inside Document Library using ECMA Script,
following code has been written for SP Hosted Apps.
In My Page I
have input Controls as follows, On clicking button I am crating folder with the
name specified in Text Box.
<div>
<p id="message">
<!-- The following content will be replaced with the
user name when you run the app - see App.js -->
initializing...
<input type="text" id="txtFolder"></input>
<br></br>
<input type="button"
id="btnCreateFolder"
value="CrateFolder"></input>
</p>
</div>
Below is the
code in App.js
'use
strict';
var
SPAppWebUrl;
var
spHostUrl;
(function () {
// This code
runs when the DOM is ready and creates a context object which is
// needed to
use the SharePoint object model
$(document).ready(function
() {
spHostUrl =
decodeURIComponent(getQueryStringParameter('SPHostUrl'));
SPAppWebUrl =
decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
$('#btnCreateFolder').on('click', function
() {
CreateFolder($('#txtFolder').val());
});
});
function
CreateFolder(folderName) {
var
context = SP.ClientContext.get_current();
var
parentContext = new SP.AppContextSite(context,
spHostUrl);
var web
= parentContext.get_web();
var
list = web.get_lists().getByTitle("MyDocLib");
var
listItemCreationInfo = new
SP.ListItemCreationInformation();
listItemCreationInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
listItemCreationInfo.set_leafName(folderName);
var
newItem = list.addItem(listItemCreationInfo);
newItem.update();
context.load(web);
context.load(list);
context.executeQueryAsync(ExecuteOnSuccess, ExecuteOnFailure);
}
function
ExecuteOnSuccess() {
alert('folder
created successfully');
}
function
ExecuteOnFailure(sender, args) {
alert('error'
+ args.get_message());
}
function
getQueryStringParameter(urlParameterKey) {
var params
= document.URL.split('?')[1].split('&');
var
strParams = '';
for (var i = 0; i < params.length; i = i + 1) {
var
singleParam = params[i].split('=');
if
(singleParam[0] == urlParameterKey)
return
decodeURIComponent(singleParam[1]);
}
}
})();
Thanks.
0 comments:
Post a Comment