Pages

Showing posts with label export. Show all posts
Showing posts with label export. Show all posts

Tuesday, May 29, 2012

Export Import SharePoint Site using Web Service

Problem:

While working with SharePoint Online – sometimes it is required to take the backup of the site or to export the site to some other environment, but due to no access to SharePoint server, we end up with writing huge code to migrate the site contents to another site using any client object model.
If server side scenario is considered, we have lots of options to perform a simple site export and import operation , like say – use stsadm commands , use Central Administration , use powershell scripts but for SharePoint Online environment what to do? Well here comes web services in picture , how? We will see in details.

Solution:
SharePoint Web services are already there since MOSS 2007, but mostly not used. Here we are going to use the sites.asmx web service to export and import the sites.
Note that before performing these operations using web services – make sure that you have site collection administrator rights on source as well as target site.
I created a Team site as source site and added some sample data to it , say about 4000 documents + 3000 list items with attachments + some content types + some site columns + some lookup columns + some list and content type workflows.

We can use the ExportWeb function given by the sites.asmx web service which takes some parameters as input – let’s see what those are.
  • 1.       jobName: this is the string parameter and can be set to any string , using this name the cmp files will be created. Please make a note that no special characters are allowed here.
  • 2.       webUrl: url of the site which needs to be exported as cmp files.
  • 3.       Datapath: path where all cmp files will be stored (typically a path of the document library on source site)
  • 4.       Includesecurity: as name suggests – Boolean parameter to include security or not
  • 5.       Overwrite: Boolean parameter to overwrite cmp files on save location
  • 6.       cabSize: size of each cmp file , can be set to 2GB max.
  • Export web function returns an integer after completion , but mostly the description is not available for the return types. 
Here it is :

Value
Description
1
Pending: The operation is in progress.
4
InvalidExportUrl: The site specified in the webUrl is not accessible.
5
ExportFileNoAccess: The location specified in the dataPath is not accessible.
6
ExportWebNoAccess: The user does not have the required permissions to execute this operation successfully. The user MUST have the ManageLists and AddListItems permissions in the solution gallery.
7
ExportError: An error other than the errors listed in this table occurred during exporting the site. An example of a condition that would result in this error is if there was no content on the site to export.
8
UploadDataError: The content migration package file is not uploaded to the server successfully. This can occur if the specified path was invalid, read-only, the caller is not authorized to access it, or the file is already open.

ImportWeb:
While importing the web from cmp files using web services – we can use ImportWeb function of sites.asmx web service.

Important part here is – the target site on which import is to be done – should be created using OOB blank site definition. There can be an error while performing the import operation like – the source site is created using template STS#0 and target site is created using STS#1 , - solution to this is – create target site collection without any template – select the select template later option while creating target the site collection.
ImportWeb function take some parameters let’s see what those are
  • 1.       jobName: string parameter , can be set to anything – but no special characters should be included.
  • 2.       webUrl: url of target web.
  • 3.       dataFiles: this is array of string containing url of all cmp files. Example : If there are 10 cmp files in the source location – then dataFiles parameter will be any array having urls of 10 files in ascending order.
  • 4.       logPath: path where log file can be created. This can be passed as null. (typically a url of any document library)
  • 5.       includesecurity: to include security or not
  • 6.       overwrite: overwrite log files.
Similar to ExportWeb function – ImportWeb function also return an interger on completion.
Here are the details

Value
Description
1
Pending: The operation is in progress.
2
GenericError: An error other than the errors listed in this table occurred importing the site.
4
InvalidImportUrl: The site specified in the webUrl is not accessible.
5
ImportFileNoAccess: At least one location specified in dataFiles is not accessible.
6
ImportWebNoAccess: The user has insufficient permission to import to the location specified in webUrl.
8
mportWebNotEmpty: The location specified by webUrl corresponds to an existing site that is not a blank site.
11
LogFileNoAccess: The location specified by logPath is not accessible.

Ok , so after lots of theory – here is the simple console application example which I tried on my environment successfully.

