Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
1  
Awesome! With only minor tweaking, your example does exactly what I want. Marked as answer! –  Omegacron Aug 11 at 22:06
    
Good to know and glad this helped you :) –  Vadim Gremyachev Aug 11 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: paultavares.wordpress.com/2014/02/18/… –  Paul T. Aug 11 at 23:22
    
Thanks Paul, nice article! –  Vadim Gremyachev Aug 12 at 7:12

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.

share|improve this answer
    
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 at 22:07
    
No problem, his is more detailed, I didn't have the time to mockup fully functioning code. –  PirateEric Aug 11 at 22:09

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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