6

I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise. I am using publishing portal template. I am developing using VSTS 2008 + C# + .Net 3.5 + ASP.Net. And I want to develop a WebPart, which refers css and javascript (.js) files. My question is (1) how to deploy css/javascript files and (2) how to write reference path (e.g. ../../themes from my code below) from webpart to refer to related css/javascript files?

BTW: the existing code of css/javascript/Webpart is from existing aspx code and I am migrating aspx code to a Webpart. The code works in aspx.

Currently my code looks like this,

<link type="text/css" href="../../themes/test.css" rel="stylesheet" />
<script type="text/javascript" src="../../test.js"></script>
2
  • 1
    @George2: Please pay attention to the tags you're using. The publishing-site tag isn't relevant here, but css and javascript are. Thanks!
    – Alex Angas
    Commented Jan 10, 2010 at 22:14
  • Thanks Alex! I will pay attention to use appropriate tags and learning tags here.
    – George2
    Commented Jan 11, 2010 at 15:13

3 Answers 3

3

The best way to add javascript (inline or external) is using the ScriptManager to add it. That way you know you only add it once (you might add the web part multiple times on a page).

For inline code use the static method ScriptManager.RegisterClientScriptBlock() For js file use the static method ScriptManager.RegisterClientScriptInclude() You can even reference a script embedded in an assembly ScriptManager.RegisterClientScriptResource().

The javascript should be provisioned like any other artifact: through solutions. It could be added to a document library or _LAYOUTS folder.

Example of web parts using script manager: Sample: Say Hello Web Part Code Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part

hth Anders Rask

1
  • My confusion is how to use register script API in my specific case to register the js and css files? Any ideas?
    – George2
    Commented Jan 13, 2010 at 9:14
1

Will this css and js file only apply to the one web part, or is it something that will be used across many customizations?

The easiest way to deploy it (not correct way) would be to add it to the layouts directory so that it can be referenced on any site in the farm.

If it is going to be reused by many customizations, I would wrap it into a solution package separate from the custom web part. That way the "core" solution can be installed and maintained separately from the various implementations of it.

If it is not reused, then those files can be included in the same solution. Any scripts, styles, images, etc can be added this way.

3
  • "add it to the layouts directory" -- "it" you mean javascript and css files? If yes, when I reference css/javascript files in layout directory, how do I refer them in WebPart? By using what relative path in my WebPart code?
    – George2
    Commented Jan 11, 2010 at 15:12
  • 1
    see thats what you dont need to worry about if you use the scriptmanager :-p Just add "it" (yes js file) to _LAYOUTS folder (in a subdir) or in Style Library (in a subdir) in root site collection, add it to ScriptManager in web part and you are done... Commented Jan 12, 2010 at 8:32
  • "add it to ScriptManager in web part and you are done" -- confused. If I add js file into layout folder, then in ScriptManager, in order to add the js file to use, how to specify the js file's location?
    – George2
    Commented Jan 12, 2010 at 12:01
1

Take a look at this blog post: Register External JavaScript and CSS Files On SharePoint 2007 WebPart.

Registering Javascript

using Microsoft.SharePoint.WebControls;
ScriptLink.Register(this.Page, <JS_FILE_NAME>, true);

Registering CSS

using Microsoft.SharePoint.WebControls;
CssRegistration.Register(CSS_FILE_NAME);

I hope this helps!

1
  • If I use this code in a custom web part and then add two instances of the web part to the same page, do these calls implicitly prevent redundant additions of the script and CSS to the page, or should I perform some sort of an explicit check?
    – Web User
    Commented Oct 11, 2013 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.