简体   繁体   中英

Create and list new Sharepoint page template with jquery and SPservices

I'm building a medical wiki on Sharepoint Foundation 2013 that'll make it possible to (1) create new wiki pages from a template and (2) add the titles and urls of those new pages to a custom list.

The following code takes care of number (1). Users input a newname for the new wiki page, click 'create dossier' and the template page is copied and renamed 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>

What I can't figure out is how to add the URL of the new dossier/page and it's corresponding myNewName to a custom list. I know that SPservice and UpdateListItems will have to come into play but I'm having no luck coding it into the jquery above.

Help would be much appreciated,

Regards,

Matt

What you're looking for is the Lists.UpdateListItems Method. Surprisingly, the Lists.UpdateListItems Method is also used for creating list items. It's well documented on the following pages:

http://msdn.microsoft.com/en-us/library/office/websvclists.lists.updatelistitems(v=office.15).aspx

In your success function, call a second function to create the new item in your custom list and pass it the variables you need to create the item. This (untested) code should be very close to what you need:

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
            }
        }
    });
}

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