简体   繁体   中英

Sharepoint Document Library - Filename to title using javascript

Good evening!

I had a working functionality on a sharepoint document library. It's purpose was to copy the name of a file to the title column.

It was working for a long time but after an update to the sharepoint platform it stopped working and i can't figure out why.

The code:

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

The error is: TypeError: $spjs is null. And it occurs inside the library spjs-utility library on the spjs_QueryItems call.

Maybe this could be due to a conflictuous library that as added during the updated, but how can i debug this?

Also, if this doesn't work, wouldn't it be simpler to do the same with jQuery? Thats what i'm trying right now, but i'm a beginner as you must have perceived.

Thanks in advance.

Best

I would test if JQuery is loaded or not in your updateTitleFromName function:

if (window.jQuery) {  
    alert('loaded');
} else {
    alert('not loaded');
}

if it is not loaded, try to reference your scripts with absolute urls to see if you can load them:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM