In my previous article,
How to access list of folder name from SharePoint List or Document Library using SPServices jQuery library in SharePoint, I already explained about
$().SPServices.GetListItems operation to access the listing of folder.
Take an example of one requirement: In SharePoint online site, we need to create the folder in SharePoint List or Document library when user click on button.
To implement above requirement, we can not use the SharePoint Server Side code, so we need to utilize the Client Side Scripting – JSOM/Call to SharePoint REST API using JQuery/Use of
SPServices JQuery Library.
Now, I am going to explain how to create/add folder in SharePoint List or Document Library using SPServices jQuery library in SharePoint. SPServices JavaScript Library ($().SPServices) provides function called UpdateListItems to update content in the SharePoint list.
The $().SPServices.UpdateListItems function details are:
[pre class="brush:jscript" ]
$().SPServices({
operation: "UpdateListItems",
async: {true/false},
listName: {List Name},
batchCmd: {"New" or "Update"},
updates: {caml Query},
valuepairs: {key/value pair of columns},
ID: {Id of the row, when update record},
webURL: {web URL}
});
[/pre]
- operation: name of the $().SPServices operation is "UpdateListItems "
- async: you can pass true or false
- listName: Name of the list from which data needs to be fetched
- batchCmd: We can set value as "New" or "Update".
- updates: It is an optional parameter and used to define caml Query. If you specify valuepairs then updates should not be specified.
- valuepairs: It is an optional parameter and used to define Key/Value pair. If you specify updates then valuepairs should not be specified.
- ID: It’s an optional parameter and needs to be used with valuepairs when we are using the batchCmd as "Update".
- webURL: This allows you to change the context for the operation to a different site
Note that if you decide to use the valuepairs approach, you also need to specify the ID option. The default for the batchCmd option is 'Update'. Also, the choices for Cmd are [New, Update, Delete, Moderate].
Now, Create folder inside the SharePoint Document Library or List using
$().SPServices.UpdateListItems operation with CAML query:
[pre class="brush:jscript" ]
var folderName= 'Amit_Kumar';
var camlQuery = '
' +
'' +
'1' +
'' + folderName + '' +
'' + folderName + '' +
'' +
'';
$().SPServices({
operation: "UpdateListItems",
async: false,
listName: "documents",
updates: camlQuery ,
completefunc: function(xData, Status) {
//--You can perform any action, when create request completed
}
});
[/pre]
In the above code block, we are not using batchCmd/valuepairs/ID attributes of "UpdateListItems" operation, because CAML query contains information about command type and all required details.
Comments