http://spservices.codeplex.com/
Just really appreciated what has been done and how useful these WebServices JQuery libraries have been. It has opened up a new dimension in interacting with SharePoint sites since their conception. Many of us have been quietly using it for our needs and in many ways allowed some of us who are in more locked down server environments to achieve things that otherwise would require .NET Components or InfoPath. Both great tools too.
Anyway, I thought I will package up a simple example for reference how these libraries can be easily used.
Imagine the following HTML page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple SPS List Management Code</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style>
body
{
margin-left:1px;
}
</style>
<script type="text/javascript" src="/myServer/IS/mn/Files%20To%20Share/jquery-1.10.2.js"></script>
<script type="text/javascript" src="/myServer/IS/mn/Files%20To%20Share/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript" src="/myServer/IS/mn/Files%20To%20Share/jquery.SPServices-2014.02.js"></script>
<script type="text/javascript">
function addItem()
{
var strSubsiteName = document.getElementById("txtSubsiteName").value
var columnsForInsert = [];
columnsForInsert[0] = ["Title", document.getElementById("txtTitle").value];
columnsForInsert[1] = ["Country", document.getElementById("txtCountry").value];
columnsForInsert[2] = ["Code", document.getElementById("txtCode").value];
// Create New List Item
$(document).ready(function() {
$().SPServices({
operation: "UpdateListItems",
webURL: "/myServer/IS/"+strSubsiteName,
async: true,
batchCmd: "New",
listName: "Countries",
valuepairs: columnsForInsert,
completefunc: function (xData, Status) {
alert("List Item successfully created.");
}
}); // End of SPServices UpdateListItems
});
}
function updateItem()
{
var strSubsiteName = document.getElementById("txtSubsiteName").value
var nDeleteID = document.getElementById("txtID").value;
if (parseInt(nDeleteID, 10))
{
alert("Item with ID of " + nDeleteID + " will be updated.");
}
else
{
alert("Please enter a valid ID to update list data.");
return;
}
var columnsForUpdate = [];
columnsForUpdate [0] = ["Title", document.getElementById("txtTitle").value];
columnsForUpdate [1] = ["Country", document.getElementById("txtCountry").value];
columnsForUpdate [2] = ["Code", document.getElementById("txtCode").value];
// Update List Item
$(document).ready(function() {
$().SPServices({
operation: "UpdateListItems",
webURL: "/myServer/IS/"+strSubsiteName,
async: true,
batchCmd: "Update",
listName: "Countries",
ID: nDeleteID,
valuepairs: columnsForUpdate,
completefunc: function (xData, Status) {
alert("List Item has been updated");
}
});
});
}
function deleteItem()
{
var strSubsiteName = document.getElementById("txtSubsiteName").value
var nDeleteID = document.getElementById("txtID").value;
if (parseInt(nDeleteID, 10))
{
alert("Item with ID of " + nDeleteID + " will be deleted.");
}
else
{
alert("Please enter a valid ID to delete from list.");
return;
}
// Delete List Item
$(document).ready(function() {
$().SPServices({
operation: "UpdateListItems",
webURL: "/myServer/IS/"+strSubsiteName,
async: true,
batchCmd: "Delete",
listName: "Countries",
ID: nDeleteID,
completefunc: function (xData, Status) {
alert("List Item deleted");
}
});
});
}
</script>
</head>
<BODY>
<div id="StatusMessages" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px; margin-left:0px; font-family:Verdana; font-size:small">
<input type="button" onclick="addItem()" value="Add Item" style="font-family:inherit;font-size:small" />
<input type="button" onclick="updateItem()" value="Update Item" />
<input type="button" onclick="deleteItem()" value="Delete Item" /><br><br>
ID: <input type="text" id="txtID" name="txtID"/> Title: <input type="text" id="txtTitle" name="txtTitle"/> Country: <input type="text" id="txtCountry" name="txtCountry"/> Code: <input type="text" id="txtCode" name="txtCode"/> Subsite URL Name: <input type="text" id="txtSubsiteName" name="txtSubsiteName"/>
</div>
<script type="text/javascript">
</script>
</BODY></html>
It turns our these SPS Services calls can work with lists, discussion boards and document libraries. The Update commands seems to be quite generic. The Add won't work easily with Document libraries as it does require an upload of a file. The delete can work but requires an extra highlighted parameter (FileRef) URL to the document in the library. See this http://joshmccarty.com/2012/02/deleting-documents-with-spservices/
Anyway adapting the DeleteFile() function like this below certainly works :
function deleteFile()
{
var nDeleteID = document.getElementById("txtID").value;
var strFileRef = document.getElementById("txtFileRef").value;
if (strFileRef == "")
{
alert("Must enter a ref URL to the file in the library that you wish to delete. Eg : http://myserver/document/SPS/mydoc.docx");
return;
}
if (parseInt(nDeleteID, 10))
{
alert("Item with ID of " + nDeleteID + " will be deleted.");
}
else
{
alert("Please enter a valid ID to delete from list.");
return;
}
// This is the command needed to delete the specified file. It uses the ID and the URL of the file name. These values must be passed into this function when calling it.
var batchCmd = "<Batch OnError='Continue'><Method ID='1' Cmd='Delete'><Field Name='ID'>" + nDeleteID + "</Field><Field Name='FileRef'>" + strFileRef + "</Field></Method></Batch>";
// Use SPServices to delete the file.
$().SPServices({
operation: "UpdateListItems",
async: false,
listName: "SPS",
updates: batchCmd,
completefunc: function ( xData, Status ) {
// Check the error codes for the web service call.
$( xData.responseXML ).SPFilterNode( 'ErrorCode' ).each( function(){
responseError = $( this ).text();
// If the error codes indicate that the file was successfully deleted, inform the user.
if ( responseError === '0x00000000' ) {
alert( "The file has been successfully deleted." );
}
// If the error codes indicate that the file was NOT successfully deleted, inform the user.
else {
alert( "There was an error trying to delete the file." );
}
});
}
});
}
No comments:
Post a Comment