Thursday, June 24, 2021

Creating a text file with the contents of a list item in a library using Event Receiver

Hello to everyone who follows my blog and today again raised the issue of the lack of functionality in the Workflow of the MS SharePoint server, which is created using the MS SharePoint Designer. On the forum, the user created a question in which he wanted, subject to the creation, modification of a list item, etc., so that a text document in the library was created, filled with the contents (column values) of this list item.
The user tried to do this using the standard workflow tools, but faced the fact that the file created in this way could not be filled in, the user also tried to run Javascript using the Workflow, which is basically impossible. And I advised him to use the Event Receiver in this case it is the best solution. How to create an Event Receiver in his articles has already been described, for those who are interested, follow the link. So, we create an Event Receiver, select the action at which our event "item was added" will be triggered, and add the code where we call the current list item (getlistitem), create variables, a text file in the Temp folder, add Unicode support for your language, fill the file with data, add it to the document library and delete the temporary text file.
using System;
using System.IO;
using System.Text;
using Microsoft.SharePoint;

namespace EventRecieverTest.EventReceiverTestList
{
    public class EventReceiverTestList : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            using (SPWeb web = properties.OpenWeb())
            {
                try
                {
                    //getcurrentitem
                    SPListItem currentItem = properties.ListItem;
                    //create variable
                    string title = currentItem["Title"].ToString();
                    string server = currentItem["Server"].ToString();
                    DateTime Created = DateTime.Parse(currentItem["Created"].ToString());
                    //create text file
                    string path = @"C:\Temp\" + title + ".txt";
                    //add to Encoding
                    Encoding encoding = Encoding.GetEncoding("UTF-8");
                    if (!File.Exists(path))
                    {
                        using (FileStream fs = new FileStream(path, FileMode.CreateNew))
                        {
                            using (StreamWriter writer = new StreamWriter(fs, encoding))
                            {
                                writer.WriteLine("Наименование: " + title);
                                writer.WriteLine("Сервер: " + server);
                                writer.WriteLine("Дата создания: " + Created);
                                writer.Close();
                            }
                        }
                        //Add file to library
                        SPList lib_destination = (SPDocumentLibrary)web.Lists["Shared Documents"];
                        var targetFolder = lib_destination.RootFolder;
                        var fileContent = File.ReadAllBytes(path);
                        var fileUrl = Path.GetFileName(path);
                        targetFolder.Files.Add(fileUrl, fileContent);
                        //remove cash file
                        File.Delete(path);
                    }
                }
                catch (Exception ex)
                {
                    File.WriteAllText(@"C:\Temp\error.txt", ex.ToString());
                }
            }
        }
    }
}

1. We check, go to the "Test" list, create a list item, where we fill in our column data and save the list item.

2. Go to the document library and see our created text file.

3. We open the file and look at its contents, everything matches.
Happy Coding!