开发者

Cannot create list in SharePoint 2010 using Client Object Model

开发者 https://www.devze.com 2022-12-30 17:08 出处:网络
I am trying to utilize SharePoint 2010 Client Object Model to create a List based on a custom template. Here\'s my code:

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:

  1. My template is derived from the default document library template. Now, the code above does not create the d开发者_高级运维ocument library based on my template - it creates the default document library instead.

  2. If I uncomment the //listInfo.TemplateFeatureId = template.FeatureId; the code throws the ServerException error: "Cannot complete this action. Please try again."

  3. If I place listInfo.TemplateFeatureId = template.FeatureId; before listInfo.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号