1

my requirement is I need to update the list item values and if list item contains attachments I need to override with the attachments, delete unused attachments from list item.

when I am trying to update list item with attachments, attachments are updating but I am getting

"version conflict"

error in clientContext.ExecuteQuery(); statement but properties are not updating, with out attachments list properties are updating but I need to update attachments also.

below is my code, where I am making mistake.

  public string updateEventsClient(string eventID, string EventTitle, string EventBody, string location, string EventRecurrenceData, DateTime EventStartDate, DateTime EventEndDate, bool allDayEvent, bool isRecurring, List<string> attachments, string group, string division, string department, string businessUnit, string category)
    {
        string itemId = "";

        try
        {
            string siteUrl = "http://vr-si-web-qa01/sites/ITForms/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("MyCal");

            Web web = clientContext.Web;

            string queryTxt = "<View><Query><Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Integer\">" + eventID + "</Value></Eq></Where></Query></View>";
            CamlQuery query = new CamlQuery();
            query.ViewXml = queryTxt;

            // 1st update
            ListItemCollection items = oList.GetItems(query);
            clientContext.Load(items);
            clientContext.ExecuteQuery();
            ListItem oListItem = items[0];

            clientContext.Load(oList, l => l.RootFolder.ServerRelativeUrl);
            clientContext.Load(oListItem, i => i.AttachmentFiles);
            clientContext.Load(oListItem, i => i.Id);
            clientContext.Load(clientContext.Site, s => s.Url);
            clientContext.ExecuteQuery();


            var version = oListItem.FieldValues["_UIVersionString"].ToString();
            double result;
            double.TryParse(version, out result);
             result = result + 1;
             var NewVersion = result;

            oListItem["Title"] = EventTitle;
            oListItem["Description"] = EventBody;


            oListItem.Update();

            var site = "http://vr-si-web-qa01";
            var site1 = "http://vr-si-web-qa01/sites/ITForms";
            var listurl = oList.RootFolder.ServerRelativeUrl;
            var listName = "MyCal";


            if (attachments.Count >= 1)
            {

                Attachment att = null;
                foreach (var attachment in attachments)
                {
                    string filename = "";
                    Uri uri = new Uri(attachment);
                    if (uri.IsFile)
                    {
                        filename = System.IO.Path.GetFileName(uri.LocalPath);
                    }


                    var filebyte = System.IO.File.ReadAllBytes(attachment);

                    UploadDocument(site, site1, oListItem, listName, listurl, filename, eventID, filebyte);

                }

            }

            clientContext.ExecuteQuery();

            MessageBox.Show("Success");
            return itemId;
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return itemId;
        }

    }


    public void UploadDocument(string siteURL, string siteURL1, ListItem listItem, string documentListName, string documentListURL, string documentName, string itemId, byte[] documentStream)
    {

        using (ClientContext clientContext = new ClientContext(siteURL1))
        {

            //Get Document List
            List calendarList = clientContext.Web.Lists.GetByTitle(documentListName);

            var fileCreationInformation = new FileCreationInformation();
            //Assign to content byte[] i.e. documentStream

            fileCreationInformation.Content = documentStream;
            //Allow owerwrite of document

            fileCreationInformation.Overwrite = true;
            //Upload URL

            fileCreationInformation.Url = siteURL + documentListURL + "/Attachments/" + itemId + "/" + documentName;
            Microsoft.SharePoint.Client.File uploadFile = calendarList.RootFolder.Files.Add(
                fileCreationInformation);

            calendarList.Update();

            listItem.Update();


            clientContext.ExecuteQuery();

        }
    }
2

I solved the issue by moving clientContext.ExecuteQuery(); to above update attachments loop.

    oListItem["Title"] = EventTitle;
    oListItem["Description"] = EventBody;

    oListItem.Update();
    clientContext.ExecuteQuery();

    var site = "http://vr-si-web-qa01";

    ....

The problem is in client object model once you update the list item will increase version, the object we have has different version with our ClientContext, so once i update list item with ClientContext, I am creating new ClientContext and updating again.

Finally, First I am updating the list item properties,then once properties is updated I am trying to update with new ClientContext and get list item and update one attachment at a time ( not all attachments at once) update it. it is working.

If you still got "version conflict" problem try to increase the list item version programmatically it will solve the issue.

 var version = oListItem.FieldValues["_UIVersionString"].ToString();
 double result;
 double.TryParse(version, out result);
 result = result + 1;
 var NewVersion = result;
 oListItem["owshiddenversion"] = NewVersion;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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