Last active
May 2, 2024 14:14
-
-
Save skokasik/7069336 to your computer and use it in GitHub Desktop.
Javascript: SPServices (add, delete, update)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SharePointFunctions = function(rootSP) { | |
this.spSiteUrl = rootSP; | |
}; | |
SharePointFunctions.prototype = { | |
retrieveListPromise : function(spListName, viewCaml, filterCaml, asyncStatus) { | |
var spPromise; | |
if (viewCaml !== undefined) | |
{ | |
spPromise = $().SPServices({ | |
operation : "GetListItems", | |
webURL : this.spSiteUrl, | |
listName : spListName, | |
CAMLViewFields : viewCaml, | |
CAMLQuery : filterCaml, | |
async: asyncStatus | |
}); // End SPServices | |
} | |
else | |
{ | |
spPromise = $().SPServices({ | |
operation : "GetListItems", | |
webURL : this.spSiteUrl, | |
listName : spListName, | |
CAMLQuery : filterCaml, | |
async: asyncStatus | |
}); // End SPServices | |
} | |
return spPromise; | |
}, | |
retrieveRowLimitListPromise : function(spListName, viewCaml, filterCaml, asyncStatus, rowLimit) { | |
var spPromise; | |
if (viewCaml !== undefined) | |
{ | |
spPromise = $().SPServices({ | |
operation : "GetListItems", | |
webURL : this.spSiteUrl, | |
listName : spListName, | |
CAMLViewFields : viewCaml, | |
CAMLQuery : filterCaml, | |
CAMLRowLimit : rowLimit, | |
async: asyncStatus | |
}); // End SPServices | |
} | |
else | |
{ | |
spPromise = $().SPServices({ | |
operation : "GetListItems", | |
webURL : this.spSiteUrl, | |
listName : spListName, | |
CAMLQuery : filterCaml, | |
CAMLRowLimit : rowLimit, | |
async: asyncStatus | |
}); // End SPServices | |
} | |
return spPromise; | |
}, | |
addNewToListPromise : function(spListName, valuePairs){ | |
var spPromise = $().SPServices({ | |
operation: "UpdateListItems", | |
batchCmd: "New", | |
async: true, | |
webURL: this.spSiteUrl, | |
valuepairs: valuePairs, | |
listName: spListName | |
}); //End SPServices | |
return spPromise; | |
}, | |
updateListItemPromise : function (spListName, valuePairs, id){ | |
var spPromise = $().SPServices({ | |
operation :"UpdateListItems", | |
async: true, | |
webURL: this.spSiteUrl, | |
valuepairs:valuePairs, | |
listName: spListName, | |
ID:id | |
}); //End SPServices | |
return spPromise; | |
}, | |
deleteListItem : function(spListName, idArray){ | |
if(idArray.length < 1){ | |
return false; | |
} | |
var batch = "<Batch OnError='Continue'>"; | |
for(var i=0;i<idArray.length;i++){ | |
batch += "<Method ID='" + (i+1) + "' Cmd='Delete'><Field Name='ID'>" + idArray[i] + "</Field></Method>"; | |
} | |
batch += "</Batch>"; | |
var spPromise = $().SPServices({ | |
operation: "UpdateListItems", | |
async: true, | |
webURL: this.spSiteUrl, | |
listName: spListName, | |
updates: batch | |
}); //End SPServices | |
return spPromise; | |
}, | |
addMultipleItemsToListPromise : function (spListName, objectArray){ | |
var batch = "<Batch OnError='Continue'>"; | |
for(var i=0;i<objectArray.length;i++) | |
{ | |
batch += "<Method ID='" + (i + 1) + "' Cmd='" + "New" + "'>"; | |
for( j=0;j<objectArray[i].length-1;j++) | |
{ | |
if (objectArray[i][j+1] !== undefined){ | |
batch += " <Field Name='" + objectArray[i][j] +"'>" + objectArray[i][j+1] + "</Field>"; | |
} | |
j++; | |
} | |
batch += "</Method>"; | |
} | |
batch += "</Batch>"; | |
var spPromise = $().SPServices({ | |
operation: "UpdateListItems", | |
async: true, | |
webURL: this.spSiteUrl, | |
updates: batch, | |
listName: spListName | |
}); | |
return spPromise; | |
}, | |
updateMultipleItemsToListPromise : function (spListName, objectArray, cmdState){ | |
var batch = "<Batch OnError='Continue'>"; | |
for(var i=0;i<objectArray.length;i++) | |
{ | |
batch += "<Method ID='" + (i + 1) + "' Cmd='" + cmdState[i] + "'>"; | |
for( j=0;j<objectArray[i].length-1;j++) | |
{ | |
if (objectArray[i][j+1] !== undefined){ | |
batch += " <Field Name='" + objectArray[i][j] +"'>" + objectArray[i][j+1] + "</Field>"; | |
} | |
j++; | |
} | |
batch += "</Method>"; | |
} | |
batch += "</Batch>"; | |
var spPromise = $().SPServices({ | |
operation: "UpdateListItems", | |
async: true, | |
webURL: this.spSiteUrl, | |
updates: batch, | |
listName: spListName | |
}); | |
return spPromise; | |
}, | |
//Deletes all items from a list | |
purgeList : function(spListName){ | |
var highID=0; | |
var lowID=0; | |
//get max ID | |
var spMaxIdPromise = $().SPServices({ | |
operation: "GetListItems", | |
webURL : this.spSiteUrl, | |
listName: spListName, | |
CAMLQuery:"<Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy></Query>", | |
CAMLRowLimit: 1, | |
async: false, | |
completefunc: function (xData, Status) { | |
$(xData.responseXML).SPFilterNode("z:row").each(function() { | |
highID=parseInt($(this).attr("ows_ID")); | |
}); | |
} | |
}); | |
//get lowest ID | |
$().SPServices({ | |
operation: "GetListItems", | |
webURL : this.spSiteUrl, | |
listName: spListName, | |
CAMLQuery:"<Query><OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy></Query>", | |
CAMLRowLimit: 1, | |
async: false, | |
completefunc: function (xData, Status) { | |
$(xData.responseXML).SPFilterNode("z:row").each(function() { | |
lowID=parseInt($(this).attr("ows_ID")); | |
}); | |
} | |
}); | |
if (highID-lowID>100){ //delete in batches of 100 if more than 100 | |
var i=lowID; | |
var j=lowID+100; | |
while (j < highID){ | |
$().SPServices.SPUpdateMultipleListItems({ | |
listName: spListName, | |
webURL : this.spSiteUrl, | |
CAMLQuery: "<Query><Where><Leq><FieldRef Name='ID'/><Value Type='Integer'>"+j+"</Value></Leq></Where></Query>", | |
batchCmd: "Delete", | |
debug: false | |
}); | |
i=i+100; | |
j=j+100; | |
} | |
var lower=highID-100; | |
$().SPServices.SPUpdateMultipleListItems({ | |
listName: spListName, | |
webURL : this.spSiteUrl, | |
CAMLQuery: "<Query><Where><Leq><FieldRef Name='ID'/><Value Type='Integer'>"+highID+"</Value></Leq></Where></Query>", | |
batchCmd: "Delete", | |
debug: false, | |
completefunc: function (xData, Status) { | |
return true; | |
} | |
}); | |
} | |
else { | |
$().SPServices.SPUpdateMultipleListItems({ | |
listName: spListName, | |
webURL : this.spSiteUrl, | |
CAMLQuery: "<Query><Where><Leq><FieldRef Name='ID'/><Value Type='Integer'>"+highID+"</Value></Leq></Where></Query>", | |
batchCmd: "Delete", | |
debug: false, | |
completefunc: function (xData, Status) { | |
return true; | |
} | |
}); | |
} | |
} //end purgeList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excellent JS ??? , it is very helpful ,
I'm from Mexico and I wondered if by chance you had some examples of its use .