Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question
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 Jan 10 '10 at 22:14
    
Thanks Alex! I will pay attention to use appropriate tags and learning tags here. –  George2 Jan 11 '10 at 15:13

3 Answers 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

share|improve this answer
    
My confusion is how to use register script API in my specific case to register the js and css files? Any ideas? –  George2 Jan 13 '10 at 9:14

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.

share|improve this answer
    
"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 Jan 11 '10 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... –  Anders Rask Jan 12 '10 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 Jan 12 '10 at 12:01

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!

share|improve this answer
    
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 Oct 11 '13 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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