Programmatically trigger “New document” action in a document library
-
10-12-2019 - |
Question
I've been asked to check if it is possible to trigger the creation of a new document linked to a content type. Like, to make it simple, you have a webpart, a button, you click it, and it opens the document template with the Document Information Panel ready to add your metadatas.
Is it possible to trigger this in C# ?
Thank you
Solution
SharePoint invokes that with JavaScript
CoreInvoke('createNewDocumentWithRedirect2',event,'http:.... //there are more parameters with site Url, document template Url etc.
so you can do it the same way.
Open the default view in a browser and view the source. Search for "Create a new 'YourContentType'", take CoreInvoke() and use it with an A tag. It should look like this
<a onclick="CoreInvoke('createNewDocumentWithRedirect2',event,'http:\u002f\u002f<yoursitename>\u002fShared Documents\u002fForms\u002fNew Word Document\u002fDocTemplate.dotx', 'http:\u002f\u002f<yoursitename>\u002fShared Documents\u002fNew Word Document', 'SharePoint.OpenDocuments', false, 'http://<yoursitename>/_layouts/CreateNewDocument.aspx?id=http://<yoursitename>/Shared%20Documents/Forms/New%20Word%20Document/DocTemplate.dotx', true, 1); return false;" href="#">Word 2010</a>
Use that in a content editor web part or call it from your code behind.
Check this article
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange