我在本地设置了一个SharePoint 2010网站,以便使用以下地形进行调试:
Main (SPSite)
-> Toolbox (SPWeb)
-> MyTool (SPWeb)
我已创建并将以下内容部署到Main:
我在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的方法,但我没有看到任何相关的文档。
有人可以帮我解决:
谢谢!
答案 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)"
}
}