I got a request for a script that updates the Title field to be the same as the Name field in a document library. This to be able to use a lookup column to pick relevant documents from another list or library.
This code will insert a button that lets you search for all documents that do not already have a Title set, and update it to be the same as the file name.
Add this code to a list view in the library you want to update:
If you want to add this code directly to the page, use a HTML form web part to hold the code. You can also use a CEWP to link to the code from another location (like a document library).
You find jQuery here
And you find spjs-utility.js here
Let me know if you have any questions.
9 thoughts on “Document library: script to update Title from Filename”
Hi, It works perfect! Is it possible to do a function for update a “Person or Group” field?
A “replace user” function to bee used when a person leaves and a new person take his role..
Replace user: xxx
With user: yyy
Getting below js error. I’ve just added the code to a CEWP above the default view of a document library. (tried putting webpart above and below the library itself) All I did was update the two js locations but get this..(have I missed something I need to change?)
Message: ‘_spPageContextInfo’ is undefined
Sorry, I forgot to tag this post with SP2010 / 2013. I suppose you are using SP 2007?
If so, replace the variable _spPageContextInfo.pageListId with ctx.listName (or the actual list GUID of the list).
Does this help?
Hi Alex, ctx.listName worked like a charm thanks! 🙂
This would be a handy button to put on the newform.aspx of a list where you have a lookup to a doc library.
In case you tried to lookup a document (with no title) you click the button to update that library (and perhaps refresh the form) and it would then appear in the lookup.
To this end, could I simply replace the ctx.listName with the list GUID on the newform.aspx?
This is wonderful! Exactly what I was looking for all over the net.
I was thinking about this very early this morning and was wondering if it is possible to modify it to execute on selection of New Form or Edit form of a list instead of update on click? My users will always have to select one or the other for what I am creating, so if the update of the lookup happened when they chose either New or Edit, there would be no forgetting to click the button, thus taking user error our of the equation.
I really appreciate your help. This is amazing!
This is possible, just add the code to newform and editform and call it directly on page load.
Script works great. However when it copies the name, it includes the file extension. Is there a way to have it copy the name excluding the file extension?