static void Main(string[] args)
{
    string sourcesiteurl = "http://sourcesitecollection/";
    string targetsiteurl = "http://targetblanksitecollection/";
    string svcUrl = "_vti_bin/sites.asmx";

    Sites sites = new Sites();
    NetworkCredential credentials = new NetworkCredential("username", "password", "domain");
    sites.Credentials = credentials;
    sites.Timeout = 600000;

    Console.WriteLine("Choose..1. Export , 2. Import");
    string responce = Console.ReadLine();

    if (responce.Equals("1"))
    {
       sites.Url = string.Format("{0}{1}", sourcesiteurl, svcUrl);
       try
       {
         int result = sites.ExportWeb("MyExportTest", sourcesiteurl, "http://sourcesite/Exports", true, true, true, 1024);
         Console.WriteLine("done with result - " + result.ToString());
       }
       catch (Exception ex)
       {
          Console.WriteLine(ex.Message);
          Console.WriteLine();
          Console.WriteLine(ex.StackTrace);
       }
     }

     else if (responce.Equals("2"))
     {
         sites.Url = string.Format("{0}{1}", targetsiteurl, svcUrl);
         string[] dataFiles = new string[] { "http://sourcesite/Exports/MyExportTest.cmp" };
         
          try
          {
            int result = sites.ImportWeb("MyExportTest", targetsiteurl, dataFiles, null, true, true);
            Console.WriteLine("done with result - " + result.ToString());
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
            Console.WriteLine();
            Console.WriteLine(ex.StackTrace);
          }
       }
     Console.ReadLine();
   }



Friday, July 29, 2011

Exporting IE Bookmarks/Favorites programmatically / .NET

A thought came in my mind yesterday while I was sitting idle and doing nothing (except browsing on InternetJ).. Thought was to get the list of URLs through .NET code which we save or mark using browser IE as favorites.
We all know that when we mark as any URL as favorite using UI in IE then it gets saved in the favorites folder of user. to take a look , I opened favorites folder and found that all the shortcuts gets saved in the form of .url file. These .url files are nothing but the Internet Shortcuts which points to the location of actual URL.
So just to try some hands on I opened the developer’s heaven (VS) and started by creating a sample console application. And here is the code which I managed to achieve what I was thinking.
there is option to export the all bookmarked URL either to XML or HTML format in Firefox (at least in latest version) also google chrome has this option to export all bookmarks to HTML but when came to IE I didn’t find any option. so I have tried creating a utility which exports all your bookmarked URLs to a HTML file.
HTML file gets created in same directory where you are executing the exe.
There might be different ways doing same but I pasted what I done.. so your views and suggestions are always welcome J
class Program
{
  private static string _favouritesPath = string.Empty;
  private static string _previousDirectoryName = string.Empty;
  private static string _currentDirectory = string.Empty;
  private static string _fileName = string.Empty;

  static void Main(string[] args)
  {
    try
    {
      _favouritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
      if (!string.IsNullOrEmpty(_favouritesPath))
      {
        DirectoryInfo di = new DirectoryInfo(_favouritesPath);
        _currentDirectory = Environment.CurrentDirectory;
        TextWriter _writer = new StreamWriter(_currentDirectory + "\\BookMarks.htm", false);

        if (di != null)
        {
          FileInfo[] _files = di.GetFiles("*.url", SearchOption.AllDirectories);
          if (_files != null && _files.Length > 0)
          {
           foreach (FileInfo _file in _files)
           {
             if (_file.Exists)
             {
               _fileName = _file.Name.Split('.').FirstOrDefault().ToString();
               StreamReader _reader = _file.OpenText();
               string _allContents = _reader.ReadToEnd();
               string[] _splits = _allContents.Split(new char[] { '=', '[' }, StringSplitOptions.RemoveEmptyEntries);
               if (_splits.Length > 0 && !string.IsNullOrEmpty(_splits[1]))
               {
                 if (!string.Equals(_file.Directory.Name, _previousDirectoryName))
                 {
                   _writer.Write("</br>");
                   //<b style="color: Green;"></b>
                   _writer.Write("<b style=\"color: Green;\">" + _file.Directory.Name + "</b>");
                   _writer.Write("</br>");
                    _writer.Write(string.Format("{0}<a href=\"{1}\">{2}</a>", " ------ ", _splits[1], _fileName));
                 }
                else
                {
                  _writer.Write("</br>");
                  _writer.Write(string.Format("{0}<a href=\"{1}\">{2}</a>", " ------ ", _splits[1], _fileName));

                }

              }
             _previousDirectoryName = _file.Directory.Name;
            }
           }
          }
         }

         _writer.Close();

       }
      }
      catch (Exception ex)
      {
        Console.WriteLine(string.Format("{0}-{1}", "Error:", ex.Message));
      }
       Console.WriteLine("File Created..press any key to exit..");                   Console.ReadLine();
     }
    }