Creating Lists/Libraries with Data using PowerShell

Creating SharePoint Lists/ Libraries

In any SharePoint development/migration/moving data across sites, we come across this typical requirement.

  • A typical power user solution can be to save the list as a template and restore the template. This has a lot of limitations, including :
    • Cannot save content properly when data crosses threshold for a list;
    • Cannot save content or restore content when metadata or lookup references are used;
    • Is not a scalable solution where there are probabilities of lots of edits/updates going ahead
  • Another typical custom solution is using code to create the initial list. Deploy the list through a deployment. Some limitations of this approach can be:
    • Every time data needs to be added/edited, code needs to be changed.
    • Development knowledge is needed, but deployment can be hectic with permission issues and challenges

An approach that has gained popularity in this context is the use of PowerShell. Data for the list is generally stored in an xml, which is easily readable and editable.

In this section we will look into the approach of using PowerShell.

PowerShell Solution

Let us follow the best practice defined in this regard.

  1. We will create an xml to keep the data
  2. We will create a power shell to read the xml values, create the list, and add the values

Defining the Xml

Let us define a very simple list, name it as test list, and we will add two columns that takes text as input.

We will not focus on how to add different types of columns, as they can be easily derived using SharePoint manager or they can be found in the various blogs on the internet.

Let us create an xml and name it listProvision.xml. The below mentioned values goes into the xml, and are self-explanatory.

Now let us create another xml that stores data for the list. Ideally, we should create separate xml to create schema and data, to make sure scalability and extensibility can be achieved when we handle multiple lists.

Let us name this xml as listdata.xml.

Now let us create the PowerShell to use these two xml to create the list and update the list columns with the mentioned data. This script will take care of creating lists, with any column types based on listProvision.xml. All you have to do is make sure this script points to the xml file properly.

Once done, we will use the below script to add rows of data to existing lists based on listData.xml values.

April 4, 2016