Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if a Document Library (SPDocumentLibrary) supports a particular ContentType
    text
    copied!<p>I have a SharePoint 2010 site set up locally for debugging with the following topography:</p> <pre><code>Main (SPSite) -&gt; Toolbox (SPWeb) -&gt; MyTool (SPWeb) </code></pre> <p>I have created and deployed the following to Main:</p> <ol> <li>Custom Field "RequestedBy"</li> <li>Custom Field "OriginalRequestFileName"</li> <li>Custom Content Type "RequestContentType" that contains the above two fields in addition to OOB fields </li> <li>Custom List Definition "RequestListDefinition" based on the above ContentType </li> <li>VisualWebPart "MyFileUploaderWebPart" that has a custom EditorPart to allow the user to define which document library the file should be uploaded to.</li> </ol> <p>I have created an instance of a list "My Request List" in MyTool that's based on my custom list definition "RequestListDefinition".</p> <p>In the EditorPart I've got a drop-down list of document libraries. </p> <pre><code> private void PopulateDocumentLibraryList(DropDownList dropDownList) { SPWeb currentWebsite = SPContext.Current.Web; SPListCollection lists = currentWebsite.GetListsOfType(SPBaseType.DocumentLibrary); if (lists.Count &gt; 0) { List&lt;SPDocumentLibrary&gt; docLibraries = lists.Cast&lt;SPList&gt;() .Select(list =&gt; list as SPDocumentLibrary) .Where(library =&gt; library != null &amp;&amp; !library.IsCatalog &amp;&amp; !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; } } </code></pre> <p>I would like to restrict the list of document libraries to only those that are derived from my custom list definition that I've deployed. I thought of doing this by checking the supported content types and thus tried altering the Where clause to:</p> <pre><code>private void PopulateDocumentLibraryList(DropDownList dropDownList) { SPWeb currentWebsite = SPContext.Current.Web; SPListCollection lists = currentWebsite.GetListsOfType(SPBaseType.DocumentLibrary); if (lists.Count &gt; 0) { SPContentType voucherRequestListContentType = currentWebsite.ContentTypes["VoucherRequestContentType"]; List&lt;SPDocumentLibrary&gt; docLibraries = lists.Cast&lt;SPList&gt;() .Select(list =&gt; list as SPDocumentLibrary) .Where(library =&gt; library != null &amp;&amp; !library.IsCatalog &amp;&amp; !library.IsSiteAssetsLibrary &amp;&amp; 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; } } </code></pre> <p>It bombs out with the following error though:</p> <pre><code>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.&lt;&gt;c__DisplayClass7.&lt;PopulateDocumentLibraryList&gt;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 </code></pre> <p>This suggests to me that it's failing to find the content type. </p> <p>Another thought I had was to try and retrieve all lists that are of my custom list definition type "RequestListDefinition". However, SPWeb.GetListsOfType() takes an SPListTemplateType, which is an enum and thus doesn't contain my custom list definition. The documentation for SPListTemplateType (<a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx</a>) suggests using a method that accepts a string or an int instead of SPListTemplateType but I haven't seen any documentation for this.</p> <p>Can someone please help me work out either:</p> <ol> <li>how I can get just those lists that are derived from my custom list definition; or</li> <li>how I can get a hold of my custom content type; or</li> <li>point me in the direction of a better solution for restricting the list of SPDocumentLibrary?</li> </ol> <p>Thanks!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload