Document library: script to update Title from Filename

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:

<input type="button" onclick="updateTitleFromName()" value="Update Title from Name" />

<script type="text/javascript" src="/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/Scripts/spjs-utility/spjs-utility.js"></script>
<script type="text/javascript">

function updateTitleFromName(){
	var q, res, uRes, count;
	count = 0;
	q = "<Where><IsNull><FieldRef Name='Title' /></IsNull></Where>";
	res = spjs_QueryItems({"listName":_spPageContextInfo.pageListId,"query":q,"viewFields":["ID","FileLeafRef"]});
	if(res.count === 0){
		alert("No files without title found.");
		return;
	}
	if(!confirm("There are "+res.count+" files to update. The page will appear as frozen while the script is working.\n\nContinue?")){
		return;
	}
	$.each(res.items,function(i,item){
		uRes = spjs_updateItem({"listName":_spPageContextInfo.pageListId,"id":item.ID,"data":{"Title":item.FileLeafRef.split(";#")[1]}});
		if(!uRes.success){
			alert("Could not update the file: "+item.FileLeafRef+" due to the follwing error:\n\n"+uRes.errorText);
		}else{
			count += 1;
		}
	});
	alert("Updated "+count+" files.");
	location.href = location.href;
}
</script>

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.

Alexander

9 thoughts on “Document library: script to update Title from Filename”

  1. 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..

    Example:
    Replace user: xxx
    With user: yyy

    1. Hi,
      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?

      Alexander

  2. 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?

  3. Hi Alexander,
    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!
    Lana

  4. Alexander,

    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?

Leave a Reply