using visual studio C# copy items from one list to another

sweetu ve 281 Reputation points
2021-04-12T05:52:04.88+00:00

I would need to Fetch sharepoint online list and update sharepoint list lookup columns ,multiline text column ,people column , override modified by column .Further copy items from one list to another with same list structure field column types.Appreciate kind help

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2021-04-12T08:04:24.007+00:00

    Hi @sweetu ve ,

    For updating list item fields (lookup/multiline text/person field and system Modified By field), please refer the following sample:

              string password = "********";  
                string account = "user@Tenant.onmicrosoft.com";  
                var secret = new System.Security.SecureString();  
                foreach (char c in password)  
                {  
                    secret.AppendChar(c);  
                }  
                var credentials = new SharePointOnlineCredentials(account, secret);  
                using (ClientContext ctx = new ClientContext("https://Tenant.sharepoint.com"))  
                {  
      
                    ctx.Credentials = credentials;  
                    var web = ctx.Web;  
                    ctx.Load(web);  
                    ctx.ExecuteQuery();  
                      
      
                    var list = ctx.Web.Lists.GetByTitle("CopyItemList");  
                    ListItem item = list.GetItemById(1);  
                    FieldLookupValue lookupFieldValue = new FieldLookupValue();  
                    lookupFieldValue.LookupId = 2; // The lookup Id, modified with yours  
                    User user = ctx.Web.EnsureUser("user@tenant.onmicrosoft.com");  
                    User modifiedUser = ctx.Web.EnsureUser("user@tenant.onmicrosoft.com");  
                      
                    ctx.Load(item);  
                    ctx.Load(modifiedUser);  
                    ctx.Load(user);  
                    ctx.ExecuteQuery();  
                    FieldUserValue fieldUserValue = new FieldUserValue();  
                    fieldUserValue.LookupId = user.Id;  
                    FieldUserValue modifiedUserValue = new FieldUserValue();  
                    modifiedUserValue.LookupId = modifiedUser.Id;  
                    item["PersonField"] = fieldUserValue;  
                    item["LookupField"] = lookupFieldValue;  
                    item["MultiLineField"] = "This is MultiLine Value";  
                    item["Editor"] = modifiedUser;  
                    item.Update();  
                    ctx.ExecuteQuery();  
                     
                }  
    

    For copying list item into another list, please refere the sample here:

    Learn About Copying List, List Fields And List Items Using CSOM

    Thanks
    Best Regards


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. sweetu ve 281 Reputation points
    2021-04-12T15:04:10.707+00:00

    Many Thanks for your help.

    I tried as per your suggested link to copy the list items from source list to destination list and its working fine

    I sincerely apologize for any issue in my understanding also I had some less skills in CSOM .

    For updating list item fields (lookup/multiline text/person field and system Modified By field):
    Please let me know above code of yours should be updated inside the Private static void CreateSPListItems function of the suggested reference link .Additionally I created the source list and destination list manually the fields structure manually.

    Thanks in advance.

    0 comments No comments

  2. Jerryzy 10,566 Reputation points
    2021-04-13T01:24:37.143+00:00

    Hi @sweetu ve ,

    The code sample for updating list item fields (lookup/multiline text/person field and system Modified By field), you can place in static void Main() method:

    87124-snipaste-2021-04-13-09-23-52.png

    Firstly update the item field and then copy into another list.

    Thanks
    Best Regards


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. sweetu ve 281 Reputation points
    2021-04-13T07:42:13.84+00:00

    Many thanks again. Thanks a lot for your quick help

    0 comments No comments