The remote server returned an error: (409) Conflict from SharePoint ClientObjectModel

Problem

I got this issue for ListItem having attachments. We’re initially creating a ListItem using the Update() & ExecuteQuery() method. Next when we tried to create an attachment for the same ListItem using ExecuteQuery(), this error was thrown.

Solution

Solution was to remove the ExecuteQuery() after the ListItem Update() and use a single ExecuteQuery() for both the ListItem and its Attachment.

using System;
using IO = System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;


string siteUrl = "http://servername:12345/";
SP.ClientContext ctx = new SP.ClientContext(siteUrl);
SP.List list = ctx.Web.Lists.GetByTitle("ListTitle");

SP.ListItemCreationInformation listItemCreationInfo = new SP.ListItemCreationInformation();
SP.ListItem oListItem = list.AddItem(listItemCreationInfo);
                
//This will create ListItem before adding attachments
oListItem.Update();

//The following line should be removed
//ctx.ExecuteQuery();


string strFilePath = "FullFilePath";

byte[] bytes = IO.File.ReadAllBytes(strFilePath);

using (IO.MemoryStream mStream = new IO.MemoryStream(bytes))
{
    SP.AttachmentCreationInformation aci = new SP.AttachmentCreationInformation();
    aci.ContentStream = mStream;
    aci.FileName = IO.Path.GetFileNameWithoutExtension(strFilePath);

    oListItem.AttachmentFiles.Add(aci);

    oListItem.Update();
    ctx.ExecuteQuery();
}

The error was thrown at the last line (ctx.ExecuteQuery() in bold). Actually the List was already created above (commented ctx.ExecuteQuery()). The solution was to remove that ExecuteQuery() and bundle both the requests, create ListItem & upload attachment, in a single ExecuteQuery().

NOTE:- Do not forget to mention the oListItem.Update() as the ListItem has to be created first before adding attachments to it. Just don’t immediately call the Execute method

Create a ListItem with Attachment using Client Object Model

FieldValuesAsTexts is a Dictionary which has been populated with some values.
using System;
using IO = System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;


string siteUrl = "http://servername:12345/";
SP.ClientContext ctx = new SP.ClientContext(siteUrl);
SP.List list = ctx.Web.Lists.GetByTitle("ListTitle");

SP.ListItemCreationInformation listItemCreationInfo = new SP.ListItemCreationInformation();
SP.ListItem oListItem = list.AddItem(listItemCreationInfo);

foreach (string key in FieldValuesAsTexts.Keys)
{
    if (ValidateKey(key))
    {
        string strKey = fieldEntities.FirstOrDefault(t => t.FieldName == key).FieldInternalName;
        if (FieldValuesAsTexts[key] != String.Empty)
        {
            string strVal = FieldValuesAsTexts[key];
            
            //You can not have a Yes/No value for a field/column whose type is bit. Hence replacing it with 1/0
            strVal = (strVal.ToLower().Trim() == "yes") ? "1" : (strVal.ToLower().Trim() == "no") ? "0" : strVal;
            oListItem[strKey] = strVal;
        }
    }
}
                
//This will create ListItem before adding attachments
oListItem.Update();

//Let's say we have a custom class called AttachmentEntity and AttachmentsCollection is the //collection of the class Attachments.

if (listItemEntity.Attachments != null)
{
    foreach (AttachmentEntity attachEntity in AttachmentsCollection)
    {
        string strFilePath = GetAbsoluteFilePath(strDirectoryPath, attachEntity.FileName);
        strFilePath = String.Concat(strFilePath, "\\", attachEntity.FileName);

        byte[] bytes = IO.File.ReadAllBytes(strFilePath);

        IO.MemoryStream mStream = new IO.MemoryStream(bytes);

        SP.AttachmentCreationInformation aci = new SP.AttachmentCreationInformation();
        aci.ContentStream = mStream;
        aci.FileName = attachEntity.FileName;

        SP.Attachment attachment = oListItem.AttachmentFiles.Add(aci);

        oListItem.Update();
    }
}
ctx.ExecuteQuery();

Upload a File to SharePoint using Client Object Model

Following is the complete process of uploading/creating a file on the SharePoint server using the Client Object Model. The following process should also be implied for some of the ListItems. Traditionally, we use ListItemCreationInformation class to create a ListItem. However, if your List is a

  • Web Page Library
  • Picture Library

then also you have to use the below process to create their ListItem otherwise, you’ll get the Microsoft.SharePoint.Client.ServerException, use the SPFileCollection.Add() to create file. This is one of such example where I am creating a ListItem for the Web Page Library List

using System;
using System.Collections.Generic;
using IO = System.IO;
using System.Linq;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
string siteUrl = "http://siteUrl";
SP.ClientContext ctx = new SP.ClientContext(siteUrl);
SP.List list = ctx.Web.Lists.GetByTitle("ListTitle");
byte[] bytes = IO.File.ReadAllBytes(strFilePath); //strFilePath is the full path of the file that needs to be uploaded

IO.MemoryStream mStream = new IO.MemoryStream(bytes);

SP.FileCreationInformation fileInfo = new SP.FileCreationInformation();
fileInfo.ContentStream = mStream;
fileInfo.Overwrite = true;
fileInfo.Url = "https://siteUrl/Subsite1/Subsite2/SitePages/TestPage.aspx";
SP.File file = list.RootFolder.Files.Add(fileInfo);

list.Update();

SP.ListItem listItem = file.ListItemAllFields;

//If you have the MetaInfo that you want to restore
listItem["MetaInfo"] = ltItemEntity.FieldValuesAsTexts["MetaInfo"];

listItem.Update();

ctx.ExecuteQuery();

To update large files, up to 2GB, check out my other blog at, https://realmpksharepoint.wordpress.com/2014/04/29/upload-large-files-to-the-sharepoint-documentlibrary-using-rpc-from-a-desktop-application-c/.

Also, you can visit the following post to upload files up to 10GB to SharePoint Online,

https://realmpksharepoint.wordpress.com/2016/07/22/upload-large-files-to-sharepoint-online/.

Create, Update, Delete a List Item in SharePoint 2013 by Client Object Model

Create List Item:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace CreateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:7777/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("ListTitle");
            ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(listCreationInformation);
            oListItem["Title"] = "Hello World";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}

Update List Item:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

using System;
using System.Collections.Generic; 
using System.Linq;
using System.Text; 
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

namespace UpdateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:7777/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("ListTitle");
            ListItem oListItem = oList.GetItemById(1);
            oListItem["Title"] = "Hello World Updated.";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}

Delete List Item:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Client;

namespace UpdateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:7777/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("ListTitle");
            ListItem oListItem = oList.GetItemById(1);         
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}