How to create a drupal node programmatically


How to create a drupal node programmatically

Many a times we want to create Drupal node programatically when there is a big data in excel format or in some other format and we want it to be added in our Drupal site through cck form cause its time consuming to do that data entry manually.

If you are finding way to do that data entry programatically then here is the solution.

Suppose you want to create nodes for your content type having fields – book number, book name, author name and publication year.

If you are having data in excel format then you can read that excel and and have above code in for/while loop to create nodes for all books.

I have done this by reading excel data and created thousands of nodes for my content type. I have code to read excel file and create these nodes but its not included in this post, you can find excel reader class on Google.

Please feel free to login using google or facebook and create a forum topic to get your query answered in few hours here

Thank you.
Sachin Wable

Follow me

Latest posts by Sachin Wable (see all)

Sharing is cool...Share on Facebook
Tweet about this on Twitter
Share on LinkedIn



  1. Robert says:

    For which version of Drupal was this example intended?

  2. Admin says:

    I used this for D6. I hope this can work for D7 also.

  3. Raido says:


    For D7 there are some changes:
    1) Node should include language definition:
    $node->language = LANGUAGE_NONE;
    For specific language, LANGUAGE_NONE constant could be replaced with specific language code string – e.g: ‘en’ or ‘ru’ etc.
    2) Fields should include language definition
    So instead:
    $node->field_book_name[0][value] = “Drupal”;
    You should use:
    $node->field_book_name[$node->language][0][‘value’] = “Drupal”;
    3) node_object_prepare($node) should be called after setting $node->type – it fills node object with default values.

    So a rewrite of above example for Drupal 7:

    Hope it helps someone. 🙂

  4. Rajiv M says:

    How can we Update an already existing node using node_save?

  5. prakash sharma says:

    In the above code i have used node_save in while loop, but it is only saving the last row which is coming by loop data.

    Is there anything we can do so each node save properly and then loop goes further?

  6. Dima says:

    Thanks, this helped!! If you use this to create a node from a custom form and want to get the user that is logged in try the following:

  7. MArk says:

    Where would this file live in the directory structure?

    • Sachin Wable says:

      Hi Mark,

      You can put this code in any modules hook depending on your need. Recommended way is you create a custom module and implement approprite hook in .module file of your custom module and put this code in it.

      Hope this helps. Please feel free to reply if you need more help on this.

      Thank you,

    • MArk says:

      Thank you sir.

Leave a Reply

Your email address will not be published. Required fields are marked *