最近在做项目的过程中,遇到个问题,创建一个SharePoint站点,在站点首页(default.aspx)添加了几个列表WebPart(ListViewWebPart),WebPart选用了自定义的视图,并将列表WebPart工具栏类型设置成“没有工具栏”,整体的效果如下图:
![](https://www.cnblogs.com/images/cnblogs_com/nirvanalst/090228-01.gif)
接着将SharePoint站点另存成模板,然后基于此模板创建个新站点,结果新站点创建完成后,原来模板中首页WebPart所应用的视图和工具栏类型都恢复到默认的样子,如下图:
![](https://www.cnblogs.com/images/cnblogs_com/nirvanalst/090228-02.gif)
这样下来又要从头到尾的手动调整WebPart的样式,想想有些崩溃。这样的站点要通过程序创建200多个,手动调整起来只能吐血了。于是开始研究如何通过代码来完成调整的任务,让他创建完站点以后自动样式调整。基本的实现思路如下:
1、首先找到首页(default.aspx)的WebPart管理对象(SPLimitedWebPartManager).
2、用SPLimitedWebPartManager搞到页面里所有的WebPart。
3、过滤掉非列表WebPart(ListViewWebPart)。
4、剩下的开始逐一处理,先调整视图,在调整它的工具栏类型。
5、最后保存调整的结果。
实现的代码如下:
实现方法
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// 修改首页WerPart样式及视图
3
/// </summary>
4
/// <param name="currentWeb">大客户站点</param>
5
public void ChangeWebPartLayout(SPWeb currentWeb)
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7
SPSecurity.RunWithElevatedPrivileges(delegate()
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
SPSite siteClt = new SPSite(currentWeb.Site.ID);
10
SPWeb managementWeb = siteClt.AllWebs["ITSImportanceCustomerManagement"]; //需要以用Mode中的变量
11
SPWeb customWeb = managementWeb.Webs[currentWeb.ID];
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
//客户年度重点项目计划
14
SPList ITSKeyProjectPlanList = this.GetListFromUrl(customWeb, "ITSKeyProjectPlan");
15
//重点项目追踪
16
SPList ITSKeyProjectTrackingList = this.GetListFromUrl(customWeb, "ITSKeyProjectTracking");
17
//客户年度普通项目计划
18
SPList ITSGeneralProjectPlanList = this.GetListFromUrl(customWeb, "ITSGeneralProjectPlan");
19
//普通项目追踪
20
SPList ITSGeneralProjectTrackingList = this.GetListFromUrl(customWeb, "ITSGeneralProjectTracking");
21
//客户动态追踪
22
SPList ITSClientDynamicTrackingList = this.GetListFromUrl(customWeb, "ITSClientDynamicTracking");
23
//客户接触记录
24
SPList ITSClientContactRecordList = this.GetListFromUrl(customWeb, "ITSClientContactRecord");
25
//大客户团队信息
26
SPList ITSClientTeamList = this.GetListFromUrl(customWeb, "ITSClientTeam");
27
//大客户团队工作
28
SPList ITSClientTeamWorkLogList = this.GetListFromUrl(customWeb, "ITSClientTeamWorkLog");
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
SPLimitedWebPartManager webPartManager = customWeb.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
customWeb.AllowUnsafeUpdates = true;
33
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartManager.WebParts)
34![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
if (wp is ListViewWebPart)
36![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
ListViewWebPart lv = wp as ListViewWebPart;
38
switch (lv.DisplayTitle)
39![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
//重点项目计划
41
case "客户年度重点项目计划":
42![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
this.SaveWebPartChanges(ITSKeyProjectPlanList, lv, webPartManager);
44
}
45
break;
46
//重点项目追踪
47
case "重点项目追踪":
48![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
this.SaveWebPartChanges(ITSKeyProjectTrackingList, lv, webPartManager);
50
}
51
break;
52
//普通项目计划
53
case "客户年度普通项目计划":
54![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
this.SaveWebPartChanges(ITSGeneralProjectPlanList, lv, webPartManager);
56
}
57
break;
58
//普通项目追踪
59
case "普通项目追踪":
60![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
this.SaveWebPartChanges(ITSGeneralProjectTrackingList, lv, webPartManager);
62
}
63
break;
64
//客户动态追踪
65
case "客户动态追踪":
66![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
this.SaveWebPartChanges(ITSClientDynamicTrackingList, lv, webPartManager);
68
}
69
break;
70
//客户接触记录
71
case "客户接触记录":
72![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
this.SaveWebPartChanges(ITSClientContactRecordList, lv, webPartManager);
74
}
75
break;
76
//客户团队信息
77
case "大客户团队信息":
78![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
this.SaveWebPartChanges(ITSClientTeamList, lv, webPartManager);
80
}
81
break;
82
//客户团队工作
83
case "大客户团队工作":
84![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
this.SaveWebPartChanges(ITSClientTeamWorkLogList, lv, webPartManager);
86
}
87
break;
88
}
89
}
90
}
91
customWeb.Update();
92
customWeb.AllowUnsafeUpdates = false;
93
});
94
}
95![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
96![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
97
/// 得到当前站点指定URL地址的列表
98
/// </summary>
99
/// <param name="web">列表所在站点</param>
100
/// <param name="listShortUrl">列表URL名(例:ITSKeyProjectPlan)</param>
101
/// <returns></returns>
102
public SPList GetListFromUrl(SPWeb web, string listShortUrl)
103![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
104
SPList list = null;
105
if ((web != null) && !string.IsNullOrEmpty(listShortUrl))
106
list = web.GetListFromUrl(string.Format("/ITSImportanceCustomerManagement/{0}/Lists/{1}/AllItems.aspx",web.Name,listShortUrl));
107
return list;
108
}
109![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
110![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
111
/// 保存对ListViewWebPart的修改。
112
/// 此方法只为ChangeWebPartLayout(SPWeb currentWeb)方法提供服务。
113
/// </summary>
114
/// <param name="list">ListViewWebPart所引用的列表</param>
115
/// <param name="lv">ListViewWebPart对象</param>
116
/// <param name="webPartManager">当前页面WebPartManager对象</param>
117
private void SaveWebPartChanges(SPList list,ListViewWebPart lv,SPLimitedWebPartManager webPartManager)
118![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
119
SPView homeView = list.Views["首页视图"];
120
//得到 客户重点项目计划WebPart当前使用的视图
121
SPView lvView = list.Views[new Guid(lv.ViewGuid)];
122
//清除视图所有的显示栏
123
lvView.ViewFields.DeleteAll();
124
foreach (string field in homeView.ViewFields)
125
lvView.ViewFields.Add(field);
126
lvView.GroupByHeader = homeView.GroupByHeader;
127
lvView.GroupByFooter = homeView.GroupByFooter;
128
lvView.RowLimit = homeView.RowLimit;
129
lvView.Query = homeView.Query;
130
lvView.ViewEmpty = homeView.ViewEmpty;
131
lvView.ViewHeader = homeView.ViewHeader;
132
lvView.ViewFooter = homeView.ViewFooter;
133
lvView.Scope = homeView.Scope;
134
lvView.Update();
135![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
//代码去除视图的工具栏
137
System.Reflection.PropertyInfo ViewProp = lv.GetType().GetProperty("View", BindingFlags.NonPublic | BindingFlags.Instance);
138
SPView spView = ViewProp.GetValue(lv, null) as SPView;
139
string txt = spView.SchemaXml;
140
System.Reflection.PropertyInfo nodeProp = spView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
141
XmlNode node = nodeProp.GetValue(spView, null) as XmlNode;
142
XmlNode tBarNode = node.SelectSingleNode("Toolbar");
143
if (tBarNode != null)
144![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
XmlAttribute typeNode = tBarNode.Attributes["Type"];
146
tBarNode.RemoveAll();
147
tBarNode.Attributes.Append(typeNode);
148
typeNode.Value = "None";
149
}
150
spView.Update();
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
webPartManager.SaveChanges(lv);
153
}
搞个命令行程序测试一下:
Code
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("http://liust-server:100");
SPWeb icmWeb = site.AllWebs["ITSImportanceCustomerManagement"];
SPWeb customWeb = icmWeb.Webs["DKHWebTemplate"];
Program p = new Program();
p.ChangeWebPartLayout(customWeb);
Console.Write("ok");
Console.ReadKey();
}
}
至此,这个问题就搞定了,可以休息一下了。