kamagra how much to take

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):

  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:

  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….

  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.

  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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>