Поделиться через


SPDocumentLibrary Class

Represents a document library in Windows SharePoint Services.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPList
    Microsoft.SharePoint.SPDocumentLibrary
      Microsoft.SharePoint.SPPictureLibrary

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPDocumentLibrary _
    Inherits SPList

Dim instance As SPDocumentLibrary
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPDocumentLibrary : SPList

Remarks

To obtain an SPDocumentLibrary object, cast the given list as a document library, as shown in the following example.

Dim documentLibrary As SPDocumentLibrary = CType(list, SPDocumentLibrary)
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;

Examples

The following code example iterates through all the sites and their lists and, excluding catalogs or form libraries, displays the name of the site and list, as well as the file name, for each item in every document library.

The example assumes the existence of an .aspx page that contains a label control.

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim sites As SPWebCollection = siteCollection.AllWebs
Dim site As SPWeb

For Each site In sites
    Dim lists As SPListCollection = site.Lists
    Dim list As SPList

    For Each list In lists

        If list.BaseType = SPBaseType.DocumentLibrary Then
            Dim docLibrary As SPDocumentLibrary = 
                CType(list, SPDocumentLibrary)

            If Not docLibrary.IsCatalog Then

                If list.BaseTemplate <> SPListTemplateType.XMLForm Then
                    Dim docLibItems As SPListItemCollection = 
                        docLibrary.Items
                    Dim docLibItem As SPListItem

                    For Each docLibItem In docLibItems
                        Label1.Text += SPEncode.HtmlEncode(site.Name) 
                                & " :: " _
                            & SPEncode.HtmlEncode(list.Title) 
                                & " :: " _
                            & SPEncode.HtmlEncode(docLibItem("Title")) 
                                & "<BR>"
                    Next docLibItem
                End If
            End If
        End If
    Next list
Next site
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;

foreach (SPWeb oWebsite in collWebsites)
{
    SPListCollection collLists = oWebsite.Lists;

    foreach (SPList oList in collLists)
    {
        if (oList.BaseType == SPBaseType.DocumentLibrary)
        {
            SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;

            if (!oDocumentLibrary.IsCatalog && oList.BaseTemplate != 
                SPListTemplateType.XMLForm)
            {
                SPListItemCollection collListItems = oDocumentLibrary.Items;

                foreach (SPListItem oListItem in collListItems)
                {
                    Label1.Text += SPEncode.HtmlEncode(oWebsite.Name) + 
                        " -- " + 
                        SPEncode.HtmlEncode(oList.Title) + " -- " + 
                        SPEncode.HtmlEncode(oListItem["Title"]) + 
                            "<BR>";
                }
            }
        }
    }
    oWebsite.Dispose();
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPDocumentLibrary Members

Microsoft.SharePoint Namespace