Inside my Sharepoint 2013 project, I can create a list and then inside a different project in the same solution, I can create a list based on that template - template

I've found this article detailing how to create a list in code based on a list template. I'm trying to create a user control that would go on a master page that is shared across all site collections. This user control will have a dependency on several lists, so I want to add them to my project and in the Feature's Event Receiver (FeatureActivated method), check to see if the list is created. If not, create it (for each list). But I would like to have the list template be part of the project (and not out on the site collection) and reference it with my code.

What I would like to do is this:

SPSite siteCollection = SPContext.Current.Site;
SPWeb mySite = SPContext.Current.Web;
SPListTemplateCollection listTemplates = //get a reference to my template here - this is not in the site collection, but in my project;
SPListTemplate listTemplate = listTemplates["Custom List Template"];

mySite.Lists.Add("Custom List", "A list created from a custom list template in my project", listTemplate);

Is something like this possible? Practical?

1 Answer 1


I was able to accomplish what I wanted by adding a List (called List1) to my Sharepoint project (but not including a list instance in the Feature) and then in the event receiver's FeatureActivated method, I check to see if the list exists (because on some sites, it may already exist, and if it doesn't, I create it like this:

var context = (SPSite)properties.Feature.Parent;

var rootContext = context.RootWeb;
SPListTemplate listTemplate = rootContext.ListTemplates["List1"];

rootContext.Lists.Add("Test List 1", "Just a fake list", listTemplate);
var mahList = rootContext.Lists.TryGetList("Test List 1");

From what I can tell, this works because the List Template for List1 gets added to the site before the feature is activated, so I have access to the template.


This works in a "normal" webpart, but I'm getting an exception while trying to do it inside a usercontrol that is on the Master page. I'm looking into it further.

EDIT 2 Ok, I finally got this to work. The issue was Sharepoint appending a "1" onto the end of the field name in the schema definition. I had to pour through the ULS logs to discover this. Once I removed the "1", it worked just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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