SharePoint 2013 Create Folder with conententtype programer

Posted 云霄宇霁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Create and Call HttpHandler in SharePoint

重定向sharepoint新表单

SharePoint 2016 图文安装教程

BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 SharePoint 2013 平台

Office Online Server部署--和SharePoint绑定

SharePoint Online: Create Multiple Lists from a CSV File using PowerShell