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. <Android 基础(二十五)> View Animation

    简介 视图动画,主要包括位移,透明度,旋转和缩放,View本身的属性并没有发生变化,只是在这个视图上添加一些渐变的效果,所以总体而言,视图动画只能实现一些简单的动画效果,属性动画功能更强大. 使用 r ...

  2. 行动学习方法----PARR

  3. windows如何查看nvidia显卡(GPU)的利用率和温度

    windows如何查看nvidia显卡(GPU)的利用率和温度 nvidia-smi 只要在文件夹C:\Program Files\NVIDIA Corporation\NVSMI里找到文件nvidi ...

  4. MySQL语句应该注意的问题

    刚刚开始写mysql语句 比较繁琐,乱哄哄的,总结了几点应该注意的事项: 1 注意标点符号,在创建表格 create table的时候  括号里面包含的内容用“,”号分割开,最后一条语句不要加标点符号 ...

  5. 填报表导出excel后不可写的单元格处于锁定状态

     填报表单元格分为可写和不可写两种状态,当填报表在web上展现的时候可写单元格可以进行数据填报和修改,非可写单元格不可操作. 报表导出为excel时,润乾导出excel包默认情况下不对excel单 ...

  6. git学习——简介、使用(一)

    本文是作者参考其他教程学习git的记录,原文:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c01 ...

  7. WiFi 干扰器,有时间可以去试试呦!

    转自社区: 0X01 引言 想不想搞个WIFI干扰器?网上搜集了一下资料,发现用esp8266可以实现简单的干扰功能,包括断网.复制.欺骗等等.刚好手上有块Tpyboard V202(30元),也是e ...

  8. SQLSERVER的 筛选索引(Fiter Index)

    fiter index(筛选索引)是SQL Server的一项功能,可使此数据库与众不同. 筛选索引的概念 SQL Server中常用的索引是一种物理结构,它包含来自所有行的一组选定列的值 在一张桌子 ...

  9. C#中关于增强类功能的几种方式

    C#中关于增强类功能的几种方式 本文主要讲解如何利用C#语言自身的特性来对一个类的功能进行丰富与增强,便于拓展现有项目的一些功能. 拓展方法 扩展方法被定义为静态方法,通过实例方法语法进行调用.方法的 ...

  10. teradata 查询创建表的时间

    如何查询teradata创建表的时间? select tablename,CreateTimeStamp from dbc.tables ---dbc的table,user是在dbc内的吗?具体不清楚 ...