2013-06-27 3 views
3

내가Sitecore 항목에 모든 입력란이 포함되어 있지 않은 이유는 무엇입니까?

private void WriteDataToItems(Item rootitem, IQueryable<LanguageData> languageData, SC.Globalization.Language sclng) 
     { 
      if (rootitem == null) 
       return; 
      rootitem = IncVersion(rootitem); 
      Response.Write(string.Format("Item {0} - {1}<br/>", rootitem.DisplayName, rootitem.Paths.FullPath)); 
      rootitem.Editing.BeginEdit(); 
      try 
      { 
       foreach (Field fld in rootitem.Fields.Where(d => !d.Shared && !d.Name.StartsWith("__") && d.Name.Trim() != "")) 
       { 
        Response.Write(string.Format("Processing fld: - {0}<br/>", fld.ID.Guid.ToString())); 
        var data = languageData.FirstOrDefault(
          d => (string.Compare(d.FieldName, fld.Name, true) == 0) && (string.Compare(d.ItemID, rootitem.ID.Guid.ToString(), true) == 0)); 
        if (data != null) 
        { 
         string newValue = null; 
         switch (sclng.Name) 
         { 
          case "en": 
           newValue = data.En; 
           break; 
          case "nn-NO": 
           newValue = data.nnNO; 
           break; 
          case "sv-SE": 
           newValue = data.svSE; 
           break; 
          case "da-DK": 
           newValue = data.DaDK; 
           break; 
          case "de-DE": 
           newValue = data.deDE; 
           break; 
          default: 
           newValue = null; 
           break; 
         } 

         if (newValue != null) 
         { 
          Response.Write(string.Format("Save field with Id:{0} New Value:{1}<br/>", fld.ID.Guid, newValue)); 
          fld.Value = newValue; 
         } 
        } 

       } 

       rootitem.Editing.EndEdit(); 
      } 
      catch (Exception ex) 
      { 
       rootitem.Editing.CancelEdit(); 

      } 

      foreach (Item cd in rootitem.GetChildren()) 
      { 
       WriteDataToItems(cd, languageData, sclng); 
      } 

     } 

rootitem.Fields 개체를하지 템플릿에 설명 된 모든 필드를 포함 할 수있는 문제 (단지 프로토 타입과 같은) 다음 코드를 가지고, 난 그냥 몇 가지를 바로 필드를 포함하는 출원이 값이 있지만 빈 데이터가있는 필드를 포함하지 않습니다.

foreach (Field fld in rootitem.Fields.Where(d => !d.Shared && !d.Name.StartsWith("__") && d.Name.Trim() != "")) 

모든 사용자 정의 필드 이름을 가져올 수있는 방법은 무엇입니까? 템플릿 데이터를 사용해야합니까?

rootitem.Fields.ReadAll(); 
foreach (Field fld in rootitem.Fields.Where(d => !d.Shared && !d.Name.StartsWith("__") && d.Name.Trim() != "")) 

답변

9

시도 :

/// <summary> 
/// Reads all. 
/// 
/// </summary> 
public void ReadAll() 
{ 
    Template template = TemplateManager.GetTemplate(this._ownerItem); 
    if (template == null) 
    return; 
    TemplateField[] fields = template.GetFields(); 
    this._fields = new Field[fields.Length]; 
    for (int index = 0; index < fields.Length; ++index) 
    this._fields[index] = new Field(fields[index].ID, this._ownerItem); 
} 

/// <summary> 
/// Resets this instance. 
/// 
/// </summary> 
public void Reset() 
{ 
    this._fields = (Field[]) null; 
    this._innerFields = (FieldList) null; 
} 
+0

궁금한 점 : ReadAll()은 무엇을합니까? 이 함수는 보이지 않는 필드를 읽습니까? –

+1

@ RG-3 : ReadAll()은 항목 템플릿을 가져 와서 "GetFields"를 호출 한 다음 해당 필드를 반복하여 각각을 FieldCollection 객체의 private _fields [] 배열에 추가합니다. –

3

당신이 방법 여기 item.Fields.Reset();

이러한 분야에 사용되는 메모리를 해제 할 수 있습니다 Fields.ReadAll()를 사용 후 ReadAll()Reset() 코드입니다 : 모든 필드 통해 반복하기 전에 Fields.ReadAll();를 사용하는

3

성능 향상을 위해 Sitecore는 giv 다음 코드에서 FieldCollection의 모든 필드는 빈 문자열을 포함하여 항목 레벨에 명시 적 값이있는 필드에만 적용됩니다.

그러나, 당신도 Sitecore.Context.Item.Fields["title"]으로 또는 SC와 같은 PAGEEDITOR 사용 웹 컨트롤로 필드에 액세스 할 수 있습니다 : 텍스트 : <sc:text field="title" runat="server" />

FieldCollection의 모든 필드를 가지고 그들을 통해 반복하기 위해, 확인 foreach 앞에 코드에 Sitecore.Data.Items.Item.Fields.ReadAll() 전화가 포함되도록하십시오.

관련 문제