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 = ;
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

SharePoint自动初始化网站列表的更多相关文章

  1. Qt中新建类构造函数的初始化参数列表

    使用Qt-creator自动生成一个窗体应用程序时会自动创建一个新的类,我的程序中名为MyDialog,类的定义为: #ifndef MYDIALOG_H #define MYDIALOG_H #in ...

  2. SharePoint 2013 跨网站集发布功能简介

    在SharePoint Server 2013网站实施中,我们经常会遇到跨网站集获取数据,而2013的这一跨网站集发布功能,正好满足我们这样的需求. 使用SharePoint 2013中的跨网站发布, ...

  3. 采用异步来实现重新连接服务器或者重新启动服务 C#中类的属性的获取 SignalR2简易数据看板演示 C#动态调用泛型类、泛型方法 asp .net core Get raw request. 从壹开始前后端分离[.NetCore 不定期更新] 38 ║自动初始化数据库

    采用异步来实现重新连接服务器或者重新启动服务 开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端! #region 检测断线并重连OPC服务 ...

  4. SQL参数化查询自动生成SqlParameter列表

    string sql = @"INSERT INTO stu VALUES (@id,@name) "; 参数化查询是经常用到的,它可以有效防止SQL注入.但是需要手动去匹配参数@ ...

  5. .NET中那些所谓的新语法之一:自动属性、隐式类型、命名参数与自动初始化器

    开篇:在日常的.NET开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...

  6. 自己实现简单的AOP(四)自动初始化代理对象

    前面三篇随笔,已经完成了AOP的核心功能,但 代理对象的初始化还是有些麻烦,本文将解决该问题. Demo 片段如下: public class HomeController : Controller ...

  7. Delphi结构体的扩展,可以自动初始化,反初始化,自定义拷贝函数.

    转载:http://www.raysoftware.cn/?p=518&utm_source=tuicool 恭贺Delphi XE7诞生,Delphi XE7在编译器内部集成了我之前所实现的 ...

  8. SpringMVC 初始化网站静态信息

    在网站开发中,一些元素经常被访问,例如 网页头部URL导航 的信息,以及Boot版权的信息,在各个页面都是重复出现的 如果每次渲染View都要通过Service层访问数据库 比较麻烦 也没有必要,但是 ...

  9. Java 专业人士必备的书籍和网站列表

    对于 Java™ 语言开发人员来说,信息过量是一个真正的问题.每个新入行的程序员都要面临一个令人畏缩的挑战:要进入的行业是一个具有海量知识的行业.要了解的东西简直 太多了.对于有经验的老手来说,情况只 ...

随机推荐

  1. 百万级数据 MySQL处理(转)

    转自 http://www.cnblogs.com/win7xt/p/3156334.html 使用MySQL处理百万级以上数据时,不得不知道的几个常识   最近一段时间参与的项目要操作百万级数据量的 ...

  2. HTML利用posotion属性定位 小技巧

    1.居中效果 父级DIV (index-top )属性设置为 text-align:center; 子级DIV( tabIndex-main)属性设置为 margin:0 auto;   2.左右对齐 ...

  3. IDEA操作maven时控制台中文乱码

    windows环境下,Intellij idea12中maven操作时,控制台中文乱码问题(编译报错或者clean install时出现的其他错误描述乱码) 在cmd中mvn中文正常显示,log4j打 ...

  4. ArcGIS for JavaScript继承TiledMapServiceLayer来实现“动态切图”

    这种方式可以提高出图速度于效果,算法见http://blog.newnaw.com/?p=633,我用ArcGIS for JavaScript API来实现.具体代码为: function init ...

  5. 用flutter写一个精美的登录页面

    先看效果图: 源代码已上传到github 我们先看一下页面 , 首先这个页面,我们并没有用到AppBar,当然也就没有自带返回功能.然后下面有个Login的文字以及一条横线. 屏幕中上方是填写帐号以及 ...

  6. 观察者设计模式( Observable类Observer接口)

    如果要想实现观察者模式,则必须依靠Java.util包中提供的Observable类和Observer接口. class House extends Observable{ // 表示房子可以被观察  ...

  7. Phoenix介绍(持续更新)

    现有hbase的查询工具有很多如:Hive,Tez,Impala,Shark/Spark,Phoenix等.今天主要记录Phoenix. phoenix,中文译为“凤凰”,很美的名字.Phoenix是 ...

  8. 实验二:klee处理未建模函数和处理error的方式

    首先,能够分析klee源码固然重要.但是目前尚未到那个地步.我按照我的过程,记录和分析我所做的实验. 结论性内容是: 1.klee处理printf传入符号值的情形时,报为error,不会将符号值具体化 ...

  9. 我是如何将linux用在开发环境中的

    1.我为什么要写这篇文章 一直想深入学习一下linux的使用,于是将家里的笔记本装了linux系统,但是要将自己的系统打造一个适合开发的环境确实是一件费心费力的事,而且会经常出现一些莫名其妙的问题,以 ...

  10. 企业级NFS网络文件共享服务_【all】

    1.1. 什么是NFS(1台机器提供服务) Network File System(网络文件系统)通过局域网让不同的主机系统之间共享文件或目录. NFS客户端可以通过挂载的方式将NFS服务器端共享的数 ...