viernes, 6 de julio de 2012

SharePoint Best Practices: PowerShell


SharePoint Best Practices: PowerShell


Introduction
Today to develop with SharePoint is often the case that we have more than one working environment, in many cases we have several development environments and at least one test environment. Of course, we also have the productive environment in which the data are real business.

In the scheme of "traditional" development of applications, i.e., outside the SharePoint world, business data is usually stored in databases, and we also tend to have several of them to the different environments we work with. Usually these bases are accessible directly by us (developers) and allows us to replicate the data housed in a database to another database, unless security restrictions, to be able to emulate scenarios through the different environments.

But what happens with SharePoint? The issue is a little different in SharePoint. The way "traditional" save data in SharePoint is through the lists, which are not so easy to replicate in other environments. The handling of lists has its peculiarities, and the storage of your data in the database is not so straightforward as with applications "traditional".

However, currently, SharePoint offers different alternatives when it comes to be able to play the content that is in a list of a server or set of servers to a list of another server or set of servers.

One of these alternatives, and on which we will focus on the article, is to make use of two PowerShell commands that can be executed through the SharePoint management console and that will allow us both export as then import the contents of a practical, fast and easy way. These commands are Export-SPWeb and Import-SPWeb respectively.

How to export the contents of a list with PowerShell
To export the contents of a SharePoint list on a server or Server Farm, SharePoint through the SharePoint management console , we must use the Export-SPWeb command, which allows us to export the contents of a SharePoint list to a file or folder on the disk. So you will have to follow the following two steps:
  1. Open the SharePoint management console as administrator.
  2. Run the Export-SPWebcommand, as shown below:
1./*** EXPORT LIST ***/
2.Export-SPWeb -Identity [WEB_IDENTITY] -Path [EXPORT_PATH] -ItemURL /Lists/[LIST_NAME] -IncludeVersions CurrentVersion –Verbose -Force -NoLogFile
Where you must set up the following parameters:
  • -Identity [WEB_IDENTITY]: It allows you to indicate the Web site from which you want to export the contents of the SharePoint list. The value of the [WEB_IDENTITY] parameter can be both the GUID and the URL of the Web site.
  • -Path [EXPORT_PATH]: It allows you to indicate the full path of the file in which the contents of the SharePoint list will be exported. If the path contains blank spaces [EXPORT_PATH] value with double quotes ("") should be enclosed.
  • -ItemURL Lists / [LIST_NAME]: Lets you specify the path of the SharePoint list that you want to export. [LIST_NAME] parameter value must be a valid name that points to the SharePoint list that you want to export.
  • -IncludeVersions CurrentVersion: It allows you to indicate which versions of the contents of the SharePoint list you want to export. In this particular case, only the current version is exported.
  • -Verbose: Lets you indicate if a log will be displayed on screen with the result of the export of the contents of the SharePoint list.
  • -Force: Allows you to overwrite the export destination file if it already exists. If he is not specified this parameter, by default the file is not overwritten.
  • -NoLogFile: It allows you to indicate that you should create a log file to export the contents of the SharePoint list.

Below is an example of how to export the contents of a SharePoint list called TestList located on the SharePoint Server develop01 to a file on disk:
1./*** EXPORT LIST ***/
2.Export-SPWeb -Identity http://develop01:8081/ -Path "C:\Develop\BF\BF.KP.Packages\ListsContents\TestList.cmp" -ItemURL /Lists/TestList -IncludeVersions CurrentVersion -Verbose -Force -NoLogFile

How to import content to a list with PowerShell
To import a SharePoint list to a server or Server Farm, SharePoint content through the SharePoint management console , we must use the Import-SPWeb command, which allows you to import the contents of a SharePoint list that has previously been exported to a file or folder on the disk, to a SharePoint Server, which can be the same server as any other that has installed the List definition in question. So you will have to follow the following two steps:
  1. Open the SharePoint management console as administrator.
  2. Run the Import-SPWebcommand, as shown below:
1./*** IMPORT LIST ***/
2.Import-SPWeb -Identity [WEB_IDENTITY] -Path [IMPORT_PATH] –Verbose -NoLogFile
Where you must set up the following parameters:
  • -Identity [WEB_IDENTITY]: It allows you to indicate the Web site on which you want to import the contents of the SharePoint list. The value of the [WEB_IDENTITY] parameter can be both the GUID and the URL of the Web site.
  • -Path [IMPORT_PATH]: It allows you to indicate the full path of the file from which the contents of the SharePoint list will be imported. If the path contains blank spaces [IMPORT_PATH] value with double quotes ("") should be enclosed.
  • -Verbose: Lets you indicate if a log will be displayed on screen with the result of the import of the contents of the SharePoint list.
  • -NoLogFile: Sets that you should create a log file to import the contents of the SharePoint list.

Below is an example of how to import the contents of the SharePoint list previously exported to the TestList.cmp file, but the SharePoint Server develop02:
1./*** IMPORT LIST ***/
2.Import-SPWeb -Identity http://develop02:8081/ -Path "C:\Develop\BF\BF.KP.Packages\ListsContents\TestList.cmp" -Verbose –NoLogFile

Exporting and importing lists SharePoint content suggestions
While in the first instance the management of the export and import of lists SharePoint content seems easy and even trivial task, it can present complications, especially when importing content from other servers.
So it will have to take into account the following issues when you export and import the contents of SharePoint lists:

  • Before you can import the contents of a SharePoint list it is necessary to have installed and enabled the corresponding definition, but the process can throw unexpected errors when attempting to create the SharePoint list again.
  • The import process and load the contents of the SharePoint list, creates a new instance of the same if it does not exist. Therefore, if you want to play in other SharePoint Server environment it will be necessary to manually delete the SharePoint list if it is that the same already exists on the target server in order to avoid potential problems with existing data.
  • If the SharePoint list you are trying to import has a field with a LookUp reference to another SharePoint list, first the contents of the referenced SharePoint list must import so that when you import the list that has the reference, you can find the contents pointed in the reference list. If the process upside down, or if the list did not exist, the import process would give unexpected results.

Finally, it is worthwhile to clarify both the export and import the contents of a SharePoint list on a server or a SharePoint Server Farm you must run the window of the SharePoint management console with user administrator privileges, because they require special permits to run the Export-SPWeb command and Import-SPWeb .

Other uses for the Export-SPWeb command and Import-SPWeb
Even though the Export-SPWeb command and Import-SPWeb to allow the export and import of lists SharePoint content were used in this article, these two commands can be used to export and import any object type SharePoint, among which to enumerate:
  • Sites collections
  • Web applications
  • Libraries
  • Lists

In Baufest

In Baufest, who build on Sharepoint, the use of PowerShell is part of our good practices in implementing what we build, always we fit in the governance of our client.
We have found that the best way to balance requested customizations and a solid methodology with Best Practices deployment is using PowerShell instead of code created in the activation of Features and other events in the packages of solutions. It is mandatory to establish when and why PowerShell - and not a compiled Feature and Solution Packages (WSPs)

Thanks to Ariel Bensussan for having collaborated with this article.

Follow us on Twitter


Follow @ Baufest