1,由于目前的SharePoint网站需要部署到多个服务器上,每个网站的内容都不一样,所以使用备份还原是不可以的。常用的方式便是将列表导出为列表模版,然后将列表模版复制到服务器上,根据列表模版创建列表。由于网站中的列表比较多,需要部署多套项目,这项工作就变成了很无聊的一项工作。因此通过编程的方式自动创建所有列表。
2,请看代码(我是用控制台程序创建的列表)
(1)主函数
static void Main(string[] args) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite("http://192.168.1.124/sites/CustomWeb")) { using (SPWeb web = site.OpenWeb()) { Console.WriteLine("准备在" + web.Url + "站点上创建列表"); web.AllowUnsafeUpdates = true; SPListTemplate customTemplate = GetListTemplate(site, web); if (customTemplate != null) { Console.WriteLine("第(1)步:开始创建《文章模版》列表..."); if (CreateArticleTemplate(customTemplate, web)) { Console.WriteLine("PS:《文章模版》列表创建成功!"); BatchCreateList(web, customTemplate); } else { Console.WriteLine("PS:创建《文章模版》列表过程中出现错误"); } } else { Console.WriteLine("PS:没有找到合适的模版"); } web.AllowUnsafeUpdates = false; } } }); Console.WriteLine("*******************************************************"); Console.WriteLine("列表全部创建完成"); Console.ReadLine(); }
(2)获取列表模版
private static SPListTemplate GetListTemplate(SPSite site, SPWeb web) { SPListTemplate CustomTemplate = null; try { SPListTemplateCollection ListTemplateCollection = site.GetCustomListTemplates(web); foreach (SPListTemplate template in ListTemplateCollection) { if (template.InternalName == "customTemp.stp") { CustomTemplate = template; break; } } } catch (Exception) { CustomTemplate = null; } return CustomTemplate; }
(3)创建文章模版列表,该表作为其他列表的外键表,以此创建LookUp类型字段
private static bool CreateArticleTemplate(SPListTemplate customTemplate, SPWeb web) { bool flag = false; try { //创建列表 Guid newListGuid = web.Lists.Add("ArticleTemplate", "文章模版列表", customTemplate); SPList newList = web.Lists[newListGuid]; newList.Title = "文章模版"; //创建字段 string result = newList.Fields.Add("Template", SPFieldType.Text, false); //更改字段英文名为中文 SPField sf_result = newList.Fields["Template"]; if (sf_result != null) { sf_result.Title = "模版"; } sf_result.Update(); newList.Update(); //初始化数据 SPListItem itemWord = newList.AddItem(); itemWord["Title"] = "word展示"; itemWord["Template"] = "<div id="OfficeDiv"><div id="FrameDiv">$word</div></div>"; itemWord.Update(); SPListItem itemPic = newList.AddItem(); itemPic["Title"] = "先图再文"; itemPic["Template"] = "<div class=newimg>$img </div>$content<p>$editor</p>"; itemPic.Update(); flag = true; } catch (Exception) { } return flag; }
(4)初始化列表数据
private static Dictionary<string, string> InitData() { Dictionary<string, string> dicInit = new Dictionary<string, string>(); //德育处列表 dicInit.Add("MoralDynamic", "德育动态"); dicInit.Add("MainEducation", "主题教育"); dicInit.Add("PlanSummary", "计划总结"); dicInit.Add("BodyHeart", "育体育心"); dicInit.Add("HealthDynamic", "卫生动态"); return dicInit; }
(5)批量创建列表
private static void BatchCreateList(SPWeb web, SPListTemplate customTemplate) { Dictionary<string, string> dics = InitData(); SPList spList = web.Lists.TryGetList("文章模版"); int flag = 2; try { foreach (KeyValuePair<string, string> dic in dics) { Console.WriteLine("第(" + flag + ")步:开始创建《" + dic.Value + "》列表..."); Guid newListGuid = web.Lists.Add(dic.Key, dic.Value + "列表", customTemplate); SPList newList = web.Lists[newListGuid]; newList.Title = dic.Value; //创建正文字段 string mainbody = newList.Fields.Add("MainBody", SPFieldType.Text, false); SPField sf_mainbody = newList.Fields["MainBody"]; if (sf_mainbody != null) { sf_mainbody.Title = "正文"; } sf_mainbody.Update(); //创建访问数量字段 string count = newList.Fields.Add("Count", SPFieldType.Text, false); SPField sf_count = newList.Fields["Count"]; if (sf_count != null) { sf_count.Title = "访问数量"; } sf_count.Update(); //创建模版字段 Guid lookupGuid = new Guid(spList.ID.ToString()); string template = newList.Fields.AddLookup("Template", lookupGuid, false); SPFieldLookup sf_lookupGuid = newList.Fields["Template"] as SPFieldLookup; //绑定数据List到Lookup字段 sf_lookupGuid.LookupField = spList.Fields["标题"].StaticName; //SPField sf_lookupGuid = newList.Fields["Count"]; sf_lookupGuid.Title = "模版"; sf_lookupGuid.Update(); newList.Update(); Console.WriteLine("PS:《" + dic.Value + "》列表创建成功"); flag++; } } catch (Exception ex) { Console.WriteLine("PS:批量创建列表过程失败!!!"); } }
参考博客:http://blog.csdn.net/qq873113580/article/details/22668833