Add a Javascript reference to SharePoint using code

Source: Internet
Author: User

In the actual development process of SharePoint, we always have some common Javascript scripts. How can we add references to these client scripts? The simplest way is to use SharePoint Designer to directly edit the Master Page and add the following code in <Header> </Header>.

<script type="text/javascript" src="/_layouts/MyScripts.js">
This is the most direct method, but it is not recommended. In the development and test environments, you can add it quickly, but in the formal environment, we do not want to directly use SharePoint Designer to modify any site. I dynamically Add a reference to the script using another method.

Add Script Reference Through Feature

First, create a simple Feature:
1. Open VS 2010 and create an empty SharePoint project. To add a javascript reference, we can only select Deploy as a Farm Solution for deployment ".
2. After creating a new project, find the Features node in solution manager and right-click to add a new Feature. For example
  

      

3. Right-click the newly created Feature1 and add an Event handler
   

      

 
4. This is a simple Feature. Of course you can complete other information, such as the name and description of the Feature.

Add reference code

Next, add a script reference when activating feature. The code is very simple.
    
 // Uncomment the method below to handle the event raised after a feature has been activated.        SPWeb currentWeb;        public override void FeatureActivated(SPFeatureReceiverProperties properties)        {            try            {                using (currentWeb = (SPWeb)properties.Feature.Parent)                {                    if (currentWeb != null)                    {                                                currentWeb.CustomJavaScriptFileUrl = @"/_layouts/MyScripts.js";                        currentWeb.Update();                    }                }            }            catch (Exception ex)            { }            finally            {                if (currentWeb != null)                    currentWeb.Dispose();            }        }

Finally, use the packaging and deployment functions of VS to deploy the solution. Then, use "manage site application" to locate Feature1 and activate it.

Check deployment

Open the SharePoint site and view the source code: you will find that our script has been embedded into the page (in bold ).

  

document.write('<script type="text/javascript" src="/_layouts/blank.js?rev=QGOYAJlouiWgFRlhHVlMKA%3D%3D"></' + 'script>');document.write('<script type="text/javascript" src="/ScriptResource.axd?d=SKFLvF-EvR1mlvrYxWyEZjFDw6bS-SVrMGavINFSyCzVEqN_9eg59nW_dFeLRE6SgfxuA6tSz34aPo1TpYBKN2yiixIon88_QeT4s2k6JrM1&t=ffffffffc9d3eaae"></' + 'script>');// ]]></script><script type="text/javascript" src="/_layouts/MyScripts.js"></script><link type="text/xml" rel="alternate" href="/sites/SB/_vti_bin/spsdisco.aspx" />

Of course, there are other ways to communicate with you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.