SharePoint 代码添加内容类型

 

 

   SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPContentTypeCollection collection = web.ContentTypes;//遍历已有的内容类型
                       
                        SPList list = web.Lists["基础数据收集库"];
                        web.AllowUnsafeUpdates = true;
                        foreach (SPContentType item in collection)
                        {
                            list.ContentTypes.Add(item);
                           
                        }

创建内容类型
                        //string[] str = {
                        //                   "1", "2", "3", 
                        //                   "4", "5", "6表"};
                        //SPContentType contentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Item];
                        //web.AllowUnsafeUpdates = true;
                        //创建一个新的内容类型——客户,派生自项目.
                        //SPContentType parentCType = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];
                        //SPList list = web.Lists["test"];

                        //list.ContentTypesEnabled = true;
                        //for (int i = 0; i < str.Length; i++)
                        //{
                        //    SPContentType childCType = new SPContentType(parentCType, web.ContentTypes, str[i]);
                        //    childCType.Group = "自定义内容类型";
                        //    // 将新的内容联系添加到网站内容类型集合
                        //    childCType = web.ContentTypes.Add(childCType);
先将模板上传到在_cts目录下的<你的内容类型>文件夹下 ,可以通过DocumentTemplate获取默认模板路径 ,也可以上传到指定的文档库                      
  //childCType.DocumentTemplate = "新建 Microsoft Excel 工作表.xlsx";
                        //    childCType.Update();
                        //    // 添加到一个列表.
                        //    list.ContentTypes.Add(childCType);
                        //}
                       
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });

 

posted on 2015-05-27 23:17  !无名之辈  阅读(186)  评论(0)    收藏  举报