我正在Sharepoint Foundation 2013上构建一个医疗wiki,这样就可以(1)从模板创建新的wiki页面,(2)将这些新页面的标题和URL添加到自定义列表中。 / p>
以下代码负责数字(1)。用户为新的wiki页面输入新名称,单击“创建档案”,模板页面将被复制并重命名为newname.aspx。
<hr/>
<p>To create a new dossier, type the child’s name then press the
<em>Create dossier</em> button below.</p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript">
</script>
<script type="text/javascript">
function myCreateProject()
{
var PATHTOWIKI = "/medical";
var PATHTOPAGES = "Medical Dossiers";
var TEMPLATEFILENAME = "template";
var myPathToWiki = encodeURIComponent(PATHTOWIKI);
var myPathToPages = PATHTOPAGES + "%2f";
var myTemplateFileName = encodeURIComponent(TEMPLATEFILENAME) + "%2easpx";
var EnteredProject = document.getElementById("NewProjName");
var myNewName = EnteredProject.value;
if(myNewName == "")
{
alert('Please enter a name for the new dossier');
}
else
{
myNewName = encodeURIComponent(myNewName) + "%2easpx"
$.ajax({
url: PATHTOWIKI + "/_vti_bin/_vti_aut/author.dll",
data: ( "method=move+document%3a14%2e0%2e0%2e4730&service%5fname="
+ myPathToWiki +
"&oldUrl=" + myPathToPages + myTemplateFileName +
"&newUrl=" + myPathToPages + myNewName +
"&url%5flist=%5b%5d&rename%5foption=nochangeall&put%5foption=edit&docopy=true"
),
success: function(data)
{
var rpcmsg1 = getMessage(data, "message=", "<p>");
$("#myInfo").append("<br/>" + rpcmsg1);
if(rpcmsg1.indexOf("successfully") < 0)
{
// get error info
var rpcmsg2 = getMessage(data, "msg=", "<li>");
$("#myInfo").append("<br/>" + rpcmsg2 + "<br/>");
}
else
{
$("#myInfo").append("<br/><a href=\"http://it.bethelchina.org/" + PATHTOWIKI + "/" + PATHTOPAGES +
"/" + myNewName + "\">Go to new page</a><br/>");
}
},
type: "POST",
beforeSend: function(XMLHttpRequest)
{
XMLHttpRequest.setRequestHeader("X-Vermeer-Content-Type",
"application/x-www-form-urlencoded");
}
});
}
}
function getMessage(data, startmsg, delim)
{
var msgpos = data.indexOf(startmsg);
var endpos = data.indexOf(delim, msgpos);
var rpcmsg = data.substring(msgpos + startmsg.length, endpos);
return rpcmsg;
}
</script>
<input id="NewProjName" type="text"/> <input id="AutoButton"
onclick="myCreateProject();" type="button" value="Create dossier"/> <br/>
<div id="myInfo"><b>Results</b><br/></div>
我无法弄清楚的是如何将新档案/页面的URL和相应的myNewName添加到自定义列表中。我知道SPservice和UpdateListItems必须发挥作用,但我没有运气编码到上面的jquery。
非常感谢帮助,
此致
马特
答案 0 :(得分:0)
您正在寻找的是Lists.UpdateListItems方法。令人惊讶的是,Lists.UpdateListItems方法也用于创建列表项。它在以下页面中有详细记录:
http://msdn.microsoft.com/en-us/library/office/websvclists.lists.updatelistitems(v=office.15).aspx
在您的成功函数中,调用第二个函数在自定义列表中创建新项目,并将创建项目所需的变量传递给它。这个(未经测试的)代码应该非常接近您的需求:
function createCustomListItem(newItemUrl, newItemName) {
var targetUrl = "../_vti_bin/lists.asmx";
var listName = "Custom List";
var newItemXml =
"<Batch OnError=\"Continue\" ListVersion=\"1\" ViewName=\"\">" +
"<Method ID=\"1\" Cmd=\"New\">" +
"<Field Name=\"ID\">New</Field>" +
"<Field Name=\"Title\">" + newItemName + "</Field>" +
"<Field Name=\"Item_x0020_Url\">" + newItemUrl + "</Field>" +
"</Method>" +
"</Batch>";
var soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
" <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
" <soap:Body> <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
" <listName>" + listName + "</listName>" +
" <updates>" + newItemXml + "</updates>" +
" </UpdateListItems> </soap:Body></soap:Envelope>";
$.ajax({
cache: false,
url: targetUrl,
type: "POST",
dataType: "xml",
data: soapEnv,
contentType: "text/xml; charset=utf-8",
beforeSend: function (xhr) {
xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
},
complete: function (msg) {
if ($(msg.responseXML).find("ErrorText").text().length === 0) {
// success
} else {
//Failure
}
}
});
}