I have a requirement to add folders to library from client side .The requirement is like when a file will upload to a library it will check for the folder structure like current year->current Month->current Date .If folders are not there folder should be created and file will be upload to the current date folder.So here I am uploading the iterative folder creation with a structure of   lib name->2016->6->13062016  .

This is basically a rough code not managed well.

 

var date = new Date();
var month = date.getMonth() + 1;
var year = date.getFullYear();
//var dateF = date.format(‘d-M-Y’);dd-MM-yy
var dateF = date.format(‘dd-MM-yyyy’);
var dateURL = “”;
var list = [];
var flag;

$(document).ready(function () {

$(“#btnClick”).click(function () {

var folderURL = “test”;
var numbers = { “year”: year, “month”: month, “date”: dateF, “listarray”: “” };
var folderDisplyURL=””;
$.each(numbers, function (index, value) {

if (index == “year”) {

folderURL1 = publicMethod.GetFolder(year, folderURL, “year”);

}
else if (index == “month”) {

folderURL1 = publicMethod.GetFolder(month, folderURL + “/” + year, “month”);

}
else if (index == “date”) {

folderURL1 = publicMethod.GetFolder(dateF, folderURL + “/” + year + “/” + month, “date”);

}
else if (index == “listarray”) {
$.each(list, function (index, value) {

var pCreate = $.ajax({
“url”: _spPageContextInfo.webAbsoluteUrl + “/_api/Web/Folders/add(‘” + value + “‘)”,
“type”: “POST”,
async: false,
“headers”: {
“accept”: “application/json; odata=verbose”,
“content-type”: “application/json; odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()
}
});

$.when(pCreate).always(function (data) {

try {
flag = “created”;
folderDisplyURL = _spPageContextInfo.webAbsoluteUrl + data.d.ServerRelativeUrl;
}
catch (err) {

}

});

});
}

if (folderDisplyURL != “”) {

if (flag == “created”) {
alert(“Folder created URL :” + folderDisplyURL);
}
}
});

});
});

var publicMethod =
{
GetFolder: function (folderName, folderURL, flag) {

var folderURL2 = “”;

var requestUri = _spPageContextInfo.webServerRelativeUrl + “/_api/Web/GetFolderByServerRelativeUrl(‘” + folderURL + “‘)/Folders?$filter=Name eq ‘” + folderName + “‘”;
)
var returnURL = “”;
$.ajax(
{
url: requestUri,
type: “GET”,

data: JSON.stringify({ ‘__metadata’: { ‘type’: ‘SP.Folder’ }, ‘ServerRelativeUrl’: folderURL }),
headers: {
“accept”: “application/json;odata=verbose”
},
success: onSuccess,
error: onError,
async: false
}
);

function onSuccess(data) {

if (data.d.results.length == 0) {
list.push(folderURL+”/”+folderName);

}
}

function onError(error) {

}
return folderURL2;

},

getFormDigest: function (webUrl) {
return $.ajax({
url: webUrl + “/_api/contextinfo”,
method: “POST”,
headers: { “Accept”: “application/json; odata=verbose” }
});
},
CreatF: function (folderURL, folderName)
{
var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/folders”;
$.ajax({
url: requestUri,
type: “POST”,
data: JSON.stringify({ ‘__metadata’: { ‘type’: ‘SP.Folder’ }, ‘ServerRelativeUrl’: folderURL + “/” + folderName }),
headers: {
“accept”: “application/json;odata=verbose”,
“content-type”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#_REQUESTDIGEST”).val()
},
success: onSuccess1,
error: onError1
});

function onSuccess1(data) {
debugger;
alert(folderName+’Folder created successfully’);
}

function onError1(error) {
debugger;
alert(JSON.stringify(error));
}
},
CreateFolder: function (folderURL) {

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/folders”;
return publicMethod.getFormDigest(_spPageContextInfo.webAbsoluteUrl).then(function (data) {
$.ajax(
{
url: requestUri,
type: “POST”,
async: false,
data: JSON.stringify({ ‘__metadata’: { ‘type’: ‘SP.Folder’ }, ‘ServerRelativeUrl’: folderURL }),
headers: {
“accept”: “application/json;odata=verbose”,
“content-type”: “application/json;odata=verbose”,
//”X-RequestDigest”: $(“#_REQUESTDIGEST”).val()
“X-RequestDigest”: data.d.GetContextWebInformation.FormDigestValue
},
success: onSuccess1,
error: onError1

}
);
function onSuccess1(data) {
debugger;
alert(folderURL + ‘ Folder created successfully’);

}

function onError1(error) {
debugger;

alert(JSON.stringify(error));
}
});
return folderName;

},
}

Advertisements