sharepoint 编程_SharePoint 2010-以编程方式将JavaScript,元标记和CSS样式添加到每个页面的页眉

sharepoint 编程

For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could

对于SharePoint网站,尤其是面向公众的网站,有时向页面<head>部分添加JavaScript,元标记,CSS样式或其他内容比修改母版页面更实用。 例如,您可以

add the jQuery library to the head of every page.  If adding tags to the page <head> section is the only customization being made side-wide, doing so programmatically instead of modifying and un-ghosting the default master pages will increase the performance of the site.  (Un-ghosting a page means that SharePoint is no longer grabbing the master page from the local hard drive and is instead grabbing it from the Content Database.  This adds another call to the database that isn't necessary unless you have a fully branded master page). 将jQuery库添加到每个页面的开头 。 如果在页面<head>部分中添加标签是唯一在侧面进行的自定义,则以编程方式进行此操作(而不是修改和清除默认母版页)将提高站点的性能。 (对页面进行无鬼影处理意味着SharePoint不再从本地硬盘驱动器上获取母版页,而是从内容数据库中获取母版页。这会向数据库中添加另一个调用,除非您拥有完全品牌化的母版页)。

To demonstrate how to accomplish this programmatically using the AdditionalPageHead Content Place Holder, we will make a SharePoint solution in Visual Studio 2010 that adds a custom JavaScript file and a custom CSS file.  This solution could be further extended to dynamically add different content based on configurable settings, however for this demonstration we will keep it simple.

为了演示如何使用AdditionalPageHead内容占位符以编程方式完成此任务,我们将在Visual Studio 2010中制作一个SharePoint解决方案,该解决方案将添加自定义JavaScript文件和自定义CSS文件。 可以进一步扩展该解决方案以基于可配置的设置动态添加不同的内容,但是对于本演示,我们将使其保持简单。

1个 (1)

在运行SharePoint 2010的计算机上打开Visual Studio 2010。

2 (2)

选择文件>新建项目。

3 (3)

确保选择了.NET Framework 3.5。

4 (4)

在“ Visual C#”>“ SharePoint”>“ 2010”下,选择“空SharePoint项目”。

5 (5)

提供解决方案的名称和位置,在演示中我们使用CustomPageHead。

6 (6)

提供用于调试解决方案的本地SharePoint网站的URL,为进行演示,我们使用 http://win7 (the URL of my local SharePoint 2010 site). http:// win7 (我本地SharePoint 2010网站的URL)。

7 (7)

选择“部署为服务器场解决方案”。 由于我们将用户控件部署到SharePoint 14 HIVE(本地硬盘驱动器),因此它必须是服务器场解决方案。

8 (8)

选择完成。

9 (9)

接下来,我们将自定义CSS样式表和JavaScript文件添加到解决方案中。 出于演示目的,我们将在Visual Studio中创建它们,但是您可以添加自己的文件。

10 (10)

在解决方案资源管理器中右键单击解决方案名称(CustomPageHead)。

11 (11)

选择添加> SharePoint“布局”映射的文件夹。 这将创建具有与解决方案(CustomPageHead)同名的子文件夹的Layouts文件夹。 必须将自定义资产(例如JS和CSS文件)放在具有唯一名称的子文件夹或另一个子文件夹中,以防止文件与内置SharePoint文件或其他已安装的解决方案冲突。

12 (12)

右键单击“布局”下的CustomPageHead文件夹。

13 (13)

选择添加>新建项目。

14 (14)

在“ Visual C#”>“ Web”下,选择“ JScript文件”。

15 (15)

为新JavaScript文件提供一个名称,在此演示中,我们使用“ custom.js”。

16 (16)

选择添加。

17 (17)

右键单击“布局”下的CustomPageHead文件夹。

18 (18)

选择添加>新建项目。

19 (19)

在“ Visual C#”>“ Web”下,选择“样式表”。

20 (20)

为新CSS文件提供一个名称,在此演示中,我们使用“ custom.css”。

21 (21)

选择添加。

22 (22)

为确保解决方案在测试时能正常工作,我们将使用custom.css样式表来更改背景颜色。 在custom.css文件中,将其内容替换为:
body
{
		background-color: #777777;
}

23 (23)

如果custom.js和custom.css文件仍然打开,请关闭它们。

24 (24)

在解决方案资源管理器中右键单击解决方案名称(CustomPageHead)。

25 (25)

选择添加> SharePoint映射文件夹...

26 (26)

展开TEMPLATE文件夹,然后选择CONTROLTEMPLATES。 在SharePoint中存储用户控件的指定位置是CONTROLTEMPLATES文件夹。 与“布局”文件夹不同,不会自动创建一个子文件夹。

27 (27)

用鼠标右键单击CONTROLTEMPLATES文件夹。

28 (28)

选择添加>新建文件夹。

29 (29)

将该文件夹命名为与Layouts文件夹(CustomPageHead)中的子文件夹相同的名称。

30 (30)

用鼠标右键单击CONTROLTEMPLATES的CustomPageHead子文件夹。

31 (31)

选择添加>新建项目。

32 (32)

在“ Visual C#”>“ SharePoint”>“ 2010”下,选择“用户控件”。

33 (33)

为用户控件提供一个文件名(CustomPageHead.ascx)。

34 (34)

选择添加。

35 (35)

在解决方案资源管理器中展开CustomPageHead.ascx文件。

36 (36)

双击CustomPageHead.ascx.cs文件以将其打开。

37 (37)

添加对Microsoft.SharePoint命名空间的引用。
using Microsoft.SharePoint;

38 (38)

在Page_Load部分中,添加以下内容以获取当前站点的URL:
string SiteURL = SPContext.Current.Web.Url;

39 (39)

在下一行中,添加以下内容以注册custom.js客户端脚本:
this.Page.ClientScript.RegisterClientScriptBlock(
	this.GetType(), "custom.js", SiteURL + "/_layouts/CustomPageHead/custom.js");

40 (40)

对于所有其他类型的<head>节标记(即样式表和meta标记),将标记的内容置于OnInit函数的替代中:
protected override void OnInit(EventArgs e)
{
	Page.Init += delegate(object sender, EventArgs e_Init)
	{
		string SiteURL = SPContext.Current.Web.Url;
                Page.Header.Controls.Add(new LiteralControl(
			"<link rel=\"stylesheet\" type=\"text/css\" href=\""
			+ SiteURL + "/_layouts/CustomPageHead/custom.css\" />"));
                Page.Header.Controls.Add(new LiteralControl(
			"<meta name=\"keywords\" content=\"sharepoint,c-sharp\" />"));
	};
	base.OnInit(e);
}

41 (41)

完整的CustomPageHead.ascx.cs文件:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace CustomPageHead.CONTROLTEMPLATES.CustomPageHead
{
    public partial class CustomPageHead : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string SiteURL = SPContext.Current.Web.Url;
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "custom.js", SiteURL + "/_layouts/CustomPageHead/custom.js");
        }
        protected override void OnInit(EventArgs e)
        {
            Page.Init += delegate(object sender, EventArgs e_Init)
            {
                string SiteURL = SPContext.Current.Web.Url;
                Page.Header.Controls.Add(new LiteralControl(
                    "<link rel=\"stylesheet\" type=\"text/css\" href=\""
                    + SiteURL + "/_layouts/CustomPageHead/custom.css\" />"));
                Page.Header.Controls.Add(new LiteralControl(
                    "<meta name=\"keywords\" content=\"sharepoint,c-sharp\" />"));
            };
            base.OnInit(e);
        }
    }
}

42 (42)

关闭所有打开的文档。

43 (43)

在解决方案资源管理器中右键单击解决方案名称(CustomPageName)。

44 (44)

选择添加>新建项目。

45 (45)

在“ Visual C#”>“ SharePoint”>“ 2010”下,选择“空元素”。

46 (46)

为Empty Element命名(CustomPageHead),然后选择Add。

47 (47)

用以下对CustomPageHead用户控件的引用替换Elements.xml文件的内容(这告诉SharePoint用CustomPageHead用户控件替换AdditionalPageHead内容占位符):
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead"
           ControlSrc="~/_controltemplates/CustomPageHead/CustomPageHead.ascx" 
           Sequence="12"/>
</Elements>

48 (48)

创建Empty Element时,会自动添加一个功能文件(Feature1.feature)。 双击Feature1.feature文件将其打开。

49 (49)

给功能标题和描述。 它们都显示在“站点操作”>“站点功能”页面上,在该页面上已激活和禁用了该功能。

50 (50)

将范围设置为Web。 SharePoint中的网站是单个SharePoint网站,而网站是SharePoint网站集。

51 (51)

解决方案完成后,在解决方案资源管理器中右键单击解决方案名称(CustomPageHead)。

52 (52)

选择部署。

53 (53)

该解决方案现在正在部署到您的测试SharePoint网站。 部署结果将显示在屏幕底部的“输出”窗口中。

54 (54)

部署完成后,请访问测试SharePoint网站以验证其是否正常运行。

Now the solution can be packaged up and deployed to  your other SharePoint farms and sites as necessary.  This solution can be customized to add just about any tags to the <head> sections of each SharePoint page.

现在,可以将解决方案打包并根据需要部署到其他SharePoint场和网站。 可以自定义此解决方案,以将几乎所有标签添加到每个SharePoint页面的<head>部分。

Article Source 文章来源

翻译自: https://www.experts-exchange.com/articles/6250/SharePoint-2010-Programmatically-Add-JavaScript-Meta-Tags-and-CSS-Styles-to-the-Header-of-Every-Page.html

sharepoint 编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值