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();