This project has moved and is read-only. For the latest updates, please go here.

Updating a single list item based on server relative URL

Aug 11, 2014 at 8:50 PM
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.
Aug 11, 2014 at 9:27 PM

With Document Libraries, you just specify the URL to the file (like "/site/library/folder/file.txt") to identify the item, rather than the ID as with lists.

See the documentation for the Lists Web Services:

Aug 11, 2014 at 9:43 PM
Is the list name required in all cases, or am I misreading the documentation? In my case I know it's going to be one of three libraries but not specifically which one. The server relative url of the item is a global variable throughout the script, so I can easily call it. It would be ideal if I could just get & update the file based solely on that.

If list name IS required, I guess I can write a function that breaks up the item url and looks to see which library is in the file path.

Either way, I appreciate the personal response. AND the awesome SPServices library you made for us.
Aug 11, 2014 at 9:46 PM
Yes, you always have to pass the listName into UpdateListItems. My guess is that by the time you have the URL for a document, you also know what list it is in. If not, you can parse the URL, as you suggested.

And you're welcome!

Aug 11, 2014 at 9:53 PM
p.s. You can also call GetListItems on the list to get additional info based on the URL (FileRef) and then call UpdateListItems.

Aug 11, 2014 at 11:43 PM
If by chance you only have a URL to a file from which you have to figure out the ListName then maybe this will help:

I had a similar need recently and found that there is an API that returns information about an item based on its URL.
Marc currently has it on his TODO list to include this service in SPServices:


Paul T.

-- Sent from Mobile

Aug 12, 2014 at 12:11 AM
Thanks Paul - that's pretty neat. In fact, I'm sure I'll find a use for that down the road, so glad to hear it's being added to SPServices. For this one, I just got it working via the method Mark mentioned - first using a GetListItems call to get the additional info using what I have, then using update in a second call.
Aug 12, 2014 at 3:53 PM

I'm curious how you are getting the FileRef without any of the associated data. Are you just grabbing it from the DOM? If so, it's very possible that the item ID is also available somewhere nearby.

Aug 12, 2014 at 10:04 PM
Using the nifty new JSLink feature, I'm overriding a text column with a series of action icons. In addition, I'm adding a checkbox to each document. When the user checks the checkbox, it's adding the item's server relative url to an array. My script than loops through that array and breaks it up into two separate arrays - one for files and one for folders. From there it does some further sorting and passes the url of the current array item to other functions.

One of the options I looked at was passing the ID into the array as well, but I ran into some issues later on when separating the ID from the path again. My scripting skills are still somewhat mediocre... but I'm learning. I managed to bash together about a dozen different scripts & snippets I found to get a working solution. The final script isn't pretty or optimized, but so far it works like a champ.
Aug 12, 2014 at 10:13 PM
What I usually do is add an HTML data- attribute to the element with the ID or any other value I need. So something like this:
<input type="checkbox" data-item-id="1"/>
This is very similar to how SharePoint "knows" what item you've checked. You can add any arbitrary data you need for client-side processing:
<input type="checkbox" data-item-id="1" data-color="red" data-font="Tahoma"/>