记录一下昨天写的SharePoint tool,需求是这样的:

在SharePoint list subfolder 下创建1000个folder,这些folder指定特殊的contenttype,contenttype中有一个Taxonomy类型的field,为这个filed赋值,并且新创建的这些folder下只能创建指定contenttype的file。

创建folder的代码如下:

 public static void CreatFolder(string siteUrl)//传入site url
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("your list title");
if (list != null)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
web.AllowUnsafeUpdates = true;
SPFolder BAFolder = list.RootFolder.SubFolders.Cast<SPFolder>().
FirstOrDefault(f => f.Name.Equals("your folder name or root folder", StringComparison.OrdinalIgnoreCase));
if (BAFolder.Exists)
{
TaxonomyFieldValue taxonomyFieldValue = GetTaxonomyFieldValue(site, list);
for (int i = ; i < ; i++)
{
string gvName = "GV" + i;
SPListItem newItem = list.Items.Add(BAFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, gvName);
newItem[SPBuiltInFieldId.ContentTypeId] = list.ContentTypes["your content type name"].Id;
newItem.Update();
newItem["Title"] = gvName;
//为Taxonomy field 赋值
newItem["your taxonomy field internal name"] = taxonomyFieldValue;
newItem.Update();
list.Update();
//为新创建的folder指定UniqueContentTypeOrder即在new folder下只能创建UniqueContentTypeOrder中包含的contenttype类型的file or folder
SPFolder gvFolder = newItem.Folder;
Collection<SPContentType> gvFolderOrderedContentTypes = new Collection<SPContentType>();
for (int j = ; j < list.ContentTypes.Count; j++)
{
if (list.ContentTypes[j].Name == "your contenttype name")
{
gvFolderOrderedContentTypes.Add(list.ContentTypes[j]);
}
}
gvFolder.UniqueContentTypeOrder = gvFolderOrderedContentTypes;
gvFolder.Update();
}
}
web.AllowUnsafeUpdates = false;
});
}
}
}
}
 private static TaxonomyFieldValue GetTaxonomyFieldValue(SPSite site, SPList list)
{
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores["Managed Metadata Service"];
Group group = termStore.Groups["group name"];
TermSet termSet = group.TermSets["termset name"];
Term term = termSet.Terms["term name"];

//获取TaxonomyFieldValue
TaxonomyField taxonomyField = list.Fields["your taxonomy field name"] as TaxonomyField;
TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
taxonomyFieldValue.TermGuid = term.Id.ToString();
taxonomyFieldValue.Label = term.Name;
return taxonomyFieldValue;
}

希望对大家有所帮助!

最新文章

  1. 比官方教程代码更简短的SignalR Server Broadcast示例
  2. hdu 1241 Oil Deposits(水一发,自我的DFS)
  3. HTML-003-模拟IDE代码展开收起功能简单示例
  4. setblendstate &amp; setdepthstencilstate
  5. VS编辑代码的时候,都会自动在资源浏览器里将文件所在项目展开
  6. 用Django Rest Framework和AngularJS开始你的项目
  7. AWS re:Invent(2019.01.09)
  8. js中的call、apply、bind
  9. Mybatis面试题整理(超详细)
  10. 展示博客---Alpha版本展示
  11. sip (db33)信令交互-视频点播与回播
  12. char *s="string"和char s[]="string"的区别
  13. gb2312提交的url编码转换成utf8的查询
  14. Hibernate or 的用法
  15. CMMI的敏捷开发
  16. pom.xml实例
  17. day09-数据库插入中文报错
  18. 解决“tar:Exiting with failure status due to previous errors”【转】
  19. NSObject协议中方法:description 和 debugDescription
  20. Oracle EBS 查询物料报错

热门文章

  1. Python 基础1 - 位运算符
  2. Codeforces 396C
  3. (五十三)c#Winform自定义控件-滚动文字
  4. 通过PHP与Python代码对比浅析语法差异
  5. VS Code 配置 Python 开发环境
  6. Codeforces Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力
  7. CF1198E Rectangle Painting 2(最小割 思维
  8. 编码神器Lombok,学会后开发效率至少提高一倍!
  9. 【Offer】[50-1] 【第一个只出现一次的字符】
  10. Python 的整数与 Numpy 的数据溢出