Useful Tip: Add a link to Create a New Document From a Content Type Template - SharePoint Development & Administration + InfoPath

Wednesday, August 18, 2010

Useful Tip: Add a link to Create a New Document From a Content Type Template

I found a blog post by Andy Burns that explains how to find and use the javascript from the 'New' button on a Document Library with managed content types to add a link to any page which will open a new document from the template.

As the "add new document" link on the summary toolbar of a List or Data View Web Part links to the Upload page of the document library, i used the javascript from the new button instead to create a link that opens a new blank document from the content type template associated with the document library.  This meant that the default save location for the document was set to the document library.

Andy's post explains how to incorporate the javascript behind the 'New' button into a link on a different page.

The issue that I was attempting to resolve is that I wanted to use the link in client applications outisde of SharePoint.  this meant that I couldn't use javascript, as the function called was not available to the application.

My solution was to add the javascript to the head setion of a separate page to a function which is called by the settimeout() method after a few seconds.  Once the function is called and the blank document loaded, the script redirected to the default view of the document library.

I used the timeout method as the script was not working properly if the page had not fully loaded, as well as to display a brief message to the user informing them of the process before redirecting to the document library.  Redirecting to the document library also prevents an infinant loop if you press "Cancel" if asked to confirm opening the blank document, as the page is reloaded when the Cancel button is pressed.

Javascript:

function newDoc()
    {

        'create a new document from a specific content type template
        createNewDocumentWithProgID('http:\u002f\u002fserver\u002fsites\u002fSales\u002fDocuments\u002fForms\u002fSales Order Form\u002fnew.xlsx', '
http:\u002f\u002fserver\u002fsites\u002fSales\u002fDocuments\u002fForms\u002fSales Order Form', 'SharePoint.OpenDocuments', false);
        

        're-direct to the document library (default view)
        window.location = 'http://server/sites/Sales/Documents/';
    }
    setTimeout ( "newDoc();", 1000 );


View Andy's post: Add a ‘Create New Document’ link to a page
Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

No comments:

Post a Comment