none
copy list item to another list using Client object model in console application RRS feed

  • Question

  • Hi,

    I have a custom list which has a column (single line of text) - "ApName". I want to use the client object model to copy all the values from ApName column to another list using console application. The target list has the column "Names" to which the data has to be copied.

    How to achieve this?

    Thank you


    • Edited by Venkatzeus Tuesday, August 21, 2012 9:50 AM
    Tuesday, August 21, 2012 9:47 AM

Answers

  • Hi,

    Please take a look at this console application:

    using Microsoft.SharePoint.Client;
    using System.Net;
    
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("http://perseus/");
            clientContext.Credentials = new NetworkCredential("Administrator", "12345");
    
            string listNameFrom = "MyList1";
            string listNameTo = "MyList2";
            string columnNameFrom = "ApName";
            string columnNameTo = "Names";
            List listFrom = clientContext.Web.Lists.GetByTitle(listNameFrom);
            List listTo = clientContext.Web.Lists.GetByTitle(listNameTo);
            
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";
            ListItemCollection listItemsFrom = listFrom.GetItems(camlQuery);
            clientContext.Load(listItemsFrom, items => items.Include(item => item[columnNameFrom]));
            clientContext.ExecuteQuery();
    
            foreach (ListItem li in listItemsFrom)
            {
                ListItemCreationInformation itemCreateInfo =
                    new ListItemCreationInformation();
                ListItem listItem = listTo.AddItem(itemCreateInfo);                       
                listItem[columnNameTo] = li[columnNameFrom];
                listItem.Update();
            }
            clientContext.ExecuteQuery();
        }
    }


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog


    Tuesday, August 21, 2012 11:11 AM
  • I belive this is it

        //change the url to your site
                ClientContext ctx = new ClientContext("http://site");
                Web web = ctx.Web;
    
                //change the list title to the list title you are getting info from
                List listFrom = web.Lists.GetByTitle("ListFrom");
                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View>" +                                
                                    "<ViewFields>" +
                                        "<FieldRef Name='ApName'/>" +                                   
                                    "</ViewFields>" +
                                "</View>";
    
                ListItemCollection listItems = listFrom.GetItems(query);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
    
                //change the list title to the list title where you are adding new items
                List listTo = web.Lists.GetByTitle("ListTo");
    
                foreach (ListItem item in listItems)
                {
                    ListItemCreationInformation newItemInfo=new ListItemCreationInformation();                
                    ListItem newItem=listTo.AddItem(newItemInfo);
                    newItem["Names"] = item["ApName"];
                    newItem.Update();
                    ctx.ExecuteQuery();
                }   

    Hope this helps,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Tuesday, August 21, 2012 10:52 AM

All replies

  • This series of article should point you to the right direction, basically you need to use .net client object model.

    http://www.codeproject.com/Articles/268196/SharePoint-2010-Client-Object-Model-Part-2


    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn | My Virtual Business Card

    A THOUGHT ABOUT MY WORKPLACE :)

    Tuesday, August 21, 2012 10:05 AM
  • I belive this is it

        //change the url to your site
                ClientContext ctx = new ClientContext("http://site");
                Web web = ctx.Web;
    
                //change the list title to the list title you are getting info from
                List listFrom = web.Lists.GetByTitle("ListFrom");
                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View>" +                                
                                    "<ViewFields>" +
                                        "<FieldRef Name='ApName'/>" +                                   
                                    "</ViewFields>" +
                                "</View>";
    
                ListItemCollection listItems = listFrom.GetItems(query);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
    
                //change the list title to the list title where you are adding new items
                List listTo = web.Lists.GetByTitle("ListTo");
    
                foreach (ListItem item in listItems)
                {
                    ListItemCreationInformation newItemInfo=new ListItemCreationInformation();                
                    ListItem newItem=listTo.AddItem(newItemInfo);
                    newItem["Names"] = item["ApName"];
                    newItem.Update();
                    ctx.ExecuteQuery();
                }   

    Hope this helps,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Tuesday, August 21, 2012 10:52 AM
  • Hi,

    Please take a look at this console application:

    using Microsoft.SharePoint.Client;
    using System.Net;
    
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("http://perseus/");
            clientContext.Credentials = new NetworkCredential("Administrator", "12345");
    
            string listNameFrom = "MyList1";
            string listNameTo = "MyList2";
            string columnNameFrom = "ApName";
            string columnNameTo = "Names";
            List listFrom = clientContext.Web.Lists.GetByTitle(listNameFrom);
            List listTo = clientContext.Web.Lists.GetByTitle(listNameTo);
            
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";
            ListItemCollection listItemsFrom = listFrom.GetItems(camlQuery);
            clientContext.Load(listItemsFrom, items => items.Include(item => item[columnNameFrom]));
            clientContext.ExecuteQuery();
    
            foreach (ListItem li in listItemsFrom)
            {
                ListItemCreationInformation itemCreateInfo =
                    new ListItemCreationInformation();
                ListItem listItem = listTo.AddItem(itemCreateInfo);                       
                listItem[columnNameTo] = li[columnNameFrom];
                listItem.Update();
            }
            clientContext.ExecuteQuery();
        }
    }


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog


    Tuesday, August 21, 2012 11:11 AM
  • Hi,

    i can move list items to new list but how can i do with attachments as well?



    • Edited by cb39 Thursday, October 19, 2017 8:13 PM
    Thursday, October 19, 2017 8:13 PM