使用 csom 复制具有相同字段但不同列表名称的列表 [英] replicating the list with same fields but different list name using csom

查看:42
本文介绍了使用 csom 复制具有相同字段但不同列表名称的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 people 的列表,其中包含多个字段和默认字段,我需要创建一个名为 people 的新列表,其中包含 people 列表中的所有字段.

I have a list called people , which contains several fields and default fields , i need to create a new list called persons with all the fields in the people list .

// load the properties of web project
Web oWeb = oClientContext.Web;
// Get the people list in the web
List sourceList = oClientContext.Web.Lists.GetByTitle("people");
ListCreationInformation creationInfo = new ListCreationInformation();
creationInfo.Title = "persons";
creationInfo.Description = "new list created using VS 2013 &CSOM";
creationInfo.TemplateType = (int)ListTemplateType.GenericList;
List newList = oClientContext.Web.Lists.Add(creationInfo);

oClientContext.Load(newList);
oClientContext.ExecuteQuery();

推荐答案

示例代码供大家参考(代码基于 这个线程).

Sample code for your reference(the code is based on this thread).

class Program
    {
        public static void AddListFromSchema(string listName, string listUrl, string webUrl, string schemaXml, int listTemplateType, bool listQuickLaunch, string listDescription)
        {
            //read document
            XDocument doc = XDocument.Parse(schemaXml);
            XElement root = doc.Root;

            ////get basic list attributes            
            //int listTemplateType = Convert.ToInt32(root.Attribute("ListTemplateType").Value);
            //bool listQuickLaunch = Convert.ToBoolean(root.Attribute("OnQuickLaunch").Value);
            //string listDescription = root.Attribute("Description").Value;

            //get fields
            IEnumerable<XElement> xFields = root.Elements("Fields").Elements("Field");

            if (string.IsNullOrEmpty(webUrl) || string.IsNullOrEmpty(listUrl))
            {
                throw new ArgumentNullException("webUrl or listUrl cannot be empty");
            }
            else if (string.IsNullOrEmpty(listName))
            {
                listName = listUrl;
            }

            ClientContext context = new ClientContext(webUrl);
            Web web = context.Web;
            IEnumerable<List> result = context.LoadQuery(web.Lists.Where(myList => myList.Title == listName));
            context.ExecuteQuery();


            //Create list if doesn't exist
            List list = result.FirstOrDefault();
            if (list == null)
            {
                ListCreationInformation creationInfo = new ListCreationInformation();

                creationInfo.Title = listName;
                creationInfo.Url = listUrl;
                creationInfo.TemplateType = listTemplateType;
                list = web.Lists.Add(creationInfo);
                list.Description = listDescription;
                list.OnQuickLaunch = listQuickLaunch;

                list.Update();

                context.ExecuteQuery();
                //Add fields
                foreach (XElement xField in xFields)
                {
                    string fldName = xField.Attribute("Name").Value;
                    string fldDisplayName = xField.Attribute("DisplayName").Value;

                    IEnumerable<Field> fields = context.LoadQuery(list.Fields.Include(f => f.InternalName));
                    context.ExecuteQuery();

                    Field existingField = fields.FirstOrDefault(f => f.InternalName == fldName);
                    if (existingField == null)
                    {
                        string fieldXml = Regex.Replace(xField.ToString(), fldDisplayName, fldName);

                        //internal name is derived from 'DisplayName' ('InternalName' attribute not working), DisplayName is overriden later as 'Title'
                        Field fld = list.Fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.DefaultValue);
                        fld.Title = fldDisplayName;

                        fld.Update();
                        context.ExecuteQuery();
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            using (var oClientContext = new ClientContext("http://sp:12001"))
            {
                // load the properties of web project
                Web oWeb = oClientContext.Web;
                // Get the people list in the web
                List sourceList = oClientContext.Web.Lists.GetByTitle("people");
                oClientContext.Load(oWeb, web => web.Url);
                oClientContext.Load(sourceList, list => list.SchemaXml, list => list.BaseTemplate);
                oClientContext.ExecuteQuery();

                //ListCreationInformation creationInfo = new ListCreationInformation();
                //creationInfo.Title = "persons";
                //creationInfo.Description = "new list created using VS 2013 &CSOM";
                //creationInfo.CustomSchemaXml = sourceList.SchemaXml;
                //creationInfo.TemplateType = (int)ListTemplateType.GenericList;
                //List newList = oClientContext.Web.Lists.Add(creationInfo);

                //oClientContext.Load(newList);
                //oClientContext.ExecuteQuery();

                AddListFromSchema("persons", "Lists/persons", oWeb.Url, sourceList.SchemaXml, (int)ListTemplateType.GenericList, true, "new list created using VS 2013 &CSOM");
                Console.WriteLine("done");
                Console.ReadKey();
            }
        }
    }

这篇关于使用 csom 复制具有相同字段但不同列表名称的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