question

sweetuve-5992 avatar image
0 Votes"
sweetuve-5992 asked sweetuve-5992 action

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

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

office-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jerryzy avatar image
0 Votes"
Jerryzy answered

Hi @sweetuve-5992 ,

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sweetuve-5992 avatar image
0 Votes"
sweetuve-5992 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jerryzy avatar image
0 Votes"
Jerryzy answered sweetuve-5992 commented

Hi @sweetuve-5992 ,

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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sweetuve-5992 avatar image
0 Votes"
sweetuve-5992 answered

Many thanks again. Thanks a lot for your quick help

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.