During one of our recent projects we had a site, which contained multiple sites each several document libraries some of which were based on a custom list definition. During testing we noticed that all the document libraries based on our definition had 'Allow management of content types' set to no, which was a problem as we had create several content types that were to be used across the site. After some discussion we decided that there were two options:

  • Manually set each document library.
  • Write a custom application to do it for us.

Given the size of the site and the time involved in setting each document library we decided that the best approach was to write a custom application to do this for us and this is what I shall be looking at today.

My application was designed to only be used by my self or other developers and was for a very specific purpose, therefore was fairly basic but you could extract some of the settings from the actual code and create a generic application that could be tailored to extract a collection of any type of list or library across a site collection.

The first problem I encountered was that while trying to run the application i didn't have appropriate permission so in order to get around this I used the RunWithElevatedPrivileges method to prevent this. The important part of this section is you must open the web before running the code with elevated permissions otherwise you will still encounter problems.



Now that we have the correct permission to access the site via the API I could then connect to the chosen site, specified via a textbox on the application, and iterate through the sites to find the selection of document libraries that match my criteria. The first step in achieving this is to open the SPWeb object again this time with the higher level of permissions and make sure there are webs within the site collection. Next I iterated through the list of SPWebs in the collection and then for each SPWeb called a separated method that looked at all the lists within that site.



The IterateThroughLists method is the one that performs the check to see if any of the document libraries in the given site matches the criteria I’m looking for and if it does set 'Allow management of content types' set to true. The first step is to get the collection of lists within the site and then iterate through the lists. Next I examine each item in the list collection and compare them against my criteria. In this example I am looking for document libraries based on my custom definition so I know that the base type must be 'Document Library' and I also know that the document libraries I’m looking for have certain TemplateFeatureId so I can match this against a specific GUID, set as a constant. Finally I check to see if the document library already has 'Allow management of content types' set to true if it does then i can ignore it otherwise set it to true.



One key thing that cause me some problems was not calling the Update method as without this the code makes the changes but does not save them. So even though you can debug through the code and watch it set 'Allow management of content types'  to true if you don't call the update method when you then look at the document library it will still say 'Allow management of content types' is set to no . I hope you found this useful and can see how easily it can be changed to look across sites and generate collections of lists given certain criteria.


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Friday, May 09, 2008 12:26 PM | in MOSS 2007

Comments

No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 5 and 2 and type the answer here: