in my company, all files attached to a sharepoint list are stored in one folder with the Path: https://SharePointListURL/Dokuments/
The upload of one file to this folder is no problem.
private void UploadFile(string FileName, System.Uri SharePointURL, string ListName, string SubFolder)
{
#region PreChecks
if (SharePointURL.ToString().Substring(SharePointURL.ToString().Length - 1, 1) != "/") { SharePointURL = new System.Uri(SharePointURL.ToString() + "/"); }
if(SubFolder.Substring(0,1) == "/") { SubFolder = SubFolder.Substring(1, SubFolder.Length - 1); }
if(SubFolder.Substring(SubFolder.Length - 1, 1) != "/") { SubFolder = SubFolder + "/"; }
if (!System.IO.File.Exists(FileName)) { throw new System.IO.FileNotFoundException(); }
#endregion
#region Sharepoint connection
Microsoft.SharePoint.Client.ClientContext cC = new Microsoft.SharePoint.Client.ClientContext(SharePointURL) { Credentials = System.Net.CredentialCache.DefaultCredentials };
Microsoft.SharePoint.Client.List SPList = cC.Web.Lists.GetByTitle(ListName);
#endregion
#region Define file stream
byte[] FileContent = System.IO.File.ReadAllBytes(FileName);
Microsoft.SharePoint.Client.FileCreationInformation fci = new Microsoft.SharePoint.Client.FileCreationInformation();
#endregion
#region Define FileCreationInformation
fci.Overwrite = true;
fci.Url = SubFolder + System.IO.Path.GetFileName(FileName);
fci.Content = FileContent;
#endregion
#region Uploading file
SPList.RootFolder.Files.Add(fci);
cC.ExecuteQuery();
#endregion
}
Now, I want to link this files to their target SharePoint.ListItems. I searched all the FieldValues of the ListItem but I don't know, where this path to the attached file is stored.
Can anybody tell me, how I can attach this files to the specified ListItem?
The folders http://SharePointListURL/Lists/ListName/Attachments/ItemID/
do not exist, because if you search in google there are several pages explaining, you just have to upload the file to this folders.
Thank you, Jan