5

Is it possible, using SPServices, to update a specific list item when all I have is the file path?

For example, I have a file located at /site/library/folder/file.txt and I want to update a single line of text column called @Validated to a value of 'Yes'. Can this be done with jQuery & SPServices or am I barking up the wrong tree?

I've found lots of examples with REST and getting the item by ID, but none this way.

P.S. - I need to stick with JavaScript and/or jQuery since this will be the last step in an existing script.

moe
  • 5,267
  • 21
  • 34
Omegacron
  • 1,822
  • 1
  • 19
  • 30

3 Answers3

5

You could perform this via CSOM/REST API.

Use SP.Web.getFileByServerRelativeUrl Method to request File object and specify SP.File.listItemAllFields property to load list item field values for the list item corresponding to the file:

   var ctx = SP.ClientContext.get_current(); 
   var file = ctx.get_web().getFileByServerRelativeUrl(url);   //get file
   ctx.load(file,'ListItemAllFields'); 

   ctx.executeQueryAsync(
      function () {
         var listItem = file.get_listItemAllFields(); //get list item for a file
         //update list item goes here..
      }, 
       function(sender,args){
         //error handling goes here..
       }
   ); 

A complete example

The following example demonstrates how to retrieve list item by its file url and update it

function getFileWithProperties(url,success,error) {

   var ctx = SP.ClientContext.get_current(); 
   var file = ctx.get_web().getFileByServerRelativeUrl(url);   //get file
   ctx.load(file,'ListItemAllFields'); 

   ctx.executeQueryAsync(
      function () {
         success(file);
      }, 
      error
   );
}


function updateListItem(listItem,properties, success,error) 
{
   var ctx = listItem.get_context();
   for(var propName in properties) {
       listItem.set_item(propName, properties[propName]) 
   }
   listItem.update();
   ctx.executeQueryAsync(
       function () {
         success();
       }, 
       error
   );
}

getFileWithProperties('/project/Shared Documents/Order.docx',
  function(file){
     var listItem = file.get_listItemAllFields(); //get list item for a file
     var itemProperties = {'Title': 'New Order'};
     //update list item
     updateListItem(listItem, itemProperties,
       function () {
         console.log('File is updated succesfully');
       }, 
       function(sender,args){
         console.log(args.get_message());
       }
      );

  },
  function(sender,args){
      console.log(args.get_message());
  });

Key points:

Two queries are submitted to server, fist one for retrieve file with its properties and second one to update list item.

Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167
  • 1
    Awesome! With only minor tweaking, your example does exactly what I want. Marked as answer! – Omegacron Aug 11 '14 at 22:06
  • Good to know and glad this helped you :) – Vadim Gremyachev Aug 11 '14 at 22:13
  • 1
    Nice @vadim. I just replied to a similar post on SPServices. There is also a way to do this via the old web services. See my blog post here: http://paultavares.wordpress.com/2014/02/18/how-to-get-information-about-a-sharepoint-list-item-using-its-url/ – Paul T. Aug 11 '14 at 23:22
2

I think you'd need to do it in 2 calls.

First, you'd have to query your list with a GetListItems call with CAML query like:

       <ViewFields>
      <FieldRef Name='ID' />
   </ViewFields>
   <Where>
      <Eq>
         <FieldRef Name='LinkFilename' />
         <Value Type='Computed'>file.txt</Value>
      </Eq>
   </Where>
   <QueryOptions>
      <ViewAttributes Scope='RecursiveAll' />
   </QueryOptions>

This would get you the ID of the item. Once you have that, you could do a standard SPServices update of the item.

Eric Alexander
  • 43,293
  • 10
  • 53
  • 93
  • Your answer is right - wish I could choose two as the answer. I ended up using the example code posted by Vadim, so I chose that one as the official answer. That code DOES use two different calls to achieve the result, though. – Omegacron Aug 11 '14 at 22:07
  • No problem, his is more detailed, I didn't have the time to mockup fully functioning code. – Eric Alexander Aug 11 '14 at 22:09
0

First fetch the data using SharePoint REST List Api. Please check following link for that: How can I query for "AssignedTo = [ME]" using REST? Here use


<QueryOptions>
      <ViewAttributes Scope='RecursiveAll' />
   </QueryOptions>

to get the data from the folder. After that use MERGE method to update data of a list Item. Check http://msdn.microsoft.com/en-us/library/ff798339.aspx

Sudip Ranjan Sil
  • 833
  • 8
  • 13