I need to add a document to a document library (on-premise SharePoint 2016) and update the metadata. I have pieced together the code to add a document, as well as successfully update multiple choice fields, but I need to update an external data field which SharePoint receives from a SQL database. Is this possible?
My update code looks like this:
public class MetaData
{
public string Field { get; set; }
public string Value { get; set; }
}
private static bool UpdateDocument(string fileName, List<MetaData> metadata)
{
bool _result = false;
// Create context
using (SP.ClientContext _context = new SP.ClientContext(_url))
{
// Get library
SP.List _library = _context.Web.Lists.GetByTitle(_libraryName);
// Get root folder of library
SP.Folder _rootFolder = _library.RootFolder;
// Load data
_context.Load(_rootFolder);
_context.ExecuteQuery();
// Caculate file url
string fileUrl = string.Format("{0}/{1}", _rootFolder.ServerRelativeUrl, fileName);
SP.File _file = _context.Web.GetFileByServerRelativeUrl(fileUrl);
_context.Load(_file);
_context.ExecuteQuery();
SP.ListItem _item = _file.ListItemAllFields;
_context.Load(_item);
_context.ExecuteQuery();
foreach (MetaData _mdata in metadata)
{
SP.Field _field = _library.Fields.GetByTitle(_mdata.Field);
_context.Load(_field);
_context.ExecuteQuery();
_item[_field.InternalName] = _mdata.Value;
_item.Update();
_context.ExecuteQuery();
}
_result = true;
}
return _result;
}
FYI: The field I need to update is called 'Client'
I can set the field itself, but of course, the value is not really 'there', although it is displayed. I've read several posts on how to do it from a machine running SharePoint, but there has to be a way to do it from a client.