Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. SharePoint
  4. referencing .js file in a web part

referencing .js file in a web part

Scheduled Pinned Locked Moved SharePoint
javascripthelpcsharpsharepointvisual-studio
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PleaseHelpCP
    wrote on last edited by
    #1

    I need a help on referencing .js file in a web part(Sharepoint 2010 & visual studio 2010). I have created javascript file as mentioned in following link http://msdn.microsoft.com/en-us/library/dd584169(v=office.11).aspx File location is C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\FolderNameSameAsProjectName\AssemblyVersion_KeyToken\myHello.js Not getting alert message on click of button. There is a postback on button click & giving javascript error that function name not found. Here is the code : protected override void CreateChildControls() { ClientScriptManager cs = Page.ClientScript; // Include the required javascript file. if (!cs.IsClientScriptIncludeRegistered("jsfile")) cs.RegisterClientScriptInclude(this.GetType(), "jsfile", "../wpresources/FolderNameSameAsProjectName/AssemblyVersion_KeyToken/myHello.js"); Button btnClick = new Button(); btnClick.Text = "Click"; btnClick.OnClientClick = "Hello();"; this.Controls.Add(btnClick); }

    S N 2 Replies Last reply
    0
    • P PleaseHelpCP

      I need a help on referencing .js file in a web part(Sharepoint 2010 & visual studio 2010). I have created javascript file as mentioned in following link http://msdn.microsoft.com/en-us/library/dd584169(v=office.11).aspx File location is C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\FolderNameSameAsProjectName\AssemblyVersion_KeyToken\myHello.js Not getting alert message on click of button. There is a postback on button click & giving javascript error that function name not found. Here is the code : protected override void CreateChildControls() { ClientScriptManager cs = Page.ClientScript; // Include the required javascript file. if (!cs.IsClientScriptIncludeRegistered("jsfile")) cs.RegisterClientScriptInclude(this.GetType(), "jsfile", "../wpresources/FolderNameSameAsProjectName/AssemblyVersion_KeyToken/myHello.js"); Button btnClick = new Button(); btnClick.Text = "Click"; btnClick.OnClientClick = "Hello();"; this.Controls.Add(btnClick); }

      S Offline
      S Offline
      saanj
      wrote on last edited by
      #2

      You can use ScriptLink class to register a javascript resource which can be used when the page gets rendered. Checkout the following thread which will give you some idea about how to use ScriptLink: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/3e4caa8d-08ee-40e8-84ea-dec6c988de0e[^]

      There is no foolish question, there is no final answer...

      N 1 Reply Last reply
      0
      • P PleaseHelpCP

        I need a help on referencing .js file in a web part(Sharepoint 2010 & visual studio 2010). I have created javascript file as mentioned in following link http://msdn.microsoft.com/en-us/library/dd584169(v=office.11).aspx File location is C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\FolderNameSameAsProjectName\AssemblyVersion_KeyToken\myHello.js Not getting alert message on click of button. There is a postback on button click & giving javascript error that function name not found. Here is the code : protected override void CreateChildControls() { ClientScriptManager cs = Page.ClientScript; // Include the required javascript file. if (!cs.IsClientScriptIncludeRegistered("jsfile")) cs.RegisterClientScriptInclude(this.GetType(), "jsfile", "../wpresources/FolderNameSameAsProjectName/AssemblyVersion_KeyToken/myHello.js"); Button btnClick = new Button(); btnClick.Text = "Click"; btnClick.OnClientClick = "Hello();"; this.Controls.Add(btnClick); }

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        SharePoint is a web application and as such follows the rules of file access. "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions" is not assessable by the ASP.NET user account for SharePoint. Any resources for your webpart should be placed under the layouts folder, which is assessable via the virtual folder under the WebApplication. Developing for SharePoint is not simple ASP.NET. You need to understand how it functions, how files are accessed, etc. Also, in the future, please format any code you post using the "code block" toolbar item.


        I know the language. I've read a book. - _Madmatt

        1 Reply Last reply
        0
        • S saanj

          You can use ScriptLink class to register a javascript resource which can be used when the page gets rendered. Checkout the following thread which will give you some idea about how to use ScriptLink: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/3e4caa8d-08ee-40e8-84ea-dec6c988de0e[^]

          There is no foolish question, there is no final answer...

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          The OP is not creating a Visual WebPart


          I know the language. I've read a book. - _Madmatt

          S 1 Reply Last reply
          0
          • N Not Active

            The OP is not creating a Visual WebPart


            I know the language. I've read a book. - _Madmatt

            S Offline
            S Offline
            saanj
            wrote on last edited by
            #5

            Hi Mark & PHC, Though the link which I had shared was related to the SharePoint 2010 Visual Web Part but that did not necessarily mean that ScriptLink is restricted to be used with only Visual Web Part. It can also be used with SharePoint 2010 Standard Web Part quite easily. I know words are not convincing enough few times; so let me give a quick code example: (i) Create a Empty SharePoint 2010 project and add a standard web part into it. (ii) Use the following code block to add a ScriptLink and a Button control:

            ScriptLink scriptLink;
            Button btnOk;
            protected override void CreateChildControls()
            {
            scriptLink = new ScriptLink();
            scriptLink.Name = "test.js";
            this.Controls.Add(scriptLink);

            btnOk = new Button();
            btnOk.Text = "Show Alert";
            btnOk.OnClientClick = "javascript:return showMsg();";
            this.Controls.Add(btnOk);
            

            }

            (iii) In a text editor, create a javascript file (.js) with the following javascript code:

            function showMsg()
            {
            alert('ScriptLink is so easy.');
            }

            (iv) Save the js file in the following location and name it test.js: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033 (v) Finally, deploy and test. I will be waiting for your comments guys. :cool:

            There is no foolish question, there is no final answer...

            N 1 Reply Last reply
            0
            • S saanj

              Hi Mark & PHC, Though the link which I had shared was related to the SharePoint 2010 Visual Web Part but that did not necessarily mean that ScriptLink is restricted to be used with only Visual Web Part. It can also be used with SharePoint 2010 Standard Web Part quite easily. I know words are not convincing enough few times; so let me give a quick code example: (i) Create a Empty SharePoint 2010 project and add a standard web part into it. (ii) Use the following code block to add a ScriptLink and a Button control:

              ScriptLink scriptLink;
              Button btnOk;
              protected override void CreateChildControls()
              {
              scriptLink = new ScriptLink();
              scriptLink.Name = "test.js";
              this.Controls.Add(scriptLink);

              btnOk = new Button();
              btnOk.Text = "Show Alert";
              btnOk.OnClientClick = "javascript:return showMsg();";
              this.Controls.Add(btnOk);
              

              }

              (iii) In a text editor, create a javascript file (.js) with the following javascript code:

              function showMsg()
              {
              alert('ScriptLink is so easy.');
              }

              (iv) Save the js file in the following location and name it test.js: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033 (v) Finally, deploy and test. I will be waiting for your comments guys. :cool:

              There is no foolish question, there is no final answer...

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              I certainly don't need a step by step tutorial on how to create a web part for SharePoint. Yes, you can create a ScriptLink object from within code but just because you can doesn't men you should. Take some time to actually learn what is happening, how the control functions and where it is most appropriate to use.


              I know the language. I've read a book. - _Madmatt

              S 1 Reply Last reply
              0
              • N Not Active

                I certainly don't need a step by step tutorial on how to create a web part for SharePoint. Yes, you can create a ScriptLink object from within code but just because you can doesn't men you should. Take some time to actually learn what is happening, how the control functions and where it is most appropriate to use.


                I know the language. I've read a book. - _Madmatt

                S Offline
                S Offline
                saanj
                wrote on last edited by
                #7

                Mark, I appreciate your remarks. My intention was not to teach you how to create a web part for SharePoint. There is nothing to get ego involved in the knowledge sharing process and get angry right? In codeproject, we share our experiences. Yes, I don't have hesitation to accept that I am unaware about when I should not use ScriptLink control. But, can you please share where exactly I am wrong? When should I not use ScriptLink control and why?

                There is no foolish question, there is no final answer...

                N 1 Reply Last reply
                0
                • S saanj

                  Mark, I appreciate your remarks. My intention was not to teach you how to create a web part for SharePoint. There is nothing to get ego involved in the knowledge sharing process and get angry right? In codeproject, we share our experiences. Yes, I don't have hesitation to accept that I am unaware about when I should not use ScriptLink control. But, can you please share where exactly I am wrong? When should I not use ScriptLink control and why?

                  There is no foolish question, there is no final answer...

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  The ScriptLink control is designed to be used on page, not within a webpart. Take some time to look at the inner workings of the class and you will see methods and functionality specific that usage, such as ondemand loading and dependency checking. Webparts typically register the scripts necessary for there operation.

                  saanj wrote:

                  My intention was not to teach you

                  saanj wrote:

                  There is nothing to get ego involved

                  Yet you automatically assume you need to give a tutorial on how to create a webpart. You give the impression that no one else in the thread knows how, and particularly me since you responded to my post, and you are more capability because you can show how it is done.


                  I know the language. I've read a book. - _Madmatt

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups