2011-10-03 73 views
1

我有一個SharePoint 2010網站設置本地調試具有以下地形:如何檢查是否一個文檔庫(SPDocumentLibrary)支持特定的ContentType

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

我已經創建並部署以下主:

  1. 自定義字段 「RequestedBy」
  2. 自定義字段 「OriginalRequestFileName」
  3. 自定義內容類型 「RequestContentType」 的CO ntains除了基於上述的ContentType
  4. VisualWebPart「MyFileUploaderWebPart」具有自定義的EditorPart允許用戶定義哪些文檔庫中的文件應該被上傳OOB領域
  5. 自定義列表定義「RequestListDefinition」上述兩個領域至。

我在MyTool中創建了一個基於我的自定義列表定義「RequestListDefinition」的「My Request List」列表實例。

在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的列表的方向?

謝謝!!

回答

0

點2:

SPContentType應通過currentWebsite.AvailableContentTypes[name]檢索。 SPWebContentTypes屬性只會返回在此特定網絡上創建的內容類型。但是,確實會返回當前網站集中可用的所有內容類型。

更新: 要檢查列表中是否有您的內容類型,你應該在名單上使用的內容類型的集合:如果給定的內容類型爲支持

SPContentTypeId ctId = voucherRequestListContentType.Id; 

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

的方法SPList.IsContentTypeAllowed檢查而不是如果內容類型是列表的一部分。請參閱MSDN文檔SPList.IsContentTypeAllowed Method

+0

感謝,斯特凡:)這似乎已經成功地找到contentType中看到這一點。不幸的是,將IsContentTypeAllowed()調用添加到where子句並沒有給我我期待的結果。 「網站頁面」文檔庫仍顯示在我的文檔庫下拉菜單中。 –

+0

更新了關於「IsContentTypeAllowed」問題的問題。 – Stefan

+0

導致沒有返回文檔庫:/ –

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)" 
    } 
} 
相關問題