1

I have a drop off library in a site collection on a SharePoint Online tenant and I am writing a C# console application to update properties of uploaded documents, my code doesn't seem to update the properties, can someone tell me what I am doing wrong or missing?

Please note, this is SharePoint Online tenant.

using (var context = new ClientContext(webUrl))
        {
            context.Credentials = new SharePointOnlineCredentials(cm.Username, password);
            var web = context.Web;

            // Get drop off library and edit all items without making changes
            context.Load(web, w => w.Lists);
            context.ExecuteQuery();

            List dropOffLibrary = web.Lists.GetByTitle("Drop Off Library");
            context.Load(dropOffLibrary, dl => dl.RootFolder.Files);
            context.ExecuteQuery();

            foreach (var file in dropOffLibrary.RootFolder.Files)
            {
                context.Load(file.ListItemAllFields);
                context.ExecuteQuery();
                file.CheckOut();
                context.ExecuteQuery();
                var title = file.ListItemAllFields.FieldValues["Title"].ToString();
                file.ListItemAllFields.FieldValues["Title"] = title + "1";
                file.Update();
                context.Load(file);
                context.ExecuteQuery();
                file.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
                context.ExecuteQuery();
            }

            context.Load(dropOffLibrary, dl => dl.RootFolder.Files);
            context.ExecuteQuery();

            Console.WriteLine("Paused... (end)");
            Console.ReadLine();
        }
    }

1 Answer 1

4

Try Following code

            foreach (var file in dropOffLibrary.RootFolder.Files)
            {
                context.Load(file, f => f.ListItemAllFields);
                context.ExecuteQuery();
                var title = file.ListItemAllFields.FieldValues["Title"].ToString();
                ListItem newItem = file.ListItemAllFields;
                newItem["Title"] = title + "1";
                newItem.Update();
                file.Update();
                context.Load(file);
                context.ExecuteQuery();
            }
1
  • 1
    Thanks Akshay, the newItem bit was the missing part, I have edited your code with the working code I used. Commented Aug 30, 2016 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.