Wednesday 4 March 2015

Export/Import SharePoint List/Library using PowerShell

Hi all! I am back again as promised with my next blog. In my last blog (Migrate SharePoint List\Library from one site to another site in different server) I discussed how we can take a backup or export a SharePoint list or library from source site and restore or import it in target site using 'save as template' approach and the 'Granular Backup' approach.

Now we will explore the most flexible way of doing it and undoubtedly the most powerful way...The PowerShell! PowerShell is a tool based on .Net framework. Commands that I will be discussing here can also be used in SharePoint Management Shell. I will be showing how to use these commands in PowerShell. Here are links to different sections of this article.



How to use Export Command

First we need to export the list/library from source site in any server,right? For that we have a very handy cmdlet i.e Export-SPWeb. This is also possible through stsadm command line but since that is deprecated since SharePoint 2010 its use is not encouraged. Actually the export of library is a new feature that is added in SharePoint 2013. So lets see how this command works.

Add-PSSnapin Microsoft.SharePoint.PowerShell
cls
Export-SPWeb -Identity "URL of source site" -path "Path\FileName.cmp"
-ItemUrl "internal name of list\library" -IncludeUserSecurity                                                          -IncludeVersions "CurrentVersion" -NoFileCompression -Verbose
Write-Host "List Backup Done!"

This is a very simple script to export a list.
For those of you who are completely new to the PowerShell, don't panic I am gonna walk you through all the statements and parameters.
First statement 'Add-PSSnapin' is used to add one or more snap ins to current PowerShell session. Here it registers a set of cmdlets specific to SharePoint. You can think of it as '#include' in C. :-)
'cls' is just used to clear output screen to make it look neat and less confusing. Next comes the main command- Export-SPWeb. You can use this command to export a site collection,list or library.
Its various parameters and their functions are as follows :
  • Identity - Here we have to pass the url of the site which contains the list we have to export.
  • Path - This specifies the path (including the file name with .cmp extension) where the backup of the list or site is stored.
  • ItemUrl - Here we have to pass the url of the list or library. For example for a list it may be like "/lists/mylist". Note: For libraries it will be like "/LibraryName" without "/lists/". You can get this from the url of the list in the browser.
  • IcludeUserSecurity - This parameter, if present will preserve the security settings of the list that we are exporting (only if the list doesn't have a broken inheritance or item level permission).
  • IncludeVersions - This indicates the list item version history to include in the export operation. It can take values of - LastMajor | CurrentVersion | LastMajorAndMinor | All. If we don't use this parameter it takes default value i.e LastMajor.
  • NoFileCompression - This parameter is used to enable or disable file compression. If you have large number of items you can use file compression to increase your performance. It is estimated to increase the performance by 30%.
There are many more parameters. If you want a detailed explanation you can visit this site.
Note: If you want to overwrite existing export files in the given path you can use -Force parameter.

Sounds simple right! Just write the command and required parameters and you have your list backup ready with versions also. Now our aim is to import it to any target site.

How to use Import Command

Add-PSSnapin Microsoft.SharePoint.PowerShell
cls
Import-SPWeb -Identity "Your target site url" -UpdateVersions Overwrite                                  -IncludeUserSecurity        -Path "yourpath\file.cmp" -NoFileCompression -Verbose
Write-host "Import Completed!"


This command here imports the list that you had exported using the Export-SPWeb command on top.
Below is the explanation for parameters I have used :
  • Identity -  We have to pass the url of the target site where we want to restore the list.
  • UpdateVersion - This parameter is used to specify what do you want to do in case the file being imported is already present in the destination library. It takes three values Add, Overwrite, Ignore. Add just adds the file with conflict as a new file with different version. Overwrite as the name suggests replaces the old files (for only the files with conflicts) with new files that were exported earlier. What it does is while importing if a file is found to be already present in the target lis then it deletes all the old versions of the file and then imports it. Ignore doesn't import the file in case of conflict. It just leaves the file as it is.
  • Other parameters work exactly the same way as in case of Export-SPWeb.
NOTE: You can use -NoFileCompression in Import command only if you have used it while exporting.
NOTE: If the library from which you have exported is already present in destination site it imports the items in that library else it creates a new library by that name.

There is an interesting scenario I will discuss here from my own experience here. These commands work perfectly even if the version setting in SharePoint library is disabled too. We had this requirement in our project and we were not sure how does these commands behave in case the version setting is disabled. So after some research I found out that you can export current version for all the items and then import as overwrite. In case of SharePoint library it compares the 'Name' field of library (which has the Name of document), to determine if the item is already present. Then it just overwrites all the other changes in item. If the 'Name' field of item is changed then it creates a new item in destination list.

So now we know how to shut the mouth of our pushing boss who wants us migrate all the SharePoint content from one server to another. I have tried my best to make it understandable and simple. I am not a professional blogger. So if there is any improvement you want please do leave comments or suggestions so that it will help us both. If there is any query feel free to ask in comments.

I'll be back! Hasta La Vista.


You can also check out my other blogs here.

9 comments:

  1. Hi Asutosh, thankyou very much for your post, it sounds good!

    I have a small question: I have a SP online site with a list that I want to move to another site within the same site-collection. The list has multiple views, a custom InfoPath form and a SP Designer workflow. Will all these elements be moved along if I use this Powershell method or do I have to recreate them afterwards?

    Any information on this would be very much appreciated, thanks in advance!

    ReplyDelete
    Replies
    1. Hi Marv,

      I am glad you liked the post :-). Not sure how you are going to use PowerShell command to take list backup from SharePoint online environment. As per my knowledge there is no command available for online environment to take list backup till now. Let me know if I am missing something.

      Delete
    2. Hi Asutosh,

      I have zero Powershell knowledge so I could be mixing stuff up here. I came across some information about the Sharepoint Online Management Shell (see: https://technet.microsoft.com/en-us/library/fp161388.aspx).

      To my understanding this is Powershell specifically for SharePoint Online, I thought I could probably use your guide to backup an SP Online list using the Management Shell.

      Does that seem logical? Please correct me if I'm wrong, I'm trying to find a good way to backup my list data.

      Thanks for your help!

      Delete
    3. Hi Marv,

      That's alright..we all learn with time :-). Yes you are correct there are set of cmdlets for SharePoint online (O365). But there are not as many cmdlets in SP online management shell as in offline. Here are the list of all the cmdlets available https://support.office.com/en-us/article/Introduction-to-the-SharePoint-Online-Management-Shell-c16941c3-19b4-4710-8056-34c034493429 . As you can see there is no cmdlet for taking list backups. Even I have never tried it but here is a link to various ways you can do it ( http://www.threewill.com/the-4-options-you-need-to-know-about-sharepoint-online-backup-and-restore/ ). If you are looking for programmatic approach check out this http://sharepoint.stackexchange.com/questions/139185/get-sp-online-site-content-via-powershell-or-c-for-backup-sharepoint-online-si . You may have found this link before while searching. Let me know if it helps!

      Delete
  2. Hey Asutosh,

    Thanks for the information. It seems a programmatic approach is the best way to get this done. I have to tell you that the SP site I mentioned earlier is a small project I'm working on for the company I work at. My job in this project is finished and someone else will take over. Doing manual backups is not a problem for them for now, I will advise them to further research a way to automate the backup process. Anyways it's not my problem anymore :P

    So again, thanks for your help and good luck with your blog!

    Greetings from the Netherlands,
    Marvin

    ReplyDelete
  3. Thank you for this post. I think this has saved me a lot of mouseclick'ing in Sharegate.

    ReplyDelete
    Replies
    1. you are welcome Svavark.. thanks for your feedback.

      Delete

Feel free to share your thoughts...