Is there a better way to edit Elements.xml while creating a custom ribbon?
-
10-12-2019 - |
Question
I am creating my own custom ribbon button using this article as reference. My code in the file Elements.xml
goes something like this:
<CommandUIHandler
Command="NewRibbonButtonCommand"
CommandAction="javascript: MULTIPLE
LINES
OF
JAVASCRIPT
CODE">
Is there a better way to edit the JavaScript code in CommandAction
attribute, such as it would have IntelliSense at least for basic JavaScript objects like document
? As of now I am using XML Editor which is pretty basic. Other editors like Source Code Editor and HTML Editor don't help either. Even the Toolbox is empty when I am editing Elements.xml
and I am unable to figure out which all tags are available to me for Elements.xml
.
I am using Visual Studio 2012.
Solution
Yeah, it is pretty common problem. No matter how you try to edit your code it will be difficult to read, debug, manage, and update, when it is written inside elements.xml
.
I know that question is about the editor itself, but sometimes it is much better to write your JavaScript code in another .js
file, and use only reference in CommandAction
. You can load your script in several ways from custom action xml:
- Write another custom action with
ScriptLink
location. Pros - easy to use, cons - it will be loaded on everypage inside your custom action scope (site, web, etc.). Show me how it is done. - Use Script On Demand (SOD) technique to load your script dynamically from inside
CommandAction
script. SOD can give you magic, because it can load your script only when ribbon tab is opened and this way you can gain performance. Show me SOD 'bible'
Why this way is better. When you have your JavaScript code not in elements.xml
but in external files and you need to update your code inside JavaScript it is much easier. You will not need to update your custom actions, you just only need to upgrade your JavaScript files, no matter there they are, in Style Library
or in _layouts
folder. Debugging of separate JavaScript file is also easier, that inline code.
OTHER TIPS
While we are working with Sharepoint xml files like Feature.xml, Elements.xml or ONET.xml files we wont get any intellisense by default.
But for any developer it is hard to remember all these information. So, here is the easiest way to get the intellisense
- Open your xml file in Visual studio.
- Go to properties pane of the xml file and click on schema and browse for the file
- locate file named "wss.xsd" from C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/XML/wss.xsd (12 for MOSS, 14 for SP 2010 and 15 for SP2013)
Note : this file will have all the xml defined functions which is easily recognized by the visual studio.
Then you will get the intellisense for the SharePoint solution..
Credit :- http://kancharla-sharepoint.blogspot.com/2012/03/how-to-get-intellisece-for-featurexml.html