![](/img/trans.png)
[英]Item is undefined when using SPServices to fetch items from Sharepoint list
[英]How to search-and-replace list item names using SPServices and SharePoint 2010?
我的目标是在我们整个SharePoint网站上修改列表项(包括显示名称,文件名,标题,URL等)。 由于主要的业务重组已更改了业务层次结构和项目名称,因此需要这样做。 我们有太多的文件和URL不能手动编辑它们。 我们需要在整个SharePoint网站的所有列表项上执行等效于搜索和替换操作。 到目前为止,我们发现的用于更新列表项的代码示例都是简单的set-a-flag样式的操作,所有列表项均使用相同的值,但是我们需要读取每个列表项的内容并使用这些值作为新更新值的一部分(除非SPServices / SharePoint具有与Unix grep / sed命令等效的功能)。
为了进行开发和测试,我将以下HTML / javascript代码添加到了MySite的“内容”页面上的内容编辑器Web部件(CEWP)的HTML部分中(因为我真的不想对实时生产数据进行试验),而且该MySite还包括许多文本/ DOC / PPT / XLS文件,并带有各种名称/标题/等供搜索。 现在,此脚本尝试修改一个名为“ test4 blah.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)。 我具有完整的网站集管理员特权。
<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>
根据您要完成的工作,建议您投资购买诸如Sharegate之类的第三方工具。 进行似乎需要的全面更改将比编写代码容易得多,并且该工具也可以很好地满足您其他目的。
如果您决定坚持使用编码路线,则需要精通控制台中的JavaScript调试。 UpdateListItems将为您提供响应错误(有时可能没有意义),您需要学习如何处理它们。
使用SharePoint SOAP和REST服务,成功意味着查询已成功提交,而未必成功执行。 您可能会获得20x状态返回,但是返回的数据中出现错误消息。
此页面为您提供了有关如何更新文档库中项目的示例: https : //msdn.microsoft.com/zh-cn/library/office/websvclists.lists.updatelistitems(v=office.14).aspx
警告:UpdateListItems上有一个阈值,每个调用最多可以更新160个项目(我不确定SPServices如何处理此问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.