SharePoint – Use Delegate Control to deploy scripts to all pages


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.

visual_studio_2010_start

Now create a new Empty SharePoint Project.

visual_studio_2010_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.

visual_studio_deploy_farm_solution

Now we need to add some new items to our project.

visual_studio_2010_add_new_item

First create a new User Control item. I called mine “jQueryAndSPServicesControl.ascx”.

visual_studio_2010_add_user_control

Next create a new Empty Element. I called mine “Elements.xml”.

visual_studio_2010_add_empty_element

Next step: Map SharePoint “Layouts” folder to your project to store your scripts.

visual_studio_2010_add_layouts_folder

Here is an example structure of the Layouts folder from my project:

sharepoint_layouts_folder_structure

Store your scripts by pasting them into your folder structure.

Now we will add our scripts into our User Control.

visual_studio_2010_user_control_custom_scripts

Here is the code from my “jQueryAndSPServicesControl.ascx” User Control:

...
<script type="text/javascript" src="/_layouts/AddSPServicesSolution/jQuery/jQuery.min.js"></script>
<script type="text/javascript" src="/_layouts/AddSPServicesSolution/jQuery.SPServices/jQuery.SPServices.js"></script>
<script type="text/javascript" src="/_layouts/AddSPServicesSolution/jQuery.SPServices/jQuery.SPServices.min.js"></script>

Now we need to add our User Control to our Elements.xml file.

visual_studio_2010_elements_custom

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.

visual_studio_2010_feature_scope

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.

sharepoint_site_collection_features_custom

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.)

sharepoint_page_source_active_feature

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:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices({
operation: "GetListItems",
async: false,
listName: "City",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
$("#tasksUL").append(liHtml);
});
}
});
});
</script>
<ul id="tasksUL"/>

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.

sharepoint_delegate_control_feature_activated

The next screenshot shows what happens when you deactivate our custom feature.

sharepoint_delegate_control_feature_deactivated

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.

Sources:

http://www.jbmurphy.com/2012/07/03/how-to-add-custom-javascript-code-to-all-sharepoint-2010-pages-delegatecontrol/

http://howtosharepoint.blogspot.co.at/2009/11/include-jquery-file-to-sharepoint.html

http://styledpoint.com/blog/inject-your-jquery-javascript-into-sharepoint-2010-without-modifying-any-master-page-page-layout-or-using-a-web-part/

http://blogs.msdn.com/b/kaevans/archive/2011/04/06/adding-jquery-to-every-page-in-sharepoint-with-delegate-controls.aspx

http://spservices.codeplex.com/wikipage?title=GetListItems

Advertisements
Tagged ,

5 thoughts on “SharePoint – Use Delegate Control to deploy scripts to all pages

  1. Apostolos says:

    Very helpfull guide!! Thanks.. Is there any way to deploy jQuery files without using visuals studio ?

  2. i find it very interesting and educational. thanks for posting anyway. lista de email lista de email lista de email lista de email lista de email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: