1

当我尝试通过代码创建内容类型和列表时,新表单我看不到新字段。我不知道还要检查什么,我在其他博客文章的基础上构建了这段代码。

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            Logger.LogDebug("NLSponsoringListSalesNumbersEventReceiver", "FeatureActivated(SPFeatureReceiverProperties properties)", "BEGIN");
            try
            {
                SPSite currentSite = properties.Feature.Parent as SPSite;
                SPWeb currentWeb = currentSite.RootWeb;
                AddSourcesList(currentWeb);



     private void AddSourcesList(SPWeb currentWeb)
        {
            currentWeb.AllowUnsafeUpdates = true;

            #region Add Source content type if it doesnt exist.
                SPContentType sourceContentType = currentWeb.ContentTypes[SponsoringCommon.Constants.CONTENTTYPES_SOURCES_NAME];
                SPContentTypeId sourceContentTypeid = new SPContentTypeId(SponsoringCommon.Constants.CONTENTTYPES_SOURCE_ID);
                if (sourceContentType == null)
                {
                    sourceContentType = new SPContentType(sourceContentTypeid,
                                                            currentWeb.ContentTypes,
                                                            SponsoringCommon.Constants.CONTENTTYPES_SOURCES_NAME);
                    currentWeb.ContentTypes.Add(sourceContentType);
                    currentWeb.Update();
                }
            #endregion

            #region Removes list if it exists
                if (currentWeb.Lists.TryGetList(SponsoringCommon.Constants.LISTNAMES_SOURCES_NAME) != null)
                {
                    currentWeb.Lists.TryGetList(SponsoringCommon.Constants.LISTNAMES_SOURCES_NAME).Delete();
                    currentWeb.Lists.Add(SponsoringCommon.Constants.LISTNAMES_SOURCES_NAME, string.Empty, SPListTemplateType.GenericList);
                }
                else
                {
                    currentWeb.Lists.Add(SponsoringCommon.Constants.LISTNAMES_SOURCES_NAME, string.Empty, SPListTemplateType.GenericList);
                } 
            #endregion           

            #region Hides title column
            SPList sourceList = currentWeb.Lists.TryGetList(SponsoringCommon.Constants.LISTNAMES_SOURCES_NAME);
            SPField titleField = sourceList.Fields.GetField("Title");
            titleField.ShowInEditForm = false;
            titleField.ShowInDisplayForm = false;
            titleField.ShowInNewForm = false;
            titleField.Update(); 
            #endregion 

            string sourceFieldName = currentWeb.Fields.Add(SponsoringCommon.Constants.FIELDS_SOURCE_NAME, SPFieldType.Text, true);
            SPField sourceField = currentWeb.Fields.GetFieldByInternalName(sourceFieldName);
            sourceField.Group = "$Resources:SPNLSponsoring,Field_NationaleLoterijSponsoringColumns_Group";
            string schemaXmlWithResourceTokens = sourceField.SchemaXmlWithResourceTokens;
            int startIndex = schemaXmlWithResourceTokens.IndexOf("\"", schemaXmlWithResourceTokens.IndexOf("DisplayName=\"")) + 1;
            int endIndex = schemaXmlWithResourceTokens.IndexOf("\"", startIndex);
            int substringLength = endIndex - startIndex;
            string value = @"DisplayName=\" + schemaXmlWithResourceTokens.Substring(startIndex, substringLength);
            schemaXmlWithResourceTokens = schemaXmlWithResourceTokens.Replace(value, @"DisplayName=\$Resources:SPNLSponsoring,Field_Source_Name");
            sourceField.SchemaXml = schemaXmlWithResourceTokens;
            sourceField.ShowInEditForm = true;
            sourceField.ShowInNewForm = true;
            sourceField.ShowInDisplayForm = true;

            SPFieldLink fieldLinkSourceName = new SPFieldLink(sourceField);
            sourceContentType.FieldLinks.Add(fieldLinkSourceName);
            sourceContentType.FieldLinks.Reorder(new string[] { sourceField.InternalName });  //Reorder fields in the content type  
            sourceList.ContentTypesEnabled = true;
            sourceList.ContentTypes.Add(sourceContentType);
            sourceList.Update();

            SPContentTypeCollection currentOrderSourceList = sourceList.ContentTypes;
            List<SPContentType> resultSourceList = new List<SPContentType>();
            foreach (SPContentType ct in currentOrderSourceList)
            {
                if (ct.Name.Contains(SponsoringCommon.Constants.CONTENTTYPES_SOURCES_NAME))
                {
                    resultSourceList.Add(ct);
                }
            }
            sourceList.RootFolder.UniqueContentTypeOrder = resultSourceList;
            sourceList.RootFolder.Update();

            #region Modify view all items to show the new field
                SPView vwAllItemsSourceList = sourceList.GetSafeViewByName(SponsoringCommon.Constants.VIEWS_ALL_ITEMS);
                SPViewFieldCollection colvwAllItemsSourceList = vwAllItemsSourceList.ViewFields;
                if (vwAllItemsSourceList.ViewFields.Exists("LinkTitle"))
                {
                    vwAllItemsSourceList.ViewFields.Delete("LinkTitle");
                }
                colvwAllItemsSourceList.Add(sourceField);
                vwAllItemsSourceList.Update(); 
            #endregion

            currentWeb.AllowUnsafeUpdates = false ;
        }
4

1 回答 1

1

问题是因为添加的内容类型默认基于文档,而不是类型。

我把那部分改成了这个

  SPContentType itemCtype = currentWeb.AvailableContentTypes[SPBuiltInContentTypeId.Item];
                SPContentType sourceContentType = new SPContentType(itemCtype, currentWeb.ContentTypes, SponsoringCommon.Constants.CONTENTTYPES_SOURCES_NAME);
                sourceContentType = currentWeb.ContentTypes.Add(sourceContentType);

然后它工作正常

于 2012-07-25T11:18:02.763 回答