public async Task <IActionResult> td([FromBody] DocumentModel[] docs) { if (docs.Length == 0) { return(new NoContentResult()); } SMB2Client client = new SMB2Client(); string site = docs[0].site; string url = _baseurl + "sites/" + site; string listname = docs[0].list; Guid listGuid = new Guid(listname); using (ClientContext cc = AuthHelper.GetClientContextForUsernameAndPassword(url, _username, _password)) try { SMBCredential SMBCredential = new SMBCredential() { username = Environment.GetEnvironmentVariable("smb_username"), password = Environment.GetEnvironmentVariable("smb_password"), domain = Environment.GetEnvironmentVariable("domain"), ipaddr = Environment.GetEnvironmentVariable("ipaddr"), share = Environment.GetEnvironmentVariable("share"), }; var serverAddress = System.Net.IPAddress.Parse(SMBCredential.ipaddr); bool success = client.Connect(serverAddress, SMBTransportType.DirectTCPTransport); NTStatus nts = client.Login(SMBCredential.domain, SMBCredential.username, SMBCredential.password); ISMBFileStore fileStore = client.TreeConnect(SMBCredential.share, out nts); List list = cc.Web.Lists.GetById(listGuid); List <Metadata> fields = SharePointHelper.GetFields(cc, list); //List list = cc.Web.Lists.GetByTitle(listname); for (int i = 0; i < docs.Length; i++) { string filename = docs[i].filename; string file_url = docs[i].file_url; var inputFields = docs[i].fields; var taxFields = docs[i].taxFields; FileCreationInformation newFile = SharePointHelper.GetFileCreationInformation(file_url, filename, SMBCredential, client, nts, fileStore); ///FileCreationInformation newFile = SharePointHelper.GetFileCreationInformation(file_url, filename); if (newFile == null) { _logger.LogError("Failed to upload. Skip: " + filename); continue; } File uploadFile; if (docs[i].foldername == null) { uploadFile = list.RootFolder.Files.Add(newFile); } else { string foldername = docs[i].foldername; string sitecontent = docs[i].sitecontent; //Folder folder = list.RootFolder.Folders.GetByUrl(foldername); Folder folder = SharePointHelper.GetFolder(cc, list, foldername); if (folder == null && taxFields != null) { folder = SharePointHelper.CreateDocumentSetWithTaxonomy(cc, list, sitecontent, foldername, inputFields, fields, taxFields); } else if (folder == null) { folder = SharePointHelper.CreateFolder(cc, list, sitecontent, foldername, inputFields, fields); } //cc.ExecuteQuery(); uploadFile = folder.Files.Add(newFile); } _logger.LogInformation("Upload file: " + newFile.Url); ListItem item = uploadFile.ListItemAllFields; if (taxFields != null) { var clientRuntimeContext = item.Context; for (int t = 0; t < taxFields.Count; t++) { var inputField = taxFields.ElementAt(t); var fieldValue = inputField.Value; var field = list.Fields.GetByInternalNameOrTitle(inputField.Key); cc.Load(field); cc.ExecuteQuery(); var taxKeywordField = clientRuntimeContext.CastTo <TaxonomyField>(field); Guid _id = taxKeywordField.TermSetId; string _termID = TermHelper.GetTermIdByName(cc, fieldValue, _id); TaxonomyFieldValue termValue = new TaxonomyFieldValue() { Label = fieldValue.ToString(), TermGuid = _termID, }; taxKeywordField.SetFieldValueByValue(item, termValue); taxKeywordField.Update(); } } DateTime dtMin = new DateTime(1900, 1, 1); Regex regex = new Regex(@"~t.*"); if (inputFields != null) { foreach (KeyValuePair <string, string> inputField in inputFields) { if (inputField.Value == null || inputField.Value == "") { continue; } string fieldValue = inputField.Value; Match match = regex.Match(fieldValue); Metadata field = fields.Find(x => x.InternalName.Equals(inputField.Key)); if (field.TypeAsString.Equals("User")) { int uid = SharePointHelper.GetUserId(cc, fieldValue); item[inputField.Key] = new FieldUserValue { LookupId = uid }; } //endre hard koding else if (inputField.Key.Equals("Modified_x0020_By") || inputField.Key.Equals("Created_x0020_By") || inputField.Key.Equals("Dokumentansvarlig")) { StringBuilder sb = new StringBuilder("i:0#.f|membership|"); sb.Append(fieldValue); item[inputField.Key] = sb; } else if (match.Success) { fieldValue = fieldValue.Replace("~t", ""); if (DateTime.TryParse(fieldValue, out DateTime dt)) { if (dtMin <= dt) { item[inputField.Key] = dt; _logger.LogInformation("Set field " + inputField.Key + "to " + dt); } else { continue; } } } else { item[inputField.Key] = fieldValue; _logger.LogInformation("Set " + inputField.Key + " to " + fieldValue); } } item.Update(); } try { await cc.ExecuteQueryAsync(); Console.WriteLine("Successfully uploaded " + newFile.Url + " and updated metadata"); } catch (System.Exception e) { _logger.LogError("Failed to update metadata."); Console.WriteLine(e); continue; } } } catch (System.Exception) { throw; } finally { client.Logoff(); client.Disconnect(); } return(new NoContentResult()); }