0

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();
1

try this code. Also, I referred this link which I found useful.

using Microsoft.SharePoint.Client;
using System.Security;

namespace ReplicateFields
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ClientContext ctx = new ClientContext("URL"))
            {
                SecureString oSec = new SecureString();
                foreach (char item in "pwd")
                {
                    oSec.AppendChar(item);
                }
                ctx.Credentials = new SharePointOnlineCredentials("username", oSec);
                List oSouceList = ctx.Site.RootWeb.Lists.GetByTitle("Person");
                ctx.Load(oSouceList.Fields);
                ctx.Load(oSouceList);

                ListCreationInformation oListinfo = new ListCreationInformation();
                oListinfo.TemplateType = (int)ListTemplateType.GenericList;
                oListinfo.Title = "Persons";
                List oDes = ctx.Web.Lists.Add(oListinfo);
                ctx.ExecuteQuery();
                foreach (Field oSourceField in oSouceList.Fields)
                {
                    if (oSourceField.Hidden== false)
                    {
                        string schemaTextField = string.Format("<Field Type='{0}' Name='{1}' StaticName='{2}' DisplayName='{3}' />", oSourceField.TypeAsString, oSourceField.InternalName, oSourceField.StaticName, oSourceField.TypeDisplayName);
                        Field simpleTextField = oDes.Fields.AddFieldAsXml(schemaTextField, true, AddFieldOptions.AddFieldInternalNameHint);
                        ctx.ExecuteQuery();
                    }
                }


            }
        }
    }
}

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.