And the first question is WHY?
Yes, that was the first question someone asked me when I posed this question on Twitter [#sphelp]. And, it is a valid question! There are of course other more "approved" ways of creating tabs and buttons, and these options are more supported and have access to the current item or list or webpart that may be selected. However, there are some times when you can not use these methods and it is usually for the same reasons you can't do other things the "approved" way.
- You can not in any way shape form or fashion deploy custom or sandboxed items from Visual Studio.
- You may have SharePoint Designer, but you don't have the time to do this on every site and sub site you want your button on.
- Or, you might want to have something that is always available on as many pages regardless of what the content type is or if you are on a list view or not.
And so the next part is the Good Luck part!
So, if you know me, and understand my "SPEvilGenius" moniker, you will realize that I like a challenge even if the result is not "approved" or may not be fully "future proof"
So the solution begins!
So the below code was placed in a CEWP on a simple wiki page in the site pages library. I actually referenced the code as it was uploaded to the site assets library.
So this code does a few things. First I set a variable with the "button" image I am using set on an anchor tag that has a click event to fire a function. Simple jQuery there. The magic is part of the CUI.js file and SP.Ribbon js file. These are supposedly able to allow you to create items in script and for some of this it worked great! However, I had hoped to get a layout, section, and button from them added as well. If anyone knows how this might be done, please let me know! So after getting a tab and group added by the code, I then just use jQuery to add that html from the variable into that "group". This worked great and I got a tab with a group and a button that is clickable with just a few lines of code.
I plan to take the time to try to do more with this and see what I can do. Part 2 of this will be the results of those tests! Any feedback is appreciated!