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

希望对大家有所帮助!

posted @ 2017-01-11 09:09  云霄宇霁  阅读(234)  评论(0编辑  收藏  举报