Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have many lists, some of which were created using a custom and now-obsolete list definition. I need a programmatic way of finding out which lists were created using that old definition.

Currently my workaround is to check the ServerTemplate attribute in the list's PropertiesXml:

SPList.PropertiesXml.Contains(string.Format("ServerTemplate=\"{0}\"", oldTemplateId))

Is there a method to do this without iterating through lists and comparing properties? Are there any other properties I can use to identify which list definition the list is based on?

share|improve this question
    
Question is unclear on what exactly the problem is causing users to post answers that are unsatisfactory to you. –  SpartanDonut Feb 29 '12 at 17:17
1  
@tylerrrr07: I've already attempted to clarify by adding the [object-model] tag (following the two PowerShell answers, despite the C# code sample), but I'm really at a loss as to what extra information I can add. I've already got some background, an example of what I'm doing, and a request for a any better methods to get the same (or similar) information. If you have any suggestions for additional clarification then they're more than welcome. –  Stuart Pegg Feb 29 '12 at 17:43
    
The off-topic (PowerShell) answers have been re-upvoted after my downvotes; this means anyone looking at this in future may end up chasing wild geese, which is unfortunate. –  Stuart Pegg Feb 29 '12 at 17:56
    
Honestly on a first pass I thought you were looking for a way to query the site for lists based on list definition but I think the focus is more on the properties because you already have an SPList? I'm not sure if you are looking for a different way to get "ServerTemplate" or just an alternate way to identify a List Definition via the API. The answers provided give you ways to access list properties but don't tell you what properties would be useful which is where I'm sure you think they fell short. Your question is actually very good. We are just doing too much guessing to effectively help. –  SpartanDonut Feb 29 '12 at 18:19
    
@tylerrrr07: I'm looking for any way to identify lists created using a certain definition, my only caveats being that the answer should a) Use the Object Model, b) Be more efficient than my current rather bodgy workaround. All of which I've tried to make clear in my question. :| As it stands, this question isn't of use to anyone with this problem at present or in future, so I'll leave until tomorrow and then delete it if there are no further answers. –  Stuart Pegg Feb 29 '12 at 19:08

3 Answers 3

up vote 4 down vote accepted

The SPList.BaseTemplate property tells you what list definition was used to create a given list.

Also, LINQ is by far the most efficient way to query all the lists in your site -- and this could obviously be extrapolated to query all lists within an entire site collection too (see Example 2 here). Creating nested "foreach" loops to manually enumerate all your sites, webs and lists can be very onerous on your system, be warned!

Here is some sample code to get you started:

var obsoleteLists = SPContext.Current.Web.Lists.Cast<SPList>().Where(l => (int)l.BaseTemplate == 10000);

foreach (SPList list in obsoleteLists) 
{ 
    Console.WriteLine("The {0} list was created using the {1} definition.", list.Title, list.BaseTemplate);
}

NOTE: This code assumes that your custom list definition has an ID of 10000. You could replace this with any of the built-in SPListTemplateType values if you were querying for any OOTB list types, e.g. SPListTemplateType.Announcements

share|improve this answer
    
A quick glance in SPM seems to indicate that BaseTemplate is blank for all lists created with any custom list definition; hence I'd ruled it out before. However, this appears to be because SPM is attempting to convert it to the enumeration. Further digging in Server Explorer shows the true stored ID value. You sir, are my saviour. :) –  Stuart Pegg Mar 1 '12 at 7:37
    
+1 for good answer but "Also, LINQ is by far the most efficient way to query all the lists in your site!" Are you sure about that? Whats happening in the background? I suspect that its doing exactly the same as a for loop over entire collection - LINQ can only be more efficient for somemthing like SQL/CAML where the LINQ operator can be converted into something the underlying store can work with - sharepointblues.com/2010/09/08/… –  Ryan Mar 1 '12 at 8:02
    
This SO post explains it well - stackoverflow.com/questions/4267783/… –  Ryan Mar 1 '12 at 8:07
    
There are bucketloads of posts and articles on the web with comparisons of foreach loops vs linq queries. The results are almost always contradictory though so it probably depends on the situation in which you're using the linq/loop. Perhaps I was a "little" excited when I disclaimed that LINQ is always faster, but I generally tend to place more faith in Microsoft's LINQ developers and testers than I do in my own unit tests. Either way, the question above is about code that will only ever run once, so inthat case it's best to go down the path of whichever is easiest to read 'n write. –  Nick Larter Mar 1 '12 at 8:17
    
@Ryan: At this point I'd be happy even if the answer used COM. –  Stuart Pegg Mar 1 '12 at 8:19

You should be able to tweak this code to get what you are looking for.

http://blogs.pointbridge.com/Blogs/monnette_jeff/Pages/Post.aspx?_ID=22

Good luck.

share|improve this answer
    
Thanks, but that article appears to fetch the list's fields (not properties). –  Stuart Pegg Feb 29 '12 at 15:28

this may be of help to get you started - Viewing SharePoint properties as lists, tables and CSV in PowerShell

You can also dig into the list properties using SPManager.

[UPDATE] from first link item -

$web = Get-SPWeb http:/fooSite/
$list = $web.Lists["fooList"] 
$list  #this would give you all the properties including the TemplateFeatureId, SchemaXML, BaseTemplate, PropertiesXML, ServerTemplate, etc..
share|improve this answer
    
If I was having difficulties retrieving properties in PowerShell in general, then perhaps. However, that article doesn't even mention list definitions as far as I can tell and as you can see from my code sample, I'm using the Object Model... –  Stuart Pegg Feb 29 '12 at 15:39
    
SPM is a great little tool; I used it to find the ServerTemplate attribute I mentioned above. But unfortunately I can't integrate with it programmatically. –  Stuart Pegg Feb 29 '12 at 15:44
    
TemplateFeatureId could be useful in some circumstances, but unfortunately there is more than one list definition in the feature in my case. Here's the OM equivalent for anyone who would find it useful: msdn.microsoft.com/en-us/library/… –  Stuart Pegg Feb 29 '12 at 15:57
    
If the PowerShell version of the object does indeed include the ServerTemplate property you say it does, then it is unique to Powershell, as there is no equivalent OM property. –  Stuart Pegg Feb 29 '12 at 16:00
    
a PowerShell script output showing ServerTemplate: ...FeatureId="00bfea71-de22-43b2-a848-c05709900100" ServerTemplate="100" Created="20120208 12:33:41" Modified="20120208 12:34:06" . –  Supriyo SB Chatterjee Feb 29 '12 at 16:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.