2012-07-24 2 views
0

코드로 콘텐츠 유형을 연결하려고합니다 (이유는 묻지 않음). 롤. 이 기능은 잘 작동하고 새로운 열이보기에 잘 추가됩니다.코드로 콘텐츠 형식을 만듭니다. 새 양식이 비어 있습니다.

NEW 또는 Edit 양식에서 어떤 이유로 새 필드 소스 이름이 없습니다. 고급 설정 인 목록 설정으로 이동하면 콘텐츠 형식 관리 = 아니오라고 표시되지만 코드에서 예를 설정합니다.

나는 아이디어가 없습니다.

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

+0

나는 시도했다, 그러나 그것은 문제가 아니었다. : ( –

+0

나는 질문을 편집하고 전체 코드를 포함했습니다 –

답변

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

해결책을 못했습니다 –

관련 문제