2009-03-11 3 views
0

글쎄, 나는 셰어 포인트 2007에 대해 상대적으로 새로운데, 나는이 플랫폼을위한 코드를 작성하는 방식을 개선하고 내가 이미 가지고있는 습관을 좀더 정교하게하고 싶다.Sharepoint 2007에서이 쿼리를 통해 문서 크기를 향상시킬 수 있습니까?

다음 코드에서는 "Documents"목록에있는 문서의 크기를 묻는 중입니다. 그러나 정보에 액세스하기 위해 많은 개체를 만드는 것처럼 보입니다. 똑바로 의사에게 갈 수있는 방법이 없습니까? (나는 spweb에 액세스해야하고 그 다음에 목록과 doc을 액세스해야합니까?)

oSPWeb.Lists [ "Documents"]에 해당하는 oSPWeb.Lists [0]을 사용하지 않는 것이 좋습니다.이 목록은 "Documentos" , o "Documents"등 ...

Plz이 코드를 개선 할 수 있습니까?

System.Text.StringBuilder oSb = new System.Text.StringBuilder(); 

       oSb.Append("  <Where>"); 
     oSb.Append("   <Eq>"); 
     oSb.Append("    <FieldRef Name=\"FileLeafRef\" />"); 
     oSb.Append("    <Value Type=\"Text\">"+documento+"</Value>"); 
     oSb.Append("   </Eq>"); 
     oSb.Append("  </Where>"); 
     oSb.Append(" <ViewFields>"); 
     oSb.Append("   <FieldRef Name=\"FileSizeDisplay\" />"); 
     oSb.Append(" </ViewFields>");  

     string sResult = oSb.ToString(); 

     bool Existe = false; 
     SPSite sps = null; 
     SPWeb oSPWeb = null; 
     SPList oList = null; 
     SPListItemCollection col = null; 

     try 
     { 
      sps = SPContext.Current.Site; 
      using(oSPWeb = sps.OpenWeb(url)) 
      { 
       oList = oSPWeb.Lists[0]; 
       SPQuery qry = new SPQuery(); 
       qry.Query = sResult; 

       col=oList.GetItems(qry); 
      } 
     } 

     catch { } 

     if (col != null) 
     { 

      //return col[0].File.Length.ToString(); 
      return col[0]["FileSizeDisplay"].ToString(); 
     } 
     else 
     { 
      return null; 
     } 

답변

1

특정 사이트 내에 여러 문서 라이브러리가있을 수 있으므로 문서 라이브러리의 이름, 색인 또는 ID를 알아야 할 수 있습니다. 따라서 이름은 구성 위치 어딘가에 저장 될 수 있습니다 (이름 - 값 쌍이있는 다른 목록 일 수 있음). 결국에는 web.Lists [ "list name"] (또는 list Id - I 목록 인덱스를 피하십시오).

목록/라이브러리에서 여러 항목을 선택하는 일반적인 방법은 CAML 쿼리를 사용하는 것입니다. 콜렉션이 null이 아닌 경우 콜렉션에 최소한 하나의 항목이 있다고 가정하지 않는 것이 유일한 권장 사항입니다. 그래서 대신 :

if (col != null) 

내가 할 것 :

if (col != null && col.Count > 0) 
+0

목록을 가져 오는 방법에 대한 정보를 저장하지 않은 경우 URL을 저장하고 대신 web.GetList (url)을 사용해야합니다. 자세한 내용은 시나리오 1을 참조하십시오. http://blogs.msdn.com/sowmyancs/archive/2008/10/26/best-practices-sharepoint-object-model-for-performance-tuning.aspx – JMD

0

마지막으로, 나는 커크와 JMD의 조언 다음 코드를 개선 한 URL을 http://blogs.msdn.com/sowmyancs/archive/2008/10/26/best-practices-sharepoint-object-model-for-performance-tuning.aspx 코드를 최적화 할 수있는 아주 좋은 시작은 정말 :

System.Text.StringBuilder oSb = new System.Text.StringBuilder(); 

      oSb.Append("  <Where>"); 
      oSb.Append("   <Eq>"); 
      oSb.Append("    <FieldRef Name=\"FileLeafRef\" />"); 
      oSb.Append("    <Value Type=\"Text\">"+document+"</Value>"); 
      oSb.Append("   </Eq>"); 
      oSb.Append("  </Where>"); 
      oSb.Append(" <ViewFields>"); 
      oSb.Append("   <FieldRef Name=\"FileSizeDisplay\" />"); 
      oSb.Append(" </ViewFields>");    

      string sResult = oSb.ToString(); 

      SPSite sps = null; 
      SPWeb oSPWeb = null; 
      SPList oList = null; 
      SPListItemCollection col = null; 

      try 
      { 
       sps = SPContext.Current.Site; 
       using(oSPWeb = sps.OpenWeb(url)) 
       { 
        //oList = oSPWeb.Lists[0]; 
        oList=oSPWeb.GetList(oSPWeb.Url + "/" + listName); 
        SPQuery qry = new SPQuery(); 
        qry.Query = sResult; 

        col=oList.GetItems(qry); 
       } 
      } 

      catch { } 

      if (col != null && col.Count > 0) 
      { 
       return col[0]["FileSizeDisplay"].ToString(); 
      } 
      else 
      { 
       return null; 
      } 
관련 문제