I have created timer job and in Execute method of job i have below code which create list

 using (SPSite oSPsite = new SPSite("http://myurl"))
                oSPsite.AllowUnsafeUpdates = true;

                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    oSPWeb.AllowUnsafeUpdates = true;

                    /* 1. create list from custom ListTemplate present within ListTemplateGalery */
                    SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
                    SPListTemplate template = lstTemp["MyTaskList"];
                    oSPWeb.Lists.Add("List Name", "Description", template);

                    /* 2. create list from sharepoint list content type (e.g. Links) */
                    oSPWeb.Lists.Add("List Name", "Description", SPListTemplateType.Links);

                    oSPWeb.AllowUnsafeUpdates = false;

                oSPsite.AllowUnsafeUpdates = false;

When i debug i get error on this line

SPListTemplate template = lstTemp["MyTaskList"];

The error is value does not fall within expected range

What could be reason of error? I have also tried to increase value of List View Lookup Threshold to 800 which is under central administration. But nothing is working.

  • Does your code work if you just run it in a console up and not from the timer job it self? Do you recycle OSWTimer and IIS before you deploy your timer job if you trying to debug timer job directly? – dotsa Apr 16 '15 at 23:08

You can upload the MyTaskList in your list templates gallery at the root web and then try to run your code again.Before testing it in a timer job , you can create a console application and test the same.


To get the custom list template collection, we have to use the RootWeb property of the site instead of specifying OpenWeb() method.

Use the code below,

SListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPsite.RootWeb);

Instead of below line

SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);

You are getting the error because lstTemp might be null. Check if the object is null before getting a specific template by Name.


Seems like "MyTaskList" list template is not included in the lstTemp. You need to debug and see if the line SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb); actually contains what you need.

Can you also try oSPWeb.ListTemplates instead of oSPsite.GetCustomListTemplates()


I got answer. I create list in already existing list instead of myself creating new list.

SPList thisTaskList = oSPWeb.Lists["MyTaskList"];

SPListItem tasks = thisTaskList.Items.Add();

tasks["Title"] = "New task" + DateTime.Now.ToString();;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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