CSV file upload to Elgg website

Hello, I am building a Elgg website for my enterprise and I would like to add a feature where I can upload csv files and import data from csv files to the fields in Elgg site. I did check for plugins but nothing is available. Any recommendations?

  • Hello, please help with the recommendations to my above query

  • Can you explain in more detail what you want to do? 

    What fields and for what entities do you want to import?

    Is it from another Elgg instance?

    If so, are your Elgg versions the same?

    Do you want to fill the fields of existing entities or do you need to create them first?

    Perhaps we can figure out what you need.

    But in any case, you'll need to either create the plugin yourself or hire a developer, as there are no ready-made solutions yet.

  • Hello, sorry I missed this response from you.

    Can you explain in more detail what you want to do? - So I have CSV file with all the "add user" profile fields. I prepared a python script to import CSV file to the database. The script works and the user metadata adds to the database, but in the Elgg UI, only the user name gets added, but all other metadata I described in the CSV is not getting added.

    I am wondering if there is any core Elgg file functions to be called in my script. What is the right path to be checked for all Elgg core files, which works when we try to "Add user" in the UI?

    I use Elgg 6.1 version

    I don't see there is any latest CSV import plugin now

  • I assume you want to fill the user profile, i.e. 'description', location', 'interests' e.t.c.

    If so, then consider in your script that profile data stores via setProfileData() function unlike metadata (e.g. 'name', 'email') which we set using setMetadata()

  • Hello, not sure if it's good to paste the code here, but let me do it. When I run the below python script, the 'user two' and all metadata gets added to the DB tables elgg_entities and elgg_metadata, but on the Elgg website, under members, only name User two gets added. I still need to click on the user and edit to type all the other fields.

    So that's the issue I am facing. The above function you shared, you recommend to use that in existing python script, or do you recommend to build my own custom php script to add users through a csv file?

    csv file

    username,name,Title_name,email_id,phone_number,job_location,Therapeutic_Area,Highest_Education,University_Name,Assay_name,Projects_worked,Experience_with_Instruments,Systems_use,interests,skills,website,description

    User_two,User Two,Research IT Support Analyst,user.two@example.com,1234567890,India,Research IT,PhD,Stanford University,Assay1,Project1|Project2,LC-MS|GC-MS,Windows|Mac,Research|Cancer,Data Analysis|Leadership,https://johndoe.com,Experienced researcher

     

    Python script

     

    import csv

    import pymysql

    import time

     

    # Database connection configuration

    db_config = {

        "host": "localhost",

        "user": "elgg_user",

        "password": "Elgg1!",

        "database": "elgg_db",

        "charset": "utf8mb4"

    }

     

    csv_file_path = "/var/www/html/elgg/elgg-6.1.0/data/import/Sample.csv"  # Path to your CSV file

     

    try:

        connection = pymysql.connect(**db_config)

        cursor = connection.cursor()

     

        with open(csv_file_path, mode="r", encoding="utf-8") as csv_file:

            csv_reader = csv.DictReader(csv_file)

     

            for row in csv_reader:

                try:

                    # Insert a new user entity into elgg_entities

                    cursor.execute(

                        """

                        INSERT INTO elgg_entities

                        (type, subtype, owner_guid, container_guid, access_id, enabled, time_created, time_updated)

                        VALUES

                        ('user', 0, 0, 0, 2, 'yes', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())

                        """

                    )

                    connection.commit()

     

                    # Retrieve the newly created entity's guid

                    entity_guid = cursor.lastrowid

     

                    hashed_password = "$2y$10$...someHashedPassword..."  # or generate it

     

                    cursor.execute(

                        """

                        INSERT INTO elgg_users_entity

                        (guid, username, email, password_hash, name, last_login, last_action, time_created)

                        VALUES

                        (%s, %s, %s, %s, %s, %s, %s, UNIX_TIMESTAMP())

                        """, (

                        entity_guid,

                        row["username"],            

                        row["email_id"],            

                        hashed_password,            

                        row["name"],                

                        0,                          

                        0                          

                    ))

                    connection.commit()

     

                    # Insert metadata for the user

                    #    All fields except 'username' come from the CSV columns

                    #    The 'username' field also comes from CSV to store as metadata

                    metadata_queries = [

                        ("username", row["username"], "text"),

                        ("name", row["name"], "text"),

                        ("Title_name", row["Title_name"], "text"),

                        ("email_id", row["email_id"], "text"),

                        ("phone_number", row["phone_number"], "text"),

                        ("job_location", row["job_location"], "text"),

                        ("Therapeutic_Area", row["Therapeutic_Area"], "text"),

                        ("Highest_Education", row["Highest_Education"], "text"),

                        ("University_Name", row["University_Name"], "text"),

                        ("Assay_name", row["Assay_name"], "text"),

                        ("Projects_worked", row["Projects_worked"], "text"),

                        ("Experience_with_Instruments", row["Experience_with_Instruments"], "text"),

                        ("Systems_use", row["Systems_use"], "text"),

                        ("interests", row["interests"], "text"),

                        ("skills", row["skills"], "text"),

                        ("website", row["website"], "text"),

                        ("description", row["description"], "text")

                    ]

     

                    for name, value, value_type in metadata_queries:

                        # Insert into elgg_metadata

                        cursor.execute(

                            """

                            INSERT INTO elgg_metadata

                            (entity_guid, name, value, value_type, time_created)

                            VALUES

                            (%s, %s, %s, %s, UNIX_TIMESTAMP())

                            """,

                            (entity_guid, name, value, value_type)

                        )

                    connection.commit()

     

                    print(f"User {row['username']} inserted successfully (GUID={entity_guid})!")

     

                except Exception as e:

                    print(f"Error inserting user {row.get('username', 'unknown')}: {e}")

                    connection.rollback()

     

    except Exception as e:

        print(f"Database connection failed: {e}")

     

    finally:

        if connection:

            cursor.close()

            connection.close()

            print("Database connection closed.")

     

     

  • You should never insert data into the database manually, always use the Elgg APIs.

    https://learn.elgg.org/en/stable/appendix/faqs.html#should-i-edit-the-database-manually

    I see your script has a table '<prefix>users_entity' this table was dropped in Elgg 3.0, which ancient (unsupported) version of Elgg are you trying to use?

  • Hello, couple of points I would like to ask here

    1. So the issue I think here is because I inserted data manually? What are the core Elgg API functions I need to use in my script?

    2. I am using Elgg 6.1 version. The elgg_user_entity is the table which I have created. The elgg_entities and elgg_metadata tables have been already existing

    3. Can you please guide me with issue? Should I use my existing python script to call for the correct API functions or can I build a custom PHP script for this to use css file to upload user data?

    Is it possible to schedule a call here? I would highly appreciate if I can get some help here.

  • Hello, can you please help me with the above queries?

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking