Adding javascript files to your SharePoint 2010 web part.

by Abe Miessler 12. April 2011 07:31

For something so simple, this problem took me quite a while so I wanted to share how I resolved it.  In my situation I wanted to add jQuery and a few jQuery plugin files to be part of my visual web part.  This web part was going to be part of a wsp that would be shipped out to SharePoint environments that I had no control over, so simply including them in the master page was not an option.

 

Step 1:

Right click on your project file and select Add->SharePoint "Layouts" Mapped Folder

 

Step 2:

In side of the Layouts Mapped folder create the following file structure

YourProjectName/js

 

Step 3:

Add your js files to the folder you just created in your project.

 

Step 4:

Inside of your ascx file for you visual web part create the ScriptLink tag

<SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="YourProjectName/js/jquery.min.js" Localizable="false" LoadAfterUI="true" />

If you have jQuery plugins, place their ScriptLink  tags AFTER the jQuery tag

 

 

You should now be in business.  If you are havinig problems check the following directory to make sure your scripts are being deployed:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\YourProjectName\js

 

 

Tags:

ASP.NET | jQuery | SharePoint

Powered by BlogEngine.NET 1.6.0.0

About the author

Abe lives with his beautiful wife Jessica, their cat Molly and their dog Duke in Sacramento California.  He enjoys outdoor activities, anything that has to do with technology and playing chess.

profile for Abe Miessler on Stack Exchange, a network of free, community-driven Q&A sites