2011-09-28 159 views
1

如何獲取內容類型的子內容類型?獲取內容類型的子內容類型

here我瞭解到,所有的子內容類型都有contentTypeId,以父母的ID開頭。所以,我試圖使用鏈接中的建議方式。

讓我告訴你,我第一次嘗試:

 using (var site = new SPSite(SPContext.Current.Site.ID)) 
     { 
      using (var web = site.RootWeb) 
      { 
       SPContentTypeId ctId = new SPContentTypeId(MasterContentTypeId); 
       SPContentType masterCt = web.ContentTypes[ctId]; 

       var queryString = string.Empty; 
       queryString += "<Where>"; 
       queryString += "<BeginsWith>"; 
       queryString += "<FieldRef Name='ContentTypeId'/>"; 
       queryString += "<Value Type='Text'>"; 
       queryString += MasterContentTypeId; 
       queryString += "</Value>"; 
       queryString += "</BeginsWith>"; 
       queryString += "</Where>"; 

       SPSiteDataQuery query = new SPSiteDataQuery { Query = queryString }; 
       DataTable dt = web.GetSiteData(query); 
       DataView dv = new DataView(dt); 

       foreach (DataRow row in dt.Rows) 
       { 
        //Do Something Necessary 
       } 
      } 
     } 

,但沒有行的DataTable的回報。

這是正確的方式來做到這一點,但有一些缺失或錯誤;還是我做這一切都錯了?

回答

1

我想我找到了一個解決方案。 這裏是參考:link

IEnumerable<SPContentType> descendantContentTypes = from SPContentType childCt in web.AvailableContentTypes 
                    where childCt.Id.IsChildOf(parentContentTypeId) 
                    select childCt;