My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and index you

Export/Import a sharepoint list/document library

Content Import/Export:

The Microsoft.SharePoint.Deployment APIs in Windows SharePoint Services 3.0 provide a flexible set of tools for migrating content between Windows SharePoint Services Web sites. Windows SharePoint Services uses the concept of the content migration package, which can include either single or multiple XML files, and then provides a flexible set of APIs to enable exporting and importing the migration packages. The content import/export feature of the deployment object model allows you to export not only Web site content but also existing dependencies, like security features, user roles, versioning data, workflows, and other metadata.

Content Migration Using the Deployment Object Model:

The import/export features of the Microsoft.SharePoint.Deployment object model uses two key objects:

Microsoft.SharePoint.Deployment.SPImport and
Microsoft.SharePoint.Deployment.SPExport.
However, before you run the import or export operations using these objects, you must first specify your import or export settings using Microsoft.SharePoint.Deployment.SPImportSettings and Microsoft.SharePoint.Deployment.SPExportSettings objects. Then you simply call the Run() method on the SPImport or SPExport object, as appropriate.

Example:

The following code illustrates how to use the deployment object model to export a SharePoint list from a source location and import it to a destination location.




using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Deployment;
using Microsoft.SharePoint.Administration;
using System.Net.Mail;
using System.Windows.Forms;

namespace TreeView1
{
class ImportExport
{


#region Export Data
///
/// Export data into log folder
///

/// Source site url
/// Destination site url
/// List to be exported
/// Name of folder where we store exported data
/// Complete path of the exported data
///
public Boolean ExportData(string sourceSiteURL, string destinationSiteURL,
string listName, string folderPath, ref string folderName)
{
SPList sourceList = null;
Boolean isExportCompleted = false;

try
{
#region Try

try
{
#region Try of Source site object
using (SPSite sourceSite = new SPSite(sourceSiteURL))
{
#region Open Source site

try
{
#region Try of Source web object
using (SPWeb sourceWeb = sourceSite.OpenWeb())
{
#region Open Source Web object

#region Check list exist or not
if (CheckListExists(listName, sourceWeb, ref sourceList))
{
#region If list exist
//Create Export Object and update the Setting for the Export object
SPExportObject exportObject = new SPExportObject();
exportObject.Id = sourceList.ID;
exportObject.Type = SPDeploymentObjectType.List;

//Create the Export Setting object and update the setting properties.
SPExportSettings settings = new SPExportSettings();
settings.SiteUrl = sourceWeb.Url;
settings.ExportMethod = SPExportMethodType.ExportAll;
folderName = folderPath +
"\\" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_
" + DateTime.Now.Year.ToString() + "_
" + DateTime.Now.Hour.ToString() + "_
" + DateTime.Now.Minute.ToString() + "_
" + DateTime.Now.Second.ToString();
settings.FileLocation = folderName;
settings.FileCompression = false;
settings.IncludeSecurity = SPIncludeSecurity.All;
settings.ExcludeDependencies = true;

// Add the Export object to the ExportSetting Object
settings.ExportObjects.Add(exportObject);

// add the Export Settings to the SPExport object and Run the Export .
SPExport export = new SPExport(settings);
export.Run();
isExportCompleted = true;
#endregion

}
else
{
#region If list not exist
isExportCompleted = false;

#endregion
}
#endregion

#endregion

}
#endregion
}
catch (Exception ex)
{
#region Catch :: Source web object not exist
isExportCompleted = false;
#endregion
}

#endregion
}
#endregion
}
catch (Exception ex)
{
#region Catch :: source SiteURL not exist
isExportCompleted = false;
#endregion
}

// ("Successfully exported list=" + listName, Log.MessageType.Information);
#endregion
}
catch (AccessViolationException accException)
{
#region Catch for AccessViolationException
isExportCompleted = false;

#endregion
}
catch (Exception ex)
{
#region General Exception
isExportCompleted = false;
throw;
#endregion
}
return isExportCompleted;
}
#endregion

#region Import Data
///
/// This method will import the data to Destination Localtes
///

/// Source site url
/// Destination site url
/// list Name
/// Complete path of exported file
public static Boolean ImportData(string sourceSiteURL, string destinationSiteURL,
string listName, string folderName)
{
bool isImported = true;
try
{
#region Try

try
{
#region Try of Destination site
// Get the Site collection Url from the config file.
using (SPSite rootSiteColl = new SPSite(destinationSiteURL))
{
#region Open Destination site

SPWebApplication webApp = rootSiteColl.WebApplication;

// Set the Allow unsage update to true to import the content to the locale site
rootSiteColl.AllowUnsafeUpdates = true;
try
{
#region Try of Destination web object
using (SPWeb rootWeb = rootSiteColl.OpenWeb())
{
#region Open web object of Destination site

// Update the Below Setting to Import the Site
rootWeb.AllowUnsafeUpdates = true;
webApp.FormDigestSettings.Enabled = false;
webApp.FormDigestSettings.Expires = false;
// Create the Import settings object and update the setting to the object
SPImportSettings importSettings = new SPImportSettings();
importSettings.SiteUrl = rootSiteColl.Url;
importSettings.WebUrl = rootWeb.Url;
importSettings.FileLocation = folderName;
importSettings.FileCompression = false;
importSettings.RetainObjectIdentity = false;

//Add import setting object to the SPImport object and run the Import
SPImport import = new SPImport(importSettings);
import.Run();
isImported = true;

// Revert back Setting for SiteCollection,Webapplicaiton and Site
rootWeb.AllowUnsafeUpdates = false;
webApp.FormDigestSettings.Enabled = true;
webApp.FormDigestSettings.Expires = true;
rootWeb.Close();

#endregion
}
#endregion
}
catch (Exception ex)
{
#region Catch :: Destination web object not exist
isImported = false;
#endregion
}

rootSiteColl.AllowUnsafeUpdates = false;
#endregion
}
#endregion
}
catch (Exception ex)
{
#region Catch :: Destination url not exist
isImported = false;
#endregion
}

#endregion

}
catch (AccessViolationException accException)
{
#region Catch for AccessViolationException
isImported = false;
#endregion
}
catch (Exception ex)
{
#region General Exception
isImported = false;
#endregion
}
return isImported;
}
#endregion

#region Import List
private void ImportList(string sourceSiteURL, string destinationSiteURL, string listName)
{
#region Variables
string exportFolderName = string.Empty;
#endregion
try
{
#region Try


if (!string.IsNullOrEmpty(sourceSiteURL) && !string.IsNullOrEmpty(destinationSiteURL))
{
if (ExportData(sourceSiteURL,destinationSiteURL,listName, "
C:\\AMITKUMAR\\LogFiles", ref exportFolderName))
{
ImportData(sourceSiteURL,destinationSiteURL,listName, exportFolderName);
}

}



#endregion
}
catch (Exception ex)
{
throw ex;
}
}
#endregion


#region Check List exist or not
///
/// Check list exist or not
///

/// list name
/// pass SPWeb object
/// pass SPList object as a Ref, it will return the list ref if exist
///
private bool CheckListExists(string listName, SPWeb web, ref SPList list)
{
try
{
//---Check list exist or not
if (web.Lists[listName] != null)
{
//--Get list reference
list = web.Lists[listName];
return true;
}
return false;
}
catch (ArgumentException ex)
{
return false;
}
catch (Exception ex)
{
return false;

}
}
#endregion
}
}






Content Migration Using the Command-Line Tool STSADM:


The command-line tool STSADM.exe supports only basic import and export operations and is only useful when importing or exporting entire SharePoint Web sites or when reparenting a Web site. Content migrated using this tool will not retain object GUIDs. Note that you cannot use this utility to import or export individual items or lists.

Export Example:

The following example illustrates how to export the contents of a Web site using the command-line tool STSADM.exe.


stsadm.exe -o export
-url
-filename
[-overwrite]
[-includeusersecurity]
[-haltonwarning]
[-haltonfatalerror]
[-nologfile]
[-versions <1-4>
1 - Last major version for files and list items (default)
2 - The current version, either the last major or the last minor
3 - Last major and last minor version for files and list items
4 - All versions for files and list items]
[-cabsize ]
[-quiet]



Import Example:

The following example illustrates how to import the contents of a Web site using the command-line tool STSADM.exe.


stsadm.exe -o import
-url
-filename
[-includeusersecurity]
[-haltonwarning]
[-haltonfatalerror]
[-nologfile]
[-updateversions <1-4>
1 - Add new versions to the current file (default)
2 - Overwrite the file and all its versions (delete then insert)
3 - Ignore the file
4 - Terminate with conflicts]
[-quiet]


Reference : MSDN

Comments

Unknown said…
I attempted to use your code today. I put it in its own class, and then called Export and Import. I am basically trying to move a list from a subsite to the root site of a site collection. I put the code in a web part on the default page of the root site and ran it. I get the all-too-common error message that Updates are not allowed on GET requests.

I see that you have AllowUnsafeUpdates=true in the code, but it doesn't seem to be working. Any other ideas?
Amit Kumar said…
@GManrodt:Hi, you are using code in Web part, in that case you need to call function using RunWithElevatedPrivileges method provided by the SPSecurity class.

SPSecurity.RunWithElevatedPrivileges(delegate() {

});

Popular posts from this blog

Sitecore GraphQL Queries

Twenty 20 Masti:

Sitecore Experience Edge GraphQL Queries