referencing .js file in a web part
-
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); }
-
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); }
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...
-
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); }
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
-
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...
The OP is not creating a Visual WebPart
I know the language. I've read a book. - _Madmatt
-
The OP is not creating a Visual WebPart
I know the language. I've read a book. - _Madmatt
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...
-
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...
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
-
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
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...
-
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...
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