基于内容类型查询页面库

时间:2012-01-24 15:41:13

标签: sharepoint sharepoint-2010

在下面的代码中,当我尝试根据页面库中使用的内容类型获取所有项目时出现错误。但是,当我查询公告列表时,它工作正常。

这是堆栈中的描述:

   at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
   at Microsoft.SharePoint.Library.SPRequest.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder)
   at Microsoft.SharePoint.SPWeb.GetList(String strUrl)
   at Intranet.ControlTemplates.ShortNewsOverview.GetItemFromList(SPWeb web, SPContentTypeUsage u, SPContentType ct)
   at Intranet.ControlTemplates.ShortNewsOverview.GetAllItems()
   at Intranet.ControlTemplates.ShortNewsOverview.Page_Load(Object sender, EventArgs e)


private void GetItemsbyContentType()
{

    SPSite site = SPContext.Current.Site;

    SPWeb rootwWeb = site.RootWeb;
    SPContentType ct = rootwWeb.ContentTypes["GeneralNews"];

    IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(ct);

    foreach (SPContentTypeUsage usage in usages)
    {

        if (usage.IsUrlToList)
        {
            SPList list = rootwWeb.GetList(usage.Url); /// <-- Error occurs here
            SPQuery query = new SPQuery();
            query.Query = "<Query />";

                //string.Concat(
                //            "<Where><Eq>",
                //                "<FieldRef Name='ContentType'/>",
                //                string.Format("<Value Type='Computed'>{0}</Value>", ct.Name),
                //            "</Eq></Where>");

            SPListItemCollection listItems = list.GetItems(query);

            Label lable1 = new Label();

            foreach (SPItem item in listItems)
            {
                lable1.Text += item["Title"].ToString() + "<br />";
            }
            Controls.Add(lable1);
            //for (int i = 0; i < listItems.Count; i++)
            //{
            //    listItems[i].Delete();
            //}
        }
    }

0 个答案:

没有答案