CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Tag Archives: spservices

Three ways to get the id of last created SPListItem

An interesting question came on SharePoint StackExchange: How to retrieve the guid of the last uploaded file. It is only possible in CSOM as far as I know. But if we simplify the task, how to get the id of the last item, the we can compare three ways of getting them: SPServices, jQuery + listdata.svc and CSOM.

SPServices

The most easiest way, but not available in async:

$().SPServices.SPGetLastItemId({	
	listName: "Documents"
});

listdata.svc

Just combine some query strings and try in the browsers address bar:

http://dev/_vti_bin/listdata.svc/Documents()?
         $filter=CreatedById eq 1
         &$orderby=Created desc
         &$top=1&$select=Id

CSOM

Not so easy to get started, but very powerful and you can get much more of information than with other methods.

function getLastItemId() {
    var userId = _spPageContextInfo.userId;
    var caml = "<View><Query><Where>"
        + "<Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" 
        + userId + "</Value></Eq></Where>" 
        + "<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>" 
        + "</Query><RowLimit>1</RowLimit></View>";
    var ctx = SP.ClientContext.get_current()
    var web = ctx.get_web()
    var list = web.get_lists().getByTitle("Documents")
    var query = new SP.CamlQuery();
    query.set_viewXml(caml);
    var items = list.getItems(query);
    ctx.load(items)
    ctx.executeQueryAsync(function() {
        // success actions
        var count = items.get_count();
        //should only be 1
        if (count > 1)  {
           throw "Something is wrong. Should only be one latest list item / doc";
        }

        var enumerator = items.getEnumerator();
        enumerator.moveNext();
        var item = enumerator.get_current();
        var id = item.get_id();
        // do something with your result!!!!
        alert(guidString);

    }, function() { 
        //failure handling comes here
        alert("failed"); 
    });
}
getLastItemId();

javascript toolkit on text file

Referencing javascript libraries from Content Delivery Networks (CDN) like jQuery can have many benefits. The self-hosted javascript files are on the other hand give you more control. But in one situation it is much better with cdns: the development. Like Marc D Anderson pointed:

I didn’t realize it at the time, but this txt file has become a standard part of my toolkit already. I’ve added it to three client environments just since last week. It’s the A #1 fastest way to get something up and running, even if you know you’re going to host the files locally later.

This kind of toolkits is really useful in situations where you have to quickly try/test some ideas without uploading multiple files to your server. Here comes my version of this javascript toolkit file:

<!--Add this to the webpart-->
<!--Inspiration got from sympmarc
http://sympmarc.com/2012/04/20/referencing-jquery-jqueryui-and-spservices-from-cdns/?utm_source=rss&utm_medium=rss&utm_campaign=referencing-jquery-jqueryui-and-spservices-from-cdns
and Thorsten Hans:
https://www.nothingbutsharepoint.com/sites/devwiki/articles/Pages/SharePoint-Development-Using-HeadJS-KnockoutJS-And-SPServices.aspx
-->

<!-- Reference the jQueryUI theme's stylesheet on the Google CDN. Here we're using the "Start" theme -->
<link  type="text/css" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css" />

<!-- Reference head.js on cdnjs (Cloudflare)-->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.96/head.min.js"></script>

<!-- Load all other scripts async and run sync: -->
<script type="text/javascript">
	head.js("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js",
		"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js", 
		"http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js",
		"http://cdnjs.cloudflare.com/ajax/libs/knockout/2.0.0/knockout-min.js",
		"http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js",
		"my.js" );
</script>

In my file I even use head.js for loading multiple javascript files at the same time, like Thorsten Hans described in his blog post.

While using head.js you have to have your javascript code in a separate file and reference it in head.js as the last file. Otherwise your javascript code will be executed before jQuery is loaded and it will break the solution. If you want to have your code in the same file as your markup, you have to provide a callback function which will be run after all javascript files are loaded:

head.js("......", "....", myCallback);
function myCallback() {
  $("#div").text("hej");
}

$().SPServices is best for SOAP

SPServices is a great tool, really nice work, Marc Anderson (@sympmarc). It has been there all the time I have developed for SharePoint. But the fact that you work with XML and you must parse the attributes (!) was the reason why I prefered listdata.svc and Client Object Model, where you get objects in JSON or you have a nice API to get objects and their properties. But there is an area where SPServices are really the best tool: SharePoint Web Services which only understand SOAP like SocialDataService.asmx.

Three ways to get list items

Just a little comparison. We’ll retrieve Titles from Tasks list (Oppgaver in nb-no).
listdata.svc

$.getJSON("/_vti_bin/ListData.svc/Oppgaver()?$select=Tittel")
	.done(function(data){
	  var tasks = data.d.results;
		for(var t in tasks) {
			var title = tasks[t].Tittel;
			console.log(title);
		}
	})
	.fail(function() {
		console.log("error");
	});

Pay attention to the field name: Tittel. I think it is the biggest shortcoming of listdata.svc: the fact that we can’t use the internal (static) field names.
Client Object Model

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var taskList = web.get_lists().getByTitle("Oppgaver");
var query = new SP.CamlQuery("<ViewFields><FieldRef Name='Title' /></ViewFields>");
var tasks = taskList.getItems(query);
ctx.load(tasks);
ctx.executeQueryAsync(function() {
	var listEnumerator = tasks.getEnumerator();
	while(listEnumerator.moveNext()) {
		var task = listEnumerator.get_current();
		var title = task.get_item("Title");
		console.log(title);
	}
}, function() {
	console.log("error");
});

SPServices

$().SPServices({
    operation: "GetListItems",
    async: true,
    listName: "Oppgaver",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            console.log( $(this).attr("ows_Title") );        
        });
    }
});
SPServices and SocialDataService

Let’s try to find the count of comments for an url. To do so we must create a SOAP envelope in javascript, send it and handle the response result:

var strUrl = "http://dev/Sider/Help.aspx";
var request = "<soap:Envelope \
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' \
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' \
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
    <soap:Body>    \
      <CountCommentsOnUrl \
          xmlns='http://microsoft.com/webservices/SharePointPortalServer/SocialDataService'> \
        <url>" + strUrl + "</url> \
      </CountCommentsOnUrl> \
     </soap:Body> \
  </soap:Envelope>";

jQuery.ajax({
  type: "POST",
  async: true,
  url: '/_vti_bin/SocialDataService.asmx?op=CountCommentsOnUrl',
  data: request,
  contentType: "text/xml; charset=utf-8",
  dataType: "xml",
  success: function (content, txtFunc, xhr) {
    var count = jQuery(content).find('CountCommentsOnUrlResult').text();
    console.log(count);
  }
});

It works but, we have to handle the creating of SOAP envelope in our javascript code which can be error prone. SPServices provides a nice abstraction layer and handles many scenarios. The same operation in SPServices:

$().SPServices({
  operation: "CountCommentsOnUrl",
  async: true,
  url:"http://dev/Sider/Help.aspx",
  completefunc: function (xData, Status) {
    var result = $(xData.responseXML).find("CountCommentsOnUrlResult")[0];
    console.log($(result).text());
  }
});

In both cases we have to iterate through responseXML. This is a sample responseXML for CountCommentsOnUrl from SocialDataService.asmx:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
      <CountCommentsOnUrlResponse
          xmlns="http://microsoft.com/webservices/SharePointPortalServer/SocialDataService">
      <CountCommentsOnUrlResult>1</CountCommentsOnUrlResult>
    </CountCommentsOnUrlResponse>
  </soap:Body>
</soap:Envelope>
Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Coder, Speaker, Author

Office 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Cameron Dwyer

Office 365, SharePoint, Azure, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

DevOps, Cloud and Blockchain Consultant

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

SharePointDiver

SharePoint på ren svenska