Sharepoint文档库 - 使用javascript将文件名转换为标题

时间:2016-03-22 23:09:08

标签: javascript jquery sharepoint sharepoint-2013 sharepointdocumentlibrary

晚上好!

我在sharepoint文档库上有一个有效的功能。它的目的是将文件名复制到标题列。

它工作了很长时间但是在更新到sharepoint平台后它停止了工作,我无法弄清楚原因。

代码:

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

错误是:TypeError:$ spjs为null。它发生在spjs_QueryItems调用的库spjs-utility库中。

也许这可能是由于在更新期间添加的冲突库,但我该如何调试呢?

另外,如果这不起作用,用jQuery做同样的事情会不会更简单?那就是我现在正在尝试的东西,但我是初学者,你必须已经认识到了。

提前致谢。

最佳

1 个答案:

答案 0 :(得分:0)

我会测试你的 updateTitleFromName 函数中是否加载了JQuery:

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

如果未加载,请尝试使用绝对网址引用您的脚本以查看是否可以加载它们:

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