Update listitem throws exception?

When I use CSOM to commit batch operations as below:

            ClientContext context = new ClientContext("http://win-cpqm71buqvj:1000/sites/DC");

            var web = context.Web;
            var file1 = web.GetFileByServerRelativeUrl("/sites/DC/Test Document Id/ddd.docx");

            file1.CheckOut();

            file1.ListItemAllFields["FileLeafRef"] = "ddd.docx";
            file1.ListItemAllFields["Editor"] = 1;
            file1.ListItemAllFields["Author"] = 1;
            file1.ListItemAllFields["Modified"] = DateTime.UtcNow.AddDays(-1);
            file1.ListItemAllFields.Update();

            file1.CheckIn("", CheckinType.OverwriteCheckIn);

            file1.ListItemAllFields["FileLeafRef"] = "ddd.docx";
            file1.ListItemAllFields["Editor"] = 1;
            file1.ListItemAllFields["Author"] = 1;
            file1.ListItemAllFields["Modified"] = DateTime.Now.AddYears(1);

            file1.ListItemAllFields.Update();

            context.ExecuteQuery();//throws exception here 

The error message is:

            Microsoft.SharePoint.Client.ServerException was unhandled
            HResult=-2146233088
            Message=The file Test Document Id/ddd.docx has been modified by SHAREPOINT\system on 03 2015 13:39:16 +0800.
            Source=Microsoft.SharePoint.Client.Runtime
            ServerErrorCode=-2130575305
            ServerErrorTraceCorrelationId=ff01e69c-1de7-107b-db0e-21e25ad7e352
            ServerErrorTypeName=Microsoft.SharePoint.SPException
            ServerStackTrace=""
            StackTrace:
                 at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
                 at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
                 at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
                 at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
                 at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
                 at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
                 at SaveConflictTest.Program.Main(String[] args) in c:\Users\ccui\Documents\GitHub\CSOM_API_Test\UpdateConlictSample\Program.cs:line 41
                 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
                 at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
                 at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
                 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
                 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
                 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
                 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
                 at System.Threading.ThreadHelper.ThreadStart()

            InnerException: 

I think the second Update cause the exception. If I use sharepoint server api to do the same operation, it is OK. Is this a CSOM bug? How to avoid this type of exception?



February 3rd, 2015 1:02am

Hi Simon,

because you need to load data first before updating or retrieving 

check the following links and code

https://msdn.microsoft.com/en-us/library/office/fp179912(v=office.15).aspx

https://msdn.microsoft.com/en-us/library/ff798388.aspx

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

// The SharePoint web at the URL.
Web web = context.Web; 

// We want to retrieve the web's properties.
context.Load(web); 

// Execute the query to the server.
context.ExecuteQuery(); 

// Now, the web's properties are available and we could display 
// web properties, such as title. 
label1.Text = web.Title;

Free Windows Admin Tool Kit Click here and download it now
February 3rd, 2015 4:50am

Hi,

you have to add the checkout statement and do the second update and try the following like this

  file1.CheckIn("", CheckinType.OverwriteCheckIn);
  
//Newly added line

file1.CheckOut();

            file1.ListItemAllFields["FileLeafRef"] = "ddd.docx";
            file1.ListItemAllFields["Editor"] = 1;
            file1.ListItemAllFields["Author"] = 1;
            file1.ListItemAllFields["Modified"] = DateTime.Now.AddYears(1);

            file1.ListItemAllFields.Update();

February 3rd, 2015 5:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics