Help registering external JavaScript files for SharePoint 2007.

Last post 01-05-2009 12:53 PM by JByrd2007. 3 replies.

Sort Posts:

  • Help registering external JavaScript files for SharePoint 2007.

    12-19-2008, 4:17 PM
    • Member
      447 point Member
    • JByrd2007
    • Member since 07-25-2007, 11:08 AM
    • Greensboro, NC
    • Posts 90

    I've been unable to do what I thought would be a simple task: register an external JavaScript file for use in SharePoint 2007 web parts/pages. This is a plain ol' JavaScript file (not AJAX related) to hold common functions to be shared across SP web parts/pages, so I don't need anything elaborate. I'm using Visual Studio Team System 2008 for all this work.

    Ideally, I'd like to register this at the Master Page level (like you would for CSS with a CssRegistration tag). If this won't do, then I'm thinking maybe having a base WebPart class that would have the registration (so that any subclasses would pick it up). Each of our web parts loads user controls that contain the bulk of what we do, so I thought I might also be able to have a base UserControl class that had the registration. I have tried each of these approaches, without success. I am always getting an 'Object Expected' error in IE at the JS function declaration.

    I used Reflector to look at the Resources in the assembly and I can see the .js file (and content) is present, so I'm thinking (hoping?) that this is purely a referencing issue.

    Here's what I've done so far:

    1.  Created a new folder in the project to hold the JavaScript files.

    2.   I have added a Common.js file to the project under the new folder.

    3.   Under the properties for Common.js, set the Build Action to "Embedded Resource."

    4.   Added the WebResource attribute to the AssemblyInfo.cs file for the project like this:

    [assembly: WebResource(@"ProjectNamespace.Files.common.js", @"text/javascript")]

    5.  I tried to register the script like the following, which doesn't give me an error, but doesn't work either (or rather doesn't solve the problem).

    Type resourceType = typeof(MyWebPartClass);

    ClientScriptManager manager = Page.ClientScript;

    string url = ResolveClientUrl(manager.GetWebResourceUrl(resourceType, "Common.js"));if (!manager.IsClientScriptIncludeRegistered(resourceType, "MyScripts"))

    {

    manager.RegisterClientScriptInclude(resourceType,
    "MyScripts", url);

    }

    This seems like it should be dead simple, has anyone else run into this issue and, if so, how did you fix it?

    Thanks,

    Jon

    Hope this helps!



    Jon

    If I was able to help, please mark this post as Answer.
  • Re: Help registering external JavaScript files for SharePoint 2007.

    12-22-2008, 11:18 AM
    Answer
    • Member
      447 point Member
    • JByrd2007
    • Member since 07-25-2007, 11:08 AM
    • Greensboro, NC
    • Posts 90

    I was able to fix this problem through two different approaches and learned some things along the way. Note: The steps I performed to add the project file and set it as an Embedded Resource are the same (though I did have to change the file location as outlined below).

    JavaScript File Reference

    First, to get the JavaScript file reference to work correctly, it appears that the JavaScript files have to either be in the root of the project directory or in a folder named Resources. Since my project structure mimics that of the 12 hive, I had tried to put it under [namespace].12.Template.Layouts, but I couldn't get the subfolder pathing/references to work properly (anyone know how to do this?).

    So, in the example my assembly reference now looks like this:

    [assembly: WebResource(@"ProjectNamespace.Resources.Common.js", @"text/javascript")]

    Secondly, I modified my web part base class in the override for CreateChildControls to include the following:

    ClientScriptManager manager = Page.ClientScript;if (!manager.IsClientScriptIncludeRegistered(@"WebPartScript"))

    {

    string url = manager.GetWebResourceUrl(this.GetType(), @"ProjectNamespace.Resources.Common.js");

    manager.RegisterClientScriptInclude(this.GetType(), @"WebPartScript", ResolveClientUrl(url));

    }

    JavaScript inline method:

    The other way (my fallback, since I discoverd this way first Smile) was to inline the script.

    First, I included the same assembly reference (above) to the script:

    [assembly: WebResource(@"ProjectNamespace.Resources.Common.js", @"text/javascript")]

    Second, I read the script from the embedded resource and registered the block as a script block on the page:

    String javaScript = string.Empty;

    using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(@"ProjectNamespace.Resources.Common.js"))

    using (StreamReader reader = new StreamReader(stream))

    javaScript = reader.ReadToEnd();

    Page.ClientScript.RegisterClientScriptBlock(javaScript.GetType(),

    @"ProjectNamespace.Resources.Common.js", javaScript, true);

     

    The real key is to make sure you get the namespace + file name correct, since it's very hard to debug if this is wrong (I only got runtime JS errors in IE for, "Object expected" when the JS runtime couldn't locate the function). 

    Hope this helps someone else out, because it was driving me crazy!

    Jon

    Hope this helps!



    Jon

    If I was able to help, please mark this post as Answer.
  • Re: Help registering external JavaScript files for SharePoint 2007.

    12-24-2008, 3:32 AM

    Hi Jon,

    Thank you sharing!  You did a good job here! Yes

    Hong-Gang Chen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Help registering external JavaScript files for SharePoint 2007.

    01-05-2009, 12:53 PM
    • Member
      447 point Member
    • JByrd2007
    • Member since 07-25-2007, 11:08 AM
    • Greensboro, NC
    • Posts 90

    Thank you Hong-Gang Chen!

     I hope it's helpful to others and thanks for the support Smile!

    Jon

    Hope this helps!



    Jon

    If I was able to help, please mark this post as Answer.
Page 1 of 1 (4 items)