In the project of the visual web part for SharePoint, I can create files containing javascript, see picture: Project with javascript folder and file containing script

Question: how to use this script in code for:

  1. Execution on the client side by pressing a button (checking the correctness of filling out the form)
  2. Execution on the client side to display a message on completion of the action on the server?

At the moment I am in the code, on the server side I implement javascript after loading, respectively, this forces me to use string variables, which is not convenient and not correct. I would like to understand how to work with this, and how correctly.

  • What is this about? What string? Why implement after downloading? In the form <script src= added, the js file was attached to the manifest, wrapped in a cab - that's all. Use exactly the same as in the usual html or aspx without SharePoint. - nick_n_a
  • @nick_n_a in what form? And what kind of manifesto are we talking about? - Monomax
  • @nick_n_a is still not quite right. You left a hint in the comments, although if you create a js file and upload it to SharePoint, it will work, but many problems arise, in the end, I found the answer and shared it. - Monomax

1 answer 1

Separately, the file will not work, since it does not expand on the SharePoint server. You must use the module: In the right place of the project (be it the root or other project folder), right-click on "Add" -> "Office / SharePoint" -> "Module ". We clean everything up there, then we add the necessary file, in my case it is JavaScript1.js, the following XML is automatically created:

 <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="ScriptPanelButton"> <File Path="ScriptPanelButton\JavaScript1.js" Url="ScriptPanelButton/JavaScript1.js" /> </Module> </Elements> 

In general, with this we can already do something, but it is better to make changes:

 <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="ScriptPanelButton" Url="Scripts/PanelButtonScripts"> <File Path="ScriptPanelButton\JavaScript1.js" Url="ScriptPanelButton/JavaScript1.js" /> </Module> </Elements> 

Well, then in the code, when loading the panel:

 protected override void OnLoad(EventArgs e) { base.OnLoad(e); string scriptPath = "Scripts/PanelButtonScripts/ScriptPanelButton/JavaScript1.js"; Page.ClientScript.RegisterClientScriptInclude(GetType(), "SHW", scriptPath); } 

I note that a sub-site has been raised on our ball and the Scripts wiki library has been created in it, in which the script is unfolding. As a result, everything worked very well!