如何在 SharePoint WebPart 中包含 JavaScript 文件?
我们有一个中等大小的 .js 文件,我们将其包含在我们的 Web 框架中,我将其移植到 SharePoint。 但是,我不确定如何解决这个问题或最佳实践是什么。 这是一个将由其他客户端项目使用的框架解决方案,因此它最好是独立且可部署的,而不是需要手动将文件部署到网络服务器。
我目前的想法是将 JavaScript 放入嵌入式资源中,然后使用脚本管理器写出文件。 这看起来合理吗? 或者有人有其他推荐吗?
We have a medium sized .js file that we include in our web framework that I am porting over to SharePoint. However, I'm not sure how to go about this or what the best practice is. This is for a framework solution that will be used by other client projects, so it's best for it to be self contained and deploy-able, rather than requiring manually deploying files to the webserver.
My current thinking to put the JavaScript into an embedded resource and then use the script manager to write out the file. Does this seem reasonable? Or does anyone have any other recommendations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嵌入资源是最好的方法,您不需要使用 ScriptManager 来呈现它(因为 AJAX 没有在 SharePoint 上配置 OoB),您可以将其呈现为任何其他客户端脚本资源(通过 ClientScriptManager)。
最好的想法是有一个 if ContainsScriptManager else UsClientScriptManager 风格。
这样你就能两全其美
Embeded resource is the best way and you don't need to use the ScriptManager to render it out (as AJAX is not configured OoB on SharePoint), you can just render it as any other client script resource (through the ClientScriptManager).
Best idea is the have an if ContainsScriptManager else UsClientScriptManager style.
That way you get the best of both worlds
您可以将其放入文档库中。
如果要将 Web 部件打包到共享点解决方案中,则可以将其包含为模块(VSEWSS 项目)。 您的清单文件将得到类似以下内容:
网上有很多关于如何执行此操作的信息。 我喜欢图像、CSS 和 JavaScript 的文档库选项,因为每次对某些样式或脚本进行细微调整时,我不必重新构建并等待 SP 进行 JIT 编译 30 秒。 我只是在 SPDesigner 中从文档库中编辑这些内容。
You could just toss it into a doc library.
If you are packaging your web part into a sharepoint solution, you could include it as a Module (VSEWSS item). Your manifest file would get something like:
Theres lots of info on how to do this on the web. I've liked the doc library option for images, css, and javascript because I don't have to rebuild and wait for for SP to JIT compile for 30 seconds each time I do a minor tweak to some style or script. I just edit these things in SPDesigner right out of the doc library.
在您的项目中创建资源映射文件夹,并将 js 文件放在那里,并将 js 文件的引用指定为 _layout/jsfilename.js,此 js 文件将随您的项目一起部署,无论您将在何处部署它。
Create resource mapped folder in your project and put the js file there and give the referene of the js file as _layout/jsfilename.js and this js file will be deployed with your project where ever you will deploy it.