I am newbie to SharePoint and I am uploading a file to the server using javascript. Now the task is to get the GUID of that file and display it ({alert(guid); is fine).

I don't even know where to start. Any help would be really appreciated.

Thanks a lot

I can get the url of the file but I need to display uniqueID/GUID of that file(Item) into RTE.


Here is an example using CSOM in JavaScript.

    var context = new SP.ClientContext.get_current();
    var listTitle = "Name of List";
    var viewTitle = "Name of View"; //You can compose a CAML or use a view.
        list = context.get_web().get_lists().getByTitle(listTitle); 
        view = list.get_views().getByTitle(viewTitle); 

        function(sender, args) {getLinkListItem("<View><Query>" + view.get_viewQuery() + "</Query></View>")}, 
        function(sender, args) {alert("error: " + args.get_message());});}
    }, 'sp.js')});

 function getLinkListItem(camlQuery){
      var context = SP.ClientContext.get_current();
      var approvalList = context.get_web().get_lists().getByTitle(listTitle);

      var query = new SP.CamlQuery(); 

      listItems = approvalList.getItems(query);

      context.executeQueryAsync(ReadListItemSucceeded, ReadListItemFailed);
//Execute this when you receive the data back from SharePoint.
function ReadListItemSucceeded(sender, args) {
  var enumerator = listItems.getEnumerator();
  while (enumerator.moveNext()) {
    var listItem = enumerator.get_current();
    alert('The ID for ' +listItem.Title + ' is ' + listItem.id);
//Execute this function if things go wrong.
function ReadListItemFailed(sender, args) {
  alert('Request failed.');

You should also take a look at the MSDN library entry for the JavaScript/ECMA CSOM.

Here is an example using a CAML query rather than a SharePoint view.


I'm not so familiar with java client object model for SharePoint, but I have worked with this ajax java script library (SPServices), this is quite useful, works with SharePoint web services and based on jQuery. It allows to query items and so on by calling functions like this:

    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
  • I cannot write "<" or ">" anywhere in the file and hence my caml query does not work... – Thisisme.. Dec 4 '11 at 17:15
  • escape the characters... < = &lt; > = &gt; – Josey Dec 6 '11 at 20:52
  • Hi There. Even though your solution might work, MS recommends using the Sharepoint Client Object Model. Have a look at this sample which should work just fine for what you're trying to do. blogs.msdn.com/b/sridhara/archive/2010/03/12/… – Fox Dec 7 '11 at 15:00
  • This link points to the post about using client object model in C#. I agree client object model is good, but we talk about JavaScript, this is not correct link. Also I dont see any problem in using SharePoint services, it is also provided by Microsoft, this is just wrapper around it. What to use is your choice. – Anton Khritonenkov Dec 10 '11 at 8:50
  • Thisisme, do you write multiline CAML query, you need to write it in single line, or check this article electrictoolbox.com/javascript-multi-line-strings. It provides guide, how to write multiline strings in JavaScript – Anton Khritonenkov Dec 10 '11 at 9:04

I am not absolutely clear what exactly you are trying to do. But if you upload files to document library (not as attachments to other list items), you could just retrieve the guid for the latest uploaded file for the current user. Is it what you are doing? Robert Kaucher gave some examples what you could do wit CSOM. Here is a working solution which I tested on my computer:

function getLatestDocumentGuid() {
    ///<summary>Gets the GUID for the latest uploaded document
    ///for current user
    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();
    var items = list.getItems(query);
    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();
        var item = enumerator.get_current();
        var guid = item.get_item("GUID");
        var guidString = guid.toString();
        // do something with your result!!!!

    }, function() { 
        //failure handling comes here
//run the function

The CAML for the latest document/item comes from Rich Bennema's answer at SO. Using CAML in js CSOM comes from Rober Kaucher and MSDN. Getting the current user id in js comes from my blog

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.