I am trying to utilize SharePoint 2010 Client Object Model to create a List based on a custom template. Here's my code:
public void MakeList(string title, string listTemplateGUID, int localeIdentifier)
{
string message;
string listUrl;
ListTemplate template;
ListCreationInformation listInfo;
ListTemplateCollection templatesCollection;
try
{
listUrl = title.Replace(spaceChar, string.Empty);
templatesCollection = clientContext.Site.GetCustomListTemplates(clientContext.Web);
clientContext.Load(templatesCollection);
clientContext.ExecuteQuery();
foreach (ListTemplate t in templatesCollection)
{
if (t.InternalName == listTemplate)
{
template = t;
break;
}
}
listInfo = new ListCreationInformation();
listInfo.TemplateType = template.ListTemplateTypeKind;
//listInfo.TemplateFeatureId = template.FeatureId;
listInfo.Url = listUrl;
listInfo.Title = title;
listInfo.Description = string.Empty;
listInfo.QuickLaunchOption = QuickLaunchOptions.On;
site.Lists.Add(listInfo);
clientContext.ExecuteQuery();
return RetrieveList(title, listUrl);
}
catch (ServerException ex)
{
//...
}
}
A few unexpected things happen when this code is run:
My template is derived from the default document library template. Now, the code above does not create the document library based on my template - it creates the default document library instead.
If I uncomment the
//listInfo.TemplateFeatureId = template.FeatureId;
the code throws the ServerException error: "Cannot complete this action. Please try again."If I place
listInfo.TemplateFeatureId = template.FeatureId;
beforelistInfo.TemplateType = template.ListTemplateTypeKind;
the end result is the same as under item 1 - the default document library is created.
Could anyone please help me realize what am I doing wrong? Thanks.