One Content-type, multiple templates

Let’s assume I want to store different documents in a document-library in SharePoint, based on different templates. The easiest way would be to create different content-types for each kind of document. Each content-type can have an individual template in turn.

But what if I have just one logical content-type, but I have different document templates? I could create master content-type, which holds all properties. Then I create sub content-types and assign the different templates to those. This way I can maintain the properties on the master content-type and don’t have to worry about the sub content-types.

Problem solved!

But what I actually wanted was to have dedicated links to add new documents based on a certain template to my document library. I achieved this, but I needed to created three content-types in order to do so.

A different approach.

Instead of creating different content-types with different templates, I just create two hyperlinks “create document a” and “create document b”. With a couple of lines of javascript I’m ready to go.

First of I need a document library to store my templates. In the next step I add a content editor webpart to my listview. Because I cannot add javascript code to the content editor webpart directly, I placed my code in a separate file, which I also placed in the template document library (to keep thinks simple for now). So I reference this file in the content editor webpart.

This file basically contains some javascript to add new documents based on a certain template to the library:

function NewWord()
{
    var strTemplate = makeAbsUrl("/Template/WordTemplate.docx");
    var strSaveLocation = makeAbsUrl("/Shared Documents");
    var strProgId = "SharePoint.OpenDocuments";
    createNewDocumentWithProgID(strTemplate, strSaveLocation, strProgId, false);
    return false;
}
function NewExcel()
{
    var strTemplate = makeAbsUrl("/Template/ExcelTemplate.xlsx");
    var strSaveLocation = makeAbsUrl("/Shared Documents");
    var strProgId = "SharePoint.OpenDocuments";
    createNewDocumentWithProgID(strTemplate, strSaveLocation, strProgId, false);
    return false;
}

This with some little HTML …

<span style="width: 10px; height: 10px; overflow: hidden; display: inline-block; position: relative;">
    <img style="left: 0px !important; top: -128px !important; position: absolute;" src="/_layouts/images/fgimg.png" alt=""/>
</span>&#160;<a onclick="NewWord();" href="#">New Word-Document</a>&#160;|&#160;
<span style="width: 10px; height: 10px; overflow: hidden; display: inline-block; position: relative;">
    <img style="left: 0px !important; top: -128px !important; position: absolute;" src="/_layouts/images/fgimg.png" alt=""/>
</span>&#160;<a onclick="NewExcel();" href="#">New Excel-Document</a>

… could just do.

Half-day events in SharePoint

The Story

If you’ve been working with SharePoint you might have been working with calendar lists already. This is an easy to use way to add events to a web-based calendar. Like Outlook this features a checkbox to mark an event as a full-day event and thus omitting the start and end time (actually that’s not correct, behind the scenes the event starts ad 00:00 and ends at 23:59).

Recently someone asked on how to add a functionality like the full-day feature, but for half-day events. So you could just select wether an event should take place in the morning or in the afternoon.

That sound fairly simple to do.

How it’s done

OK, so I start by creating a new calendar from scratch. Next move to add an new item – and this is where we want to make our modifications to the form to allow selecting half-day events.

Because this view can not be edited directly in the browser you have to tweak SharePoint a little bit by altering the URL by adding toolpaneview=2 to the querystring.

Next you drag a content-editor-webpart below the editform and switch to the HTML source view.

I usually keep a copy of jQuery around, so I just add

<script language="javascript" src="/_layouts/jQuery/jquery-1.4.2.min.js" type="text/javascript"></script>

in the source-view. You might have to adjust the URL pointing to jQuery at this point. If you have no access to the file-system on your SharePoint server you could also just create a document library and place the jQuery library there and put the path in the above reference.

Next we add two buttons (this might not be the most pretty solution, but this is mostly about showing how this could be done!).

<input type=button value="morning" id="btnMorning">
<input type=button value="afternooen" id="btnAfternoon">

So the final step would be to hook to the click-events of the two buttons. Whenever one of the buttons are pressed, we just change the hour-selection for the start end end time of the event.

So the complete source looks like this:

<script language="javascript" src="/_layouts/jQuery/jquery-1.4.2.min.js" type="text/javascript"></script>
<input type=button value="morning" id="btnMorning">
<input type=button value="afternoon" id="btnAfternoon">
<script language="javascript">
  var startTime = $("#" + g_strDateTimeControlIDs["SPEventDate"] + "Hours");
  var endTime = $("#" + g_strDateTimeControlIDs["SPEndDate"] + "Hours");
$("#btnMorning").click(function() {
  startTime.val("08:");
  endTime.val("12:");
});
$("#btnAfternoon").click(function() {
  startTime.val("12:");
  endTime.val("18:");
});
</script>

Neat, if I may say so myself!

Getting those ASMXs

Creating a ASMX-page to leverage some service functionality to a client is fairly easy. The nice thing about that is, that you can even consume these services from JavaScript.

But out-of-the-box you can only call those services either via SOAP 1.1, SOAP 1.2 or a POST-request. But what if you want to use a plain-ol’ GET-request? You’ll always get an error-message stating, that GET is not supported – what a bummer.

But working around this problem is quite easy to do. Just open up your code-behind of your ASMX-Service and add

[ScriptMethod(UseHttpGet = true)]

To the method you’re calling. Finally you’ll also have to add the HTTP-GET to the list of "known" protocolls for webservices in the web.config. This is done by adding

<webServices>
  <protocols>
    <add name="HttpGet"/>
  </protocols>
</webServices>

to the system.web section in the web.config.

Thats all.