2012-07-24 54 views
0

我想通过代码连接一个内容类型,(不要问为什么),大声笑。该功能可以正常工作,新列可以很好地添加到视图中。通过代码创建内容类型。新窗体是空的

由于某种原因,在NEW或编辑窗体中新的字段源名称不存在。当我转到列表设置,高级时,它表示管理内容类型=否,但我在代码上将其设置为是。

我没有想法。

private static void RemoveProductListAndCreateProductAndSourceList(SPWeb currentWeb) 
     { 
      currentWeb.AllowUnsafeUpdates = true; 
      #region Adds/Removes the product and source list 
       if (currentWeb.Lists.TryGetList(SponsoringCommon.Constants.LISTNAMES_PRODUCT_NAME) != null) 
       { 
        currentWeb.Lists.TryGetList(SponsoringCommon.Constants.LISTNAMES_PRODUCT_NAME).Delete(); 
        currentWeb.Lists.Add(SponsoringCommon.Constants.LISTNAMES_PRODUCT_NAME, string.Empty, SPListTemplateType.GenericList); 
       } 


       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 HIde the title columns 
       SPList productList = currentWeb.Lists.TryGetList(SponsoringCommon.Constants.LISTNAMES_PRODUCT_NAME); 
       SPField titleField = productList.Fields.GetField("Title"); 
       titleField.ShowInEditForm = false; 
       titleField.ShowInDisplayForm = false; 
       titleField.ShowInNewForm = false; 
       titleField.Update(); 

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

      #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); 
       } 
      #endregion 

      #region Removes the link from the content type column called Product Name 
       SPContentType productCT = currentWeb.ContentTypes[SponsoringCommon.Constants.CONTENTTYPES_PRODUCT_NAME]; 
       productCT.DeleteFieldRefFromContentType(currentWeb.Fields[SponsoringCommon.Constants.FIELDS_PRODUCT_NAMEOLD]);     
      #endregion 

      #region New fields properties 
       SPFieldCurrency amountField = (SPFieldCurrency)currentWeb.Fields.GetFieldByInternalName(SponsoringCommon.Constants.FIELDS_PRICE_NAME); 
       amountField.ShowInNewForm = true; 
       amountField.ShowInEditForm = true; 
       amountField.ShowInDisplayForm = true; 

       string productFieldName = currentWeb.Fields.Add(SponsoringCommon.Constants.FIELDS_PRODUCT_NAMENEW, SPFieldType.Text, true); 
       SPField productField = currentWeb.Fields.GetFieldByInternalName(productFieldName); 
       productField.Group = "$Resources:SPNLSponsoring,Field_NationaleLoterijSponsoringColumns_Group"; 
       string schemaXmlWithResourceTokens = productField.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_Product_Name"); 
       productField.SchemaXml = schemaXmlWithResourceTokens; 
       productField.ShowInNewForm = true; 
       productField.ShowInEditForm = true; 
       productField.ShowInDisplayForm = true; 

       SPFieldLink fieldLinkProductName = new SPFieldLink(productField); 
       SPFieldLink fieldLinkPriceName = new SPFieldLink(amountField); 
       productCT.FieldLinks.Add(fieldLinkProductName); 
       productCT.FieldLinks.Reorder(new string[] {productField.InternalName,amountField.InternalName}); //Reorder fields in the content type 
       productCT.Update(true);     

       productList.ContentTypesEnabled = true; 
       productList.ContentTypes.Add(productCT); 
       SPContentTypeCollection currentOrder = productList.ContentTypes; 
       List<SPContentType> result = new List<SPContentType>(); 
       foreach (SPContentType ct in currentOrder) 
       { 
        if (ct.Name.Contains(SponsoringCommon.Constants.CONTENTTYPES_PRODUCT_NAME)) 
        { 
         result.Add(ct); 
        } 
       } 
       productList.RootFolder.UniqueContentTypeOrder = result; 
       productList.RootFolder.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"; 
      schemaXmlWithResourceTokens = sourceField.SchemaXmlWithResourceTokens; 
      startIndex = schemaXmlWithResourceTokens.IndexOf("\"", schemaXmlWithResourceTokens.IndexOf("DisplayName=\"")) + 1; 
      endIndex = schemaXmlWithResourceTokens.IndexOf("\"", startIndex); 
      substringLength = endIndex - startIndex; 
      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.Update(true); 

      sourceList.ContentTypesEnabled = true; 
      sourceContentType.Update(true); 

      sourceList.ContentTypes.Add(sourceContentType);    
      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 views. 
       SPView vwAllItemsProductList = productList.GetSafeViewByName(SponsoringCommon.Constants.VIEWS_ALL_ITEMS); 
       SPViewFieldCollection colvwAllItemsProductList = vwAllItemsProductList.ViewFields; 
       if (vwAllItemsProductList.ViewFields.Exists("LinkTitle")) 
       { 
        vwAllItemsProductList.ViewFields.Delete("LinkTitle"); 
       } 
       colvwAllItemsProductList.Add(productField); 
       colvwAllItemsProductList.Add(amountField); 
       vwAllItemsProductList.Update(); 

       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; 

     } 
+0

你为什么要这样做?!?!?....只有开玩笑:D ......这可能没有什么帮助,但你应该不要调用sourceList.Update ()也适用于sourceList.ContentTypesEnabled = true; 生效? – Truezplaya 2012-07-24 14:41:42

+0

我试过了,但不是问题所在。 :( – 2012-07-24 14:56:27

+0

我编辑的问题,并包括完整的代码 – 2012-07-24 14:57:44

回答

0

的问题是,当我创建的内容类型,它是基于文档,而不是项目,所以我改变了一部分,这和它的工作。

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

//Update the sourceList here this before you add the CT 
sourceList.ContentTypes.Add(sourceContentType);    
+0

解决方案没有工作 – 2012-07-25 08:10:06

相关问题