如何检查文档库(SPDocumentLibrary)是否支持特定的ContentType

时间:2011-10-03 05:16:15

标签: linq sharepoint-2010

我在本地设置了一个SharePoint 2010网站,以便使用以下地形进行调试:

Main (SPSite)
 -> Toolbox (SPWeb)
    -> MyTool (SPWeb)

我已创建并将以下内容部署到Main:

  1. 自定义字段“RequestedBy”
  2. 自定义字段“OriginalRequestFileName”
  3. 自定义内容类型“RequestContentType”,除OOB字段外还包含上述两个字段
  4. 自定义列表定义“RequestListDefinition”基于以上ContentType
  5. VisualWebPart“MyFileUploaderWebPart”,它具有自定义EditorPart,允许用户定义文件应上传到哪个文档库。
  6. 我在MyTool中创建了一个列表“我的请求列表”的实例,该列表基于我的自定义列表定义“RequestListDefinition”。

    在EditorPart中,我有一个文档库的下拉列表。

     private void PopulateDocumentLibraryList(DropDownList dropDownList)
        {
            SPWeb currentWebsite = SPContext.Current.Web;
    
            SPListCollection lists = currentWebsite.GetListsOfType(SPBaseType.DocumentLibrary);
            if (lists.Count > 0)
            {
                List<SPDocumentLibrary> docLibraries = lists.Cast<SPList>()
                    .Select(list => list as SPDocumentLibrary)
                    .Where(library => library != null && !library.IsCatalog && !library.IsSiteAssetsLibrary)
                    .ToList();
    
                dropDownList.DataSource = docLibraries;
                dropDownList.DataTextField = "Title";
                dropDownList.DataValueField = "ID";
                dropDownList.DataBind();
    
                // Default the selected item to the first entry
                dropDownList.SelectedIndex = 0;
            }
        }
    

    我想将文档库列表限制为仅从我部署的自定义列表定义派生的文档库列表。我想通过检查支持的内容类型来做到这一点,因此尝试将Where子句改为:

    private void PopulateDocumentLibraryList(DropDownList dropDownList)
    {
        SPWeb currentWebsite = SPContext.Current.Web;
    
        SPListCollection lists = currentWebsite.GetListsOfType(SPBaseType.DocumentLibrary);
        if (lists.Count > 0)
        {   
            SPContentType voucherRequestListContentType = currentWebsite.ContentTypes["VoucherRequestContentType"];
            List<SPDocumentLibrary> docLibraries = lists.Cast<SPList>()
                .Select(list => list as SPDocumentLibrary)
                .Where(library => library != null && !library.IsCatalog && !library.IsSiteAssetsLibrary && library.IsContentTypeAllowed(voucherRequestListContentType))
                .ToList();
    
            dropDownList.DataSource = docLibraries;
            dropDownList.DataTextField = "Title";
            dropDownList.DataValueField = "ID";
            dropDownList.DataBind();
    
            // Default the selected item to the first entry
            dropDownList.SelectedIndex = 0;
        }
    }
    

    但是它出现了以下错误:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------
    
    Value cannot be null.
    Parameter name: ct 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: ct
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Stack Trace: 
    
    
    [ArgumentNullException: Value cannot be null.
    Parameter name: ct]
       Microsoft.SharePoint.SPList.IsContentTypeAllowed(SPContentType ct) +26981638
       Dominos.OLO.WebParts.FileUploader.<>c__DisplayClass7.<PopulateDocumentLibraryList>b__4(SPDocumentLibrary library) +137
       System.Linq.WhereEnumerableIterator`1.MoveNext() +269
       System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +578
       System.Linq.Enumerable.ToList(IEnumerable`1 source) +78
       Dominos.OLO.WebParts.FileUploader.DocumentLibrarySelectorEditorPart.PopulateDocumentLibraryList(DropDownList dropDownList) +801
       Dominos.OLO.WebParts.FileUploader.DocumentLibrarySelectorEditorPart.CreateChildControls() +154
       System.Web.UI.Control.EnsureChildControls() +146
       Dominos.OLO.WebParts.FileUploader.DocumentLibrarySelectorEditorPart.SyncChanges() +102
       Microsoft.SharePoint.WebPartPages.ToolPane.OnSelectedWebPartChanged(Object sender, WebPartEventArgs e) +283
       System.Web.UI.WebControls.WebParts.WebPartEventHandler.Invoke(Object sender, WebPartEventArgs e) +0
       Microsoft.SharePoint.WebPartPages.SPWebPartManager.BeginWebPartEditing(WebPart webPart) +96
       Microsoft.SharePoint.WebPartPages.SPWebPartManager.ShowToolPaneIfNecessary() +579
       Microsoft.SharePoint.WebPartPages.SPWebPartManager.OnPageInitComplete(Object sender, EventArgs e) +296
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Page.OnInitComplete(EventArgs e) +11056990
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1674
    

    这告诉我,它找不到内容类型。

    我的另一个想法是尝试检索我的自定义列表定义类型“RequestListDefinition”的所有列表。但是,SPWeb.GetListsOfType()采用SPListTemplateType,它是一个枚举,因此不包含我的自定义列表定义。 SPListTemplateType(http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx)的文档建议使用接受字符串或int而不是SPListTemplateType的方法,但我没有看到任何相关的文档。

    有人可以帮我解决:

    1. 我如何才能获得从我的自定义列表定义派生的那些列表;或
    2. 如何获取我的自定义内容类型;或
    3. 指出我是否有更好的解决方案来限制SPDocumentLibrary列表?
    4. 谢谢!

2 个答案:

答案 0 :(得分:0)

第2点:

SPContentType应通过currentWebsite.AvailableContentTypes[name]检索。 ContentTypes的{​​{1}}属性仅返回在此特定网站上创建的内容类型。但是,SPWeb会返回当前网站集中可用的所有内容类型。

<强>更新 要检查列表是否包含您的内容类型,您应该使用列表中的内容类型集合:

AvailableContentTypes

方法SPContentTypeId ctId = voucherRequestListContentType.Id; // LINQ where clause: .Where(library => (...) && library.ContentTypes[ctID] != null); 检查列表中的给定内容类型是否支持,而不是内容类型是列表的一部分。请参阅MSDN文档SPList.IsContentTypeAllowed Method

答案 1 :(得分:0)

我发现IsApplicationList(SP 2013)有助于限制非系统库的文档库(即IsApplicationList适用于_catalogs,SiteAssets和SitePages,但不适用于共享文档)。

在PowerShell中,您可以通过运行以下

来查看此内容
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PsSnapin Microsoft.SharePoint.PowerShell
$site = Get-SPSite "http://sharePoint2013Url" 
$webs = $site.AllWebs
foreach($web in $webs)
{
    Write-Host "$($web.Url)"
    foreach($list in $web.GetListsOfType([Microsoft.SharePoint.SPBaseType]::DocumentLibrary))
    {
       Write-Host "$($list.DefaultEditFormUrl)  $($list.IsApplicationList)"
    }
}