At first you must have loaded the SharePoint PowerShell extension. After that you need to load the SPFolderImport script itself. In my case it’s in the same directory as the demo script. You could rename “SPFolderImport.ps1” to “SPFolderImport.psm1” and copy it to you personal PowerShell Module store in your Windows user profile or to the local computer PowerShell module repository.
The next step is to specify an output folder. There the import structure will be created.
The last four lines belong to 1 function call. This is the call of the “SPFolderImport” function.
Parameter list for “SPFolderImport” function:
folder to import to SharePoint document library
folder for the import structure created by the script
destination URL for the imported folder. It’s not the root folder of an document library. Instead it the URL of the folder that will be created during import!
here you can specify Content Type mappings for the import process. For each mapping you need to add a hash table like this:
(switch) If set the objects (folders and files) properties “Created” and “Modified” will set to the file system attributes “CreationTime” and “LastWriteTime” of the objects.
default: false = do not use file system time stamps
(switch) If set the script will not write any output using “write-host”. Otherwise it reports the steps of processing.
3. Let’s have a look at the process.
This is the root folder of my demo data:
I’m working on a team site with URL “http://sharepoint.local”
On the site I’ve created two site content types “Document 2” and “Folder 2”:
“Document 2” has an additional site columns (“Start Date”). This and “Title” column are mandatory:
“Start Date” has a default value of data type “Date”: “Today”. Instead “Title” has no default value. => This is interesting later.
Now I create a document library: “My SPFolderImport test library”:
Than I enabled “Content Type management” in the library settings (=> Advanced Settings) and added my new content types “Document 2” and “Folder 2” and added columns “Title” and “Start Date” to the default view.
Than I copied the the link of the library’s source folder and inserted it in the demo script extended by the name of the destination import folder:
BEFORE execution the library is empty:
Than I execute the “demo.ps1” script.
This is the script output:
Lets have a look into the output folder that contains the import structure:
This contains all nessessary files and information to feed the Content Migration API of SharePoint and import the files and folders to the document library.
This is the document library AFTER import:
Inside the “SPFolderImport” folder of the document library:
Inside “Folder 2 (DOCX)”:
As you see:
The “modified” date is “now”
The “modified by” user is the user that executes the script
The required “Start Date” column is filled with it’s default value (“Today”)
the required “Title” column ist empty (!!!)
Let’s edit one files properties:
As you see:
the Content type is set to “Document 2” like specified in the mapping in the demo script
the “title” field is empty => you will not be able to save the document properties now without entering a value for “Title”!
Let’s have a look on a folders properties:
The content type is set as specified in the mapping in the demo script
I’ve tested the script in many ways
Folder in default “Shared Documents” library on site collection root web
Folder in default “Shared Documents” library on sub web of a site collection
Sub Folder in default “Shared Documents” library on sub web of a site collection
all that with custom document library created in the browser.
Import versions of files
Implement user mapping or file and/or folder import
If you intend to use this in an production environment please do lots of tests before you go ahea!