Showing posts with label SPServices. Show all posts
Showing posts with label SPServices. Show all posts

Saturday, February 7, 2015

Batch update list items using client scripts

Share/Save/Bookmark
We know that SPServices has a way to create new or update items but when in the case of creating multiple items(say 10), calling the same query 10 times is not going to be good from performance perspective. so lets use batch command to do it from client side as below

var query_string = "";
for(int i=1;i<=5;i++)
{
var title = $('input#title'+i).val();
var desc = $('input#desc'+i).val();
query_string += "<Method ID='1' Cmd='New'>";
query_string += "<Field Name='Title'>"+title+"</Field>";
query_string += "<Field Name='Description'>"+desc+"</Field>";
query_string += "</Method>";

}
query_string += "
";

$().SPServices({
    operation: "UpdateListItems",
    async: false,
    batchCmd="New",
    listName: "Requests",
    updates: query_string,
    completefunc: function(xData, Status) {
        alert(xData.responseText);
    }
});

for update operations, batchCmd should be updated as 'Update' and same in the query string as well.


Subscribe

Thursday, June 12, 2014

Check in file using Client script


Share/Save/Bookmark

Here is a simple way of doing few file operations in sharepoint using client script. You can try JQuery for client object model or CSOM or JQuery with SPServices(in my case).

function DiscardCheckOut(url)
{

    var errorMessage = "";

    $().SPServices({
    operation: "UndoCheckOut",
    async: false,
    pageUrl: url,
    completefunc: function (xData, Status) {
         $(xData.responseXML).find("errorstring").each(function() {
         errorMessage = $(this).text();
         });

        $(xData.responseXML).find("faultstring").each(function() {
               errorMessage += $(this).text();
         });

        var undoCheckOutVal= $(xData.responseXML).find("UndoCheckOutResult").text();
        if(undoCheckOutVal == "true")
             alert("Operation success");
        else
            alert("Operation failed:"+errorMessage);
     }
});

}

function CheckIn(furl)
{

   var errorMessage = "";

   $().SPServices({
   operation: "CheckInFile",
   async: false,
   pageUrl: furl,
   comment': "Checked in from checkout list",
   CheckinType: 1,
   completefunc: function (xData, Status) {
       //get errorstring
        $(xData.responseXML).find("errorstring").each(function() {
             message = $(this).text();
        });

       //get fault string
       $(xData.responseXML).find("faultstring").each(function() {
            message +=" "+ $(this).text();
       });

      var checkInResultVal= $(xData.responseXML).find("CheckInFileResult").text();
      if(checkInResultVal == "true")
            alert("Check-in operation success");
      else
            alert("Operation failed:"+errorMessage);
   }
   });
}



Subscribe

Friday, May 23, 2014

Autocomplete using SPServices with JQuery

Share/Save/Bookmark

Call it auto suggestion or type ahead or auto complete, its pretty similar that im trying to achieve here in SharePoint using SPServices API with JQuery.
Get your copy of SPServices js from http://spservices.codeplex.com/ and have it in a library or layouts folder so it can be referenced in the script that we gonna write.

Here, im implementing a sharepoint site page that has a custom filter option that will allow user to filter the data in mulitple listviewwebpart added to the same page.Sometime names may sound same but spell different, so i want to give auto suggestions when user starts typing the text in filter textbox.

$(document).ready(function(){
 $().SPServices.SPAutocomplete({
     sourceList:"Employee list",
     sourceColumn: "FullName", // SPField name from which data will be pulled using lists webservice
     columnName:"FilterByName", // either sp field name if using on list form or input field control title attribute
     ignoreCase:true,
     numChars:3, //autocomplete shows when there is a minimum of 3 char in the textbox of the field/control
     slideDownSpeed: 1000,
  });
});

Once value is set in filtertexbox  and submitted i have an additional script that appends the querystring FilterField1=fileldname&FilterValue1=value that will filters the data in multiple list webparts.

SPServices made developers job easy when it comes to writing client side code. Ofcourse, you can achieve the same thing by writing your own code from scratch with a jquery ajax request to sharepoint lists webservice and bind results with textbox.
Im hoping SPServices will introduce a way to find the input control not just by 'Title' but also by name/id because some sharepoint input controls(may not be list forms) will not have title attribute set.


Subscribe