I need to read one text file from layouts folder and need to write that file in sharepoint document library, I tried c# file stream file.openread and file.writeopen it's working for local files. I am not sure how to write the files in the sharepoint document library or some other Sharepoint URL path. Please suggest some idea to achieve this.
1 Answer
To write file in document library you can use the below code, that using the FileCreationInformation
FileCreationInformation createFile = new FileCreationInformation();
createFile.Url = "testFile.txt";
//Read the file and convert it into byte array to set content of the file
string somestring = "File content";
byte[] toBytes = Encoding.ASCII.GetBytes(somestring);
createFile.Content = toBytes;
List spList = context.Web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File addedFile = spList.RootFolder.Files.Add(createFile);
context.Load(addedFile);
context.ExecuteQuery();
ListItem item = addedFile.ListItemAllFields;
item["Title"] = "File created";
item.Update();
ctx.Load(item);
ctx.ExecuteQuery();
Console.WriteLine("File created");