Hello everyone. Recently I was searching for an easy way to add scripts to all pages in a SharePoint Site Collection. Mostly because I wanted to find an easy way to add jQuery and SPServices to the pages where I need them with an easy way to deploy new versions and without editing the Master Page of my SharePoint websites. And I think I found a good solution called SharePoint Delegate Control. All you need for this solution is Visual Studio installed on a SharePoint Development Server and you are ready to go.
The first step is to open up Visual Studio to create a new project.
Now create a new Empty SharePoint Project.
Choose Deploy as a farm solution for your project. This way your solution will be deployed in your entire farm and you can use it in every Web Application when you need it.
Now we need to add some new items to our project.
First create a new User Control item. I called mine “jQueryAndSPServicesControl.ascx”.
Next create a new Empty Element. I called mine “Elements.xml”.
Next step: Map SharePoint “Layouts” folder to your project to store your scripts.
Here is an example structure of the Layouts folder from my project:
Store your scripts by pasting them into your folder structure.
Now we will add our scripts into our User Control.
Here is the code from my “jQueryAndSPServicesControl.ascx” User Control:
Now we need to add our User Control to our Elements.xml file.
Here is the code from my “Elements.xml” XML file:
<?xml version="1.0" encoding="UTF-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Control ControlSrc="~/_CONTROLTEMPLATES/AddSPServicesSolution/jQueryAndSPServicesControl.ascx" Sequence="90" Id="AdditionalPageHead"/> </Elements>
After that you need to configure your feature settings. Enter a Title and a Description for your feature and choose a Scope. I chose Site since I wanted my feature to deploy my scripts in the entire Site Collection when I activate it.
After you finished all of the above steps you are ready to go. Start the Debug process to deploy your solution to test it.
When you have a look at your Site Collection features (when you chose Site as a Scope for your feature like I did) you will see that your feature is listed and activated.
Now go to a page in your SharePoint Site Collection and have a look at its source code. You will see that your scripts have been successfully added to your pages source code. (If you retract your solution, for example by stopping the Debug process, your scripts will be retracted from your pages as well.)
I used an example script to show you what happens when you activate and deactivate your solution in your Site Collection.
Here is the code of my example script:
My example script will simply get all items from a List called “City” and display them as a Bulleted List.
The below screenshot shows what my script will do when our custom feature is activated.
The next screenshot shows what happens when you deactivate our custom feature.
As you can see nothing bad happens to your page or Site Collection. Your scripts will just stop working.
That´s it. Like that you can deploy your own scripts to your Site Collections in a pretty simple manner. I hope you liked my post and I hope it was useful for you. If you liked my post and want the source code you can download my project from here.