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

jvlzgdj9  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(118)

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

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

我已经创建了以下内容并将其部署到Main:
1.自定义字段“请求者”
1.自定义字段“原始请求文件名”
1.自定义内容类型“RequestContentType”,包含上述两个字段以及OOB字段
1.基于上述ContentType的自定义列表定义“RequestListDefinition”

  1. VisualWebPart“MyFileUploaderWebPart”具有自定义EditorPart,允许用户定义应将文件上载到哪个文档库。
    我在MyTool中创建了一个列表示例“MyRequestList”,它基于我的自定义列表定义“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”的所有列表。()接受一个SPListTemplateType,这是一个枚举,因此不包含我的自定义列表定义。SPListTemplateType的文档(http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx)建议使用一个接受字符串或整型的方法,而不是SPListTemplateType,但我还没有看到任何关于这方面的文档。
有人能帮我解决以下两个问题吗:
1.如何获得从自定义列表定义派生的列表;或
1.如何获取自定义内容类型;或
1.请为我指出限制SPDocumentLibrary列表的更好解决方案的方向。
谢谢!

toe95027

toe950271#

第二点:

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

**更新:**若要检查列表是否包含您的内容类型,应使用列表上的内容类型集合:

SPContentTypeId ctId = voucherRequestListContentType.Id;

// LINQ where clause:
.Where(library => (...) && library.ContentTypes[ctID] != null);

方法SPList.IsContentTypeAllowed检查给定的内容类型是否在列表中受支持,而不检查该内容类型是否是列表的一部分。请参阅MSDN文档SPList.IsContentTypeAllowed Method

r1zhe5dt

r1zhe5dt2#

我发现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)"
    }
}

相关问题