About      |       Articles      |      Services      |      Software      |      Contact

Latest Free SharePoint Software

ARB Security Solutions regularly releases free SharePoint software, including WebParts, Client Applications, Framework Extensions, and other Miscellaneous Components.
The most recent freeware is:

Title: Simple SharePoint Rollup WebPart
Date Published: 10/22/2009

Previous Two Free WebPart Releases:

SecureCenter For SharePoint

By SharePoint security integrators, for SharePoint security integrators.

SharePoint Security Assurance Program™

For externally facing SharePoint deployments, security is an acutely important deployment concern. Learn how through daily security scanning, you can ensure external business users and partners that they can collaborate in confidence!

Security Assurance WebPart:



Using BaseTemplate and BaseType

In the development newsgroups there was a question then when performing a foreach loop for SPLists in a SPListCollection whether there was a way that the return could be controlled to a specific type. More importantly, not to just a SPBaseType, but also to a BaseTemplate by passing in the ID that corresponds to the specific BaseTemplate integer.

So normally, we just have this in order to loop through all the lists (I am not using SPListCollection, the result is the same though):

C#:
  1. using (SPSite oSPsite = new SPSite("http://localhost"))
  2. {
  3. using (SPWeb oSPWeb = oSPsite.OpenWeb())
  4. {
  5. SPListCollection lists = oSPWeb.Lists;
  6.  
  7. foreach (SPList list in lists)
  8. {
  9. string stream= (string.Format("{0}/{1}", list.ParentWeb.Url, list.Title));
  10. }
  11. }
  12. }

The above would return all lists on the web, and then assuming that the stream string is called later will render out the URL (assuming that the Title hasen't changed :) ) to standard output. But, what if you want to refine it? Let's say for document libraries only? Normally, one would approach this like this:

C#:
  1. using (SPSite oSPsite = new SPSite("http://localhost"))
  2. {
  3. using (SPWeb oSPWeb = oSPsite.OpenWeb(treeView1.SelectedNode.Text))
  4. {
  5. SPListCollection lists = oSPWeb.Lists;
  6.  
  7. foreach (SPList list in lists)
  8. {
  9. if (list.BaseType == SPBaseType.DocumentLibrary)
  10.  
  11. string stream= (string.Format("{0}/{1}", list.ParentWeb.Url, list.Title));
  12. }
  13. }
  14. }

Thats a start, but perhaps it makes the most sense to only show the libraries that aren't considered hidden? Modify it a little bit further....

C#:
  1. using (SPSite oSPsite = new SPSite("http://localhost"))
  2. {
  3. using (SPWeb oSPWeb = oSPsite.OpenWeb(treeView1.SelectedNode.Text))
  4. {
  5. SPListCollection lists = oSPWeb.Lists;
  6.  
  7. foreach (SPList list in lists)
  8. {
  9. if ((!list.Hidden) && (list.BaseType == SPBaseType.DocumentLibrary))
  10.  
  11. string stream= (string.Format("{0}/{1}", list.ParentWeb.Url, list.Title));
  12. }
  13. }
  14. }

Now this is looking better, but let's make it way better! You can use BaseTemplate to specify you only want the 101 ancestral type, which will only return those libraries created with that type.

C#:
  1. using (SPSite oSPsite = new SPSite("http://localhost"))
  2. {
  3. using (SPWeb oSPWeb = oSPsite.OpenWeb(treeView1.SelectedNode.Text))
  4. {
  5. SPListCollection lists = oSPWeb.Lists;
  6.  
  7. foreach (SPList list in lists)
  8. {
  9. if ((!list.Hidden) && (list.BaseType == SPBaseType.DocumentLibrary) && ((int) list.BaseTemplate == 101))
  10.  
  11. string stream= (string.Format("{0}/{1}", list.ParentWeb.Url, list.Title));
  12. }
  13. }
  14. }

That is much better. Now you have a much cleaner return.

  • Share/Bookmark

1 Comment »

  1. Hi

    this trick could be very useful too :
    SPWeb.GetListsOfType

    //—————-
    using (SPWeb pweb = this.Site.OpenWeb())
    {
    SPListCollection Lists = pweb.GetListsOfType(SPBaseType.DocumentLibrary);
    Lists.ListsForCurrentUser = true;
    Lists.IncludeRootFolder = true;

    foreach (SPList list in Lists)
    {

    if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
    }
    //Do fun stuff
    }

    Comment by Renaud Comte — August 24, 2007 @ 2:35 am

RSS feed for comments on this post. TrackBack URL

Leave a comment