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

問題は、追加される contenttype がデフォルトでタイプではなく Document に基づいていたためです。

その部分をこれに変更しました

  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 に答える