首页 > 代码库 > SharePoint 2013 Create Folder with conententtype programer
SharePoint 2013 Create Folder with conententtype programer
记录一下昨天写的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 = 100000; i < 101001; 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 = 0; 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; }
希望对大家有所帮助!
SharePoint 2013 Create Folder with conententtype programer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。