Javascript 如何使用SPServices和SharePoint 2010搜索和替换列表项名称?
我的目标是在整个SharePoint网站中修改列表项(包括显示名、文件名、标题、URL等)。这是因为一项重大的业务重组改变了业务层次结构和项目名称。我们有太多的文件和URL手动编辑它们。我们需要对整个SharePoint站点中的所有列表项执行相当于搜索和替换操作的操作。到目前为止,我们发现的更新列表项的代码示例都是简单的set-a-flag操作样式,对所有列表项使用相同的值,但我们需要读取每个列表项的内容,并将这些值用作新更新值的一部分(除非SPServices/SharePoint具有一些与Unix grep/sed命令等效的命令) 为了开发和测试,我将以下HTML/javascript代码添加到内容编辑器Web部件(CEWP)的HTML部分,该部分位于MySite的内容页面上(因为我真的不想对实时生产数据进行实验),这个MySite还包括很多文本/DOC/PPT/XLS文件,其中有各种名称/标题等可供搜索。现在,这个脚本试图修改一个名为“test4blah.txt”的文件(最终我将替换这个CAMLQuery来修改多组文件)。javascript执行时没有错误,成功地找到查询的列表项,并在GUI中显示假定已修改的列表项和假定已修改的新值(即,从用户的GUI角度来看,一切看起来都是成功的),但不幸的是在幕后,SharePoint中的实际列表项从未实际修改过。执行此脚本时,javascript控制台中未显示任何错误消息。在代码的UpdateListItems部分中,我们尝试了许多关于内部静态名称、显示名称等的变体,但都没有成功 我们使用的是Microsoft SharePoint 2010、SPServices(jquery.SPServices-2014.01.min.js)和jquery(jquery-1.11.0.min.js)。我拥有完整的网站集管理员权限Javascript 如何使用SPServices和SharePoint 2010搜索和替换列表项名称?,javascript,jquery,replace,sharepoint-2010,spservices,Javascript,Jquery,Replace,Sharepoint 2010,Spservices,我的目标是在整个SharePoint网站中修改列表项(包括显示名、文件名、标题、URL等)。这是因为一项重大的业务重组改变了业务层次结构和项目名称。我们有太多的文件和URL手动编辑它们。我们需要对整个SharePoint站点中的所有列表项执行相当于搜索和替换操作的操作。到目前为止,我们发现的更新列表项的代码示例都是简单的set-a-flag操作样式,对所有列表项使用相同的值,但我们需要读取每个列表项的内容,并将这些值用作新更新值的一部分(除非SPServices/SharePoint具有一些与U
<html>
<head>
<script type="text/javascript" src="https://MySiteName123/SiteAssets/jquery.min.js"></script>
<script type="text/javascript" src="https://MySiteName123/SiteAssets/jquery.SPServices.min.js"></script>
<title>Edit the file</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.btn {
font-family: verdana;
font-weight: normal;
cursor: pointer;
color: #ffffff;
font-size: 16px;
background: #3498db;
padding: 10px 20px 10px 20px;
text-decoration: none;
}
.btn:hover {
background: #3cb0fd;
text-decoration: none;
}
</style>
<script type="text/javascript">
// Define some of our variables as "global" (to be accessible from our jQuery external libraries).
var modifiedListItems = 0;
var modifiedListItemsAttempted = 0;
var ms2min = 1 / (1000 * 60); // constant to convert milliseconds into minutes
function EditTheFile() {
myStartDate = new Date();
$("#ScriptStatus").show();
$('#ScriptStatus').append("Started running at " + myStartDate + "<br>");
// Loop through all sites in the site collection
GetAllSites();
myEndDate = new Date();
myDuration = (myEndDate - myStartDate) * ms2min; // date differences are calculated in milliseconds
$('#ScriptStatus').append("Finished running at " + myEndDate + "; duration was " + (Math.round(myDuration * 100) / 100) + " minutes" + "<br>");
$('#ScriptStatus').append("Number of modified List Items = " + modifiedListItems + " out of " + modifiedListItemsAttempted + " attempts" + "<br>");
}
function GetAllSites() {
$().SPServices({
operation: "GetAllSubWebCollection",
async:false,
completefunc: function (xData, status) {
$(xData.responseXML).find("Webs > Web").each(function() {
mySiteTitle = $(this).attr("Title");
mySiteURL = $(this).attr("Url");
GetAllLists(mySiteURL);
});
}
});
} // end of GetAllSites
function GetAllLists(mySite) {
$().SPServices({
operation: "GetListCollection",
webURL: mySite,
async: false,
completefunc: function (xData, status) {
$(xData.responseXML).find("List").each(function() {
myListTitle = $(this).attr("Title");
myListURL = $(this).attr("DefaultViewUrl");
myListType = GetListType( $(this).attr("BaseType") );
ModifyListItemURL(mySiteURL, myListURL, myListTitle);
});
}
});
} // end of GetAllLists
function ModifyListItemURL(myURL, myListURL, myListName) {
$().SPServices({
operation: "GetListItems",
webURL: myURL,
listName: myListName,
async: false,
CAMLQuery: "<Query><Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>test4 blah</Value></Contains></Where></Query>",
CAMLViewFields: '<ViewFields Properties="True"/>',
completefunc: function(xData, Status) {
$(xData.responseXML).SPFilterNode('z:row').each(function() {
var listItemID = $(this).attr("ows_ID");
var myListItemName = $(this).attr("ows_FileLeafRef"); // SharePoint display-name of "Name" is equivalent to internal-name of "FileLeafRef"
var myListItemURL = $(this).attr("ows_EncodedAbsUrl"); // SharePoint display-name of "Encoded Absolute URL" is equivalent to internal-name of "EncodedAbsUrl"
var myListItemTitle = $(this).attr("ows_Title");
// Modify the local variable's text values.
var new_myListItemName = myListItemName.replace("blah", "doubleplus-blah");
var new_myListItemURL = myListItemURL.replace("blah", "doubleplus-blah");
console.log("stevie17 modifyTheData for Name, oldString=" + myListItemName + ", newString=" + new_myListItemName);
console.log("stevie18 modifyTheData for EncodedAbsUrl, oldString=" + myListItemURL + ", newString=" + new_myListItemURL);
myListItemName = new_myListItemName;
myListItemURL = new_myListItemURL;
// Modify the data to have the newly-modified text values.
$().SPServices({
operation: 'UpdateListItems',
// webURL: myURL,
listName: myListName,
async: false,
updates: '<Batch><Method ID="1" Cmd="Update">'
+ '<Field Name="ID">' + listItemID + '</Field>'
// + '<Field Name="FileLeafRef">' + myListItemName + '</Field>'
// + '<Field Name="EncodedAbsUrl">' + myListItemURL + '</Field>'
// + '<Field Name="Name">' + myListItemName + '</Field>'
// + '<Field Name="Encoded Absolute URL">' + myListItemURL + '</Field>'
+ '<Field Name="ows_FileLeafRef">' + myListItemName + '</Field>'
+ '<Field Name="ows_EncodedAbsUrl">' + myListItemURL + '</Field>'
+ '</Method></Batch>',
completefunc: function(xData, Status) {
modifiedListItemsAttempted++;
// Append information to the display table.
$('#ListInfoTable > tbody:last').append("" +
"<tr>" + // Start row
"<td>" + mySiteURL + "</td>" + // Site URL
"<td>" + mySiteTitle + "</td>" + // Site Title
"<td>" + myListName + "</td>" + // List Title
"<td>" + myListURL + "</td>" + // List URL
"<td>" + myListType + "</td>" + // List Type
"<td>" + myListItemURL + "</td>" + // List Item URL
"<td>" + myListItemName + "</td>" + // List Item Name
"<td>" + myListItemTitle + "</td>" + // List Item Title
"</tr>" +
"");
if (Status != "success") {
alert("Something went wrong with the update procedure.");
}
else {
modifiedListItems++;
}
}
});
});
}
});
} // end of GetListItemURL
// Display a human-readable form for the item type.
function GetListType(myBaseType) {
var myBaseTypeDescription;
if ( myBaseType == 0 ) { myBaseTypeDescription = "Generic List"; }
else if ( myBaseType == 1 ) { myBaseTypeDescription = "Document Library"; }
else if ( myBaseType == 2 ) { myBaseTypeDescription = "Unused"; }
else if ( myBaseType == 3 ) { myBaseTypeDescription = "Discussion Board"; }
else if ( myBaseType == 4 ) { myBaseTypeDescription = "Survey"; }
else if ( myBaseType == 5 ) { myBaseTypeDescription = "Issue"; }
else { myBaseTypeDescription = "None"; }
return myBaseTypeDescription;
} // end of GetListType
</script>
</head>
<body>
<!-- Display GUI controls for the query operations. -->
<div>
<span class="btn" style="width:100px; text-align:center; margin-top:5px; margin-bottom:10px; display:inline-block" onClick="javascript:EditTheFile();">Edit the file</span>
</div>
<!-- Display summary statistics about the query results. -->
<div id="ScriptStatus" style="padding:5px; margin-bottom:10px; border:thin gray solid; display:none;">
</div>
<!-- Display a table with the query results. -->
<table id="ListInfoTable" cellpadding="2" cellspacing="2" border="1">
<thead>
<tr bgcolor="#E4E4E4">
<th>Site URL</th>
<th>Site Title</th>
<th>List Title</th>
<th>List URL</th>
<th>List Type</th>
<th>ListItem URL</th>
<th>ListItem Name</th>
<th>ListItem Title</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
编辑文件
.btn{
字体系列:verdana;
字体大小:正常;
光标:指针;
颜色:#ffffff;
字体大小:16px;
背景:#3498db;
填充:10px 20px 10px 20px;
文字装饰:无;
}
.btn:悬停{
背景#3cb0fd;
文字装饰:无;
}
//将一些变量定义为“全局”(可从jQuery外部库访问)。
var modifiedListItems=0;
var modifiedListItemsAttentite=0;
var ms2min=1/(1000*60);//将毫秒转换为分钟的常量
函数editFile(){
myStartDate=新日期();
$(“#脚本状态”).show();
$(“#ScriptStatus”).append(“在“+myStartDate+”
”开始运行”;
//循环浏览网站集中的所有网站
GetAllSites();
myEndDate=新日期();
myDuration=(myEndDate-myStartDate)*ms2min;//日期差异以毫秒为单位计算
$(“#ScriptStatus”).append(“在“+myEndDate+”完成运行”;持续时间为“+(Math.round(myDuration*100)/100)+“分钟”+”
”;
$(“#ScriptStatus”)。追加(“修改列表项的数量=“+modifiedListItems+”,共“+ModifiedListItemsAttentited+”尝试“+”
”;
}
函数GetAllSites(){
$().SPServices({
操作:“GetAllSubWebCollection”,
async:false,
completefunc:函数(扩展数据、状态){
$(扩展数据.responseXML).find(“Web>Web”).each(函数(){
mySiteTitle=$(this.attr(“Title”);
mySiteURL=$(this.attr(“Url”);
GetAllList(mySiteURL);
});
}
});
}//GetAllSites结束
函数getAllList(mySite){
$().SPServices({
操作:“GetListCollection”,
webURL:mySite,
async:false,
completefunc:函数(扩展数据、状态){
$(扩展数据.responseXML).find(“List”).each(函数(){
myListTitle=$(this.attr(“Title”);
myListURL=$(this.attr(“DefaultViewUrl”);
myListType=GetListType($(this.attr(“BaseType”));
ModifyListItemURL(mySiteURL、myListURL、myListTitle);
});
}
});
}//GetAllList的结尾
函数ModifyListItemURL(myURL、myListURL、myListName){
$().SPServices({
操作:“GetListItems”,
webURL:myURL,
listName:myListName,
async:false,
CAMLQuery:“test4废话”,
CAMLViewFields:“”,
completefunc:函数(扩展数据、状态){
$(扩展数据.responseXML).SPFilterNode('z:row').each(函数(){
var listItemID=$(this.attr(“ows_ID”);
var myListItemName=$(this).attr(“ows_FileLeafRef”);//SharePoint显示名称“name”相当于内部名称“FileLeafRef”
var myListItemURL=$(this.attr(“ows_EncodedAbsUrl”);//Sh