Subscribe:      
 

SharePoint WIKI Templates

Written By: Knox Cameron -- 1/6/2011 -- join -- contribute -- (3084) comments -- printer friendly version

Rating: Rate --

Categories: Design, MOSS 2007, SharePoint 2010, SharePoint Foundation 2010, WSS3

< Prev - 1 | 2 | - Next > | Become a paid author

SharePoint 2010

Unfortunately, the solution above won't work for SharePoint 2010.

For a start, it does not have a page for creating new wiki pages like the one in 2007. Even if it did, the above solution would not bring over all the content.

The reason is that SharePoint 2010 now allows you to embed web parts in the rich text content of wiki pages. Although these appear as part of the rich content to a user, on the page they are actually stored in a hidden web part zone. The rich content just contains a marker where they are placed when the page is displayed. So the above approach would copy the markers but miss the actual web parts.

So what we need is a solution that will actually copy the template page as a complete object. The best way I could find to do this was to use the FrontPage RPC protocol, the same protocol used by SharePoint Designer when editing a site.

This protocol is documented and supported, and has a move document Method that does what we need (when you give it the option to copy rather than actually move the file). However, it is also very old, and pre-dates all our modern standards for web services. I found it temperamental about getting data in exactly the right format, and the results of operations are returned as fairly unstructured html. The key to getting the protocol right in the end was to use Fiddler to capture the commands used by SharePoint Designer to copy a file, and emulate those commands.

For this solution, I used jQuery: The Write Less, Do More, JavaScript Library. This has functions to handle posting the RPC requests to SharePoint. Note that, although Microsoft supports the use of jQuery with SharePoint 2010, you still need to add the jQuery library onto your site and add a reference to it.

The steps are:

  • Download the jQuery library file, and save it into a SharePoint library on your site. Make sure it is published and available to all users.
  • Create the template page in your wiki site/library. For demonstration, I made one that looks like this:

Template page

  • Create a page to be used to create new pages. I called mine "NewProject". Add some explanatory text, then insert a Content Editor web part in the rich content field.
  • Click inside the content area of the web part and select HTML > Edit HTML Source from the ribbon. Make sure you are not seeing the HTML source of the rich text field containing the web part (I find this sometimes happens). Copy and paste the following code.
<hr/>
<p>To create a new project page, type the project name then press the 
<em>Create project</em> button below.</p>
<p>This will run a script that copies the template project page into a new 
page.</p>
<p>Edit the body of the page as required. Save the new page when you have 
finished. When you are ready to 
publish the project page, add a link to it onto the home page of the wiki.</p>

<!-- edit the path to the JQuery library file for your environment -->
<script src="/wikisite/Documents/jquery-1.4.4.min.js" type="text/javascript">
</script>

<script type="text/javascript">

function myCreateProject() 
{

// Configure these for your environment
// include no slashes in paths
var PATHTOWIKI = "/wikisite";
var PATHTOPAGES = "Pages";
// file name only for template page, no extension
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 project page'); 
}
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=\"/" + 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 project"/> <br/>
<p> </p>
<div id="myInfo"><b>Results</b><br/></div>
  • Edit the three variables with names in BLOCK CAPITALS near the top to match your site URL, the name of the library containing the wiki pages and the file name of the template page. Also edit the path to the jQuery library file near the top, or remove that block if you already load the jQuery library in your master page.
  • Save the changes to the Content Editor Web Part then save the page. Now you can test!

Page showing new project page created

In this screenshot, I entered a name for a new page of "Test" and clicked the button. This was successfully created by copying the template (although SharePoint reports it as a "rename" operation), and the link can be used to go to the new page. To demonstrate the error handling, I clicked the button again without changing the name, which generated the error that the file already exists. 

Comments and caveats

This solution requires the FrontPage RPC functionality to work. This is enabled by default, but may be disabled in your environment if (for example) use of SharePoint Designer is blocked.

I have not tested it, but this solution should also work for SharePoint 2007. The version number on the "move document" method may need to be adjusted.

The solution could easily be adapted to other requirements involving copying files in SharePoint.


Some notes on the code for programmers who are interested:

  • I had problems with the id selector '#' in jQuery not working reliably, similar to what was described in jQuery in SharePoint for Hackers, hence the use of getElementById.
  • I ended up manually encoding some of the characters when I was having issues getting the RPC calls to work. Some of that is probably unnecessary, but hey, it works.
  • I wasted a lot of time trying to use the object/value pairs format for the data parameter on the ajax call, but gave up as it never seemed to encode them the way the FrontPage RPC requires. In particular, RPC required the underscore character in parameter names to be encoded. Manually encoding my own string was inelegant, but again, it works.
  • The results from the RPC call are returned as an HTML page with paragraphs and list items. Hence the string matching to find the useful bits of information to return to the user.

Next Steps

< Prev - 1 | 2 | - Next >



Learn more about SharePoint



Sponsor Information




Copyright (c) 2010-2017 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | contribute | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


MSSharePointTips.com | MSSQLTips.com