Buttons, Buttons, Buttons.How to create a Sp 2013 Javascript button to create a new doc

6 04 2016

You know what’s not so easy in Sp2013? Newform.aspx urls. Sure they live in the document library ribbon.
Sure you can also just drag stuff in, but what if you want to create a custom page and make it even easier for your users to create a new document?
That’s not so easy. So i set about figuring out the best way to do this.
Javascript buttons look cool and professional, so I thought, hey why not.
So I scoured the interwebs for some code. I came across this Gem by Ahmed Farag:
https://blogs.msdn.microsoft.com/how24/2014/09/23/create-a-link-to-display-edit-and-create-new-items-from-a-dialog-layover-in-sharepoint/
Thanks Ahmed you got me started!
This is an elegant solution if your library is out of the box. But what if you have custom content types?
That is not so easy to figure out. Once i did figure it out it was easy but not at first. You see, if you add a custom content type to a document library, the content type lives in _layouts, not in list/library name.
So how do you link that?
Well my friends, I am glad you asked.
First things first, like Ahmed says, grab yourself a content editor webpart (not script editor, that one has issues) and add it to a page.

Open your fav notepad editor
Copy Mr. Farag’s elegant code into it and click save.

 

(script type="text/javascript")
 function displayLayover(url) { 
 var options = SP.UI.$create_DialogOptions(); 
 options.url = url; 
 options.dialogReturnValueCallback = Function.createDelegate( 
 null, null); 
 SP.UI.ModalDialog.showModalDialog(options); 
 } 
 (/script)
Upload to your site asset library and link it via web part editor propeties
Now you have a pretty nifty button, but it doesn't do anything.
According to Ahmed, you should add the HREF key after your code for newitem.aspx.
However, as I alluded to early, that's not going to fly if you have a custom content type.
So here is his code:
<a href='javascript:displayLayover("/Lists/Contacts/NewForm.aspx?IsDlg=1")'>Display Item </a>
As you will figure out, content types do not live in /list/, they live in _layouts. So, how do i get that darn URL?

Its actually stupid simple. 
Basically, all you have to do navigate to your library settings, advanced settings, and find the open forms in dialogue, and click no.
Go back to your library and click new document and select your custom content type.

That will open your new item in a page, where, gasp, you can copy the url and past it into Ahmed code.

<a href='javascript:displayLayover("http://mysharepoint.domain.com/_layouts/15/NewDocSet.aspx?List=bc691a02%2D6335%2D4aee%2Da0e8%2D70185ab0491e&Source=http%3A%2F%2site%2Edomain%2Edomain%2Ecom%2F15%2Fnewlibrary%2FForms%2FAllItems%2Easpx&ContentTypeId=0x0120D520006B5814CAEDD0FD4C8F8F5C6B8AD7978C00F45CB6B8B2D5A74C8046CA70881BB0F7&RootFolder=%2F15%2Fputthisinyournewlibrary")'>New Content Type </a>

That's it. Now you have a new button that creates your new content type.

Tips: Make sure when you use content editor you upload your code to the asset library and link the file inside the content editor, so you can edit it easier.

TIP: Turn on versioning in your asset library so you can version your code out.

 

You are welcome world!

Nando

Advertisements