SharePoint自动初始化网站列表
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自动初始化网站列表的更多相关文章
- Qt中新建类构造函数的初始化参数列表
使用Qt-creator自动生成一个窗体应用程序时会自动创建一个新的类,我的程序中名为MyDialog,类的定义为: #ifndef MYDIALOG_H #define MYDIALOG_H #in ...
- SharePoint 2013 跨网站集发布功能简介
在SharePoint Server 2013网站实施中,我们经常会遇到跨网站集获取数据,而2013的这一跨网站集发布功能,正好满足我们这样的需求. 使用SharePoint 2013中的跨网站发布, ...
- 采用异步来实现重新连接服务器或者重新启动服务 C#中类的属性的获取 SignalR2简易数据看板演示 C#动态调用泛型类、泛型方法 asp .net core Get raw request. 从壹开始前后端分离[.NetCore 不定期更新] 38 ║自动初始化数据库
采用异步来实现重新连接服务器或者重新启动服务 开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端! #region 检测断线并重连OPC服务 ...
- SQL参数化查询自动生成SqlParameter列表
string sql = @"INSERT INTO stu VALUES (@id,@name) "; 参数化查询是经常用到的,它可以有效防止SQL注入.但是需要手动去匹配参数@ ...
- .NET中那些所谓的新语法之一:自动属性、隐式类型、命名参数与自动初始化器
开篇:在日常的.NET开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...
- 自己实现简单的AOP(四)自动初始化代理对象
前面三篇随笔,已经完成了AOP的核心功能,但 代理对象的初始化还是有些麻烦,本文将解决该问题. Demo 片段如下: public class HomeController : Controller ...
- Delphi结构体的扩展,可以自动初始化,反初始化,自定义拷贝函数.
转载:http://www.raysoftware.cn/?p=518&utm_source=tuicool 恭贺Delphi XE7诞生,Delphi XE7在编译器内部集成了我之前所实现的 ...
- SpringMVC 初始化网站静态信息
在网站开发中,一些元素经常被访问,例如 网页头部URL导航 的信息,以及Boot版权的信息,在各个页面都是重复出现的 如果每次渲染View都要通过Service层访问数据库 比较麻烦 也没有必要,但是 ...
- Java 专业人士必备的书籍和网站列表
对于 Java™ 语言开发人员来说,信息过量是一个真正的问题.每个新入行的程序员都要面临一个令人畏缩的挑战:要进入的行业是一个具有海量知识的行业.要了解的东西简直 太多了.对于有经验的老手来说,情况只 ...
随机推荐
- 2017年5月22日 HTML基础知识(一)
一.Html 结构 1.1.HTML基本文档格式—<html> 标记 —<html>文档的头部好和主体内容 </html> 根标记 —<head> 文 ...
- Ubuntu搜索不到WiFi的解决办法
时间:2018年1月25日 废话连篇:杭州下了第一场雪,冒险严寒来到实验室,打开电脑,纳尼连不上wifi了,好吧!不要被这件小事影响心情,开始修复了,经过一顿搜索,可能是因为驱动的问题,终端输入以下两 ...
- 【小程序】返回顶部wx.pageScrollTo和scroll-view的对比
一.wx.pageScrollTo(https://mp.weixin.qq.com/debug/wxadoc/dev/api/scroll.html) 1. 小程序中双击顶部的textbar.会默认 ...
- Git『Everything up-to-date』问题解决
今天提交代码的时候遇到了一个小问题,这里解决了记下小记. 提交代码遇到『Everything up-to-date』 上网查了下,发现中文大多答非所问,少数能解决的并没有阐述原理,所以我写这篇文章记录 ...
- sql 脚本 oracle scott 用户的四张表导入 mysql 中
/* 要先删除emp表,不能先删除dept表,因为dept有一个外键关联emp表*/drop TABLE emp;drop TABLE dept; drop TABLE salgrade;drop T ...
- ES6-Function
Function 箭头函数 ES6中对于函数的扩展最吸引人的莫过于箭头函数啦,不多说,先学会再说. 函数体内的this对象,是定义时所在的对象,而不是使用时所在的对象,这个特性与正常函数不同. // ...
- CMake中添加Qt模块的合理方法
https://www.jianshu.com/p/7eeb6f79a275 转载自这里 用CMake来组织的工程中要用Qt首先要设置.找到Qt相关模块.主要是通过find_package这个CMak ...
- 【Java】得到本机IP
import java.net.InetAddress; import java.net.UnknownHostException; public class MainProcess { public ...
- machine learning model(algorithm model) .vs. statistical model
https://www.analyticsvidhya.com/blog/2015/07/difference-machine-learning-statistical-modeling/ http: ...
- [EXCEL] 不能清除剪贴板: We couldn't free up space on the clipboard. Another program might be using it right now
Excel复制粘贴时出现以下错误,原因是有程序占用了剪切板. We couldn't free up space on the clipboard. Another program might be ...