Showing posts with label stsadm. Show all posts
Showing posts with label stsadm. Show all posts

April 9, 2012

Command line parser like stsadmin in sharepoint console app


In our recent console application we decided to provide sharepoint Consultant administrators similar experience like stsadmin while providing custom features for administration and analysis. So we used an open source command line parser (codeplex) in order to implement command line parsing.

It has been proven a good choice. Here is how we can easily use it.

Source code:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:  using CommandLine;
   5:  using CommandLine.Text;
   6:  using Microsoft.SharePoint;
   7:  using Microsoft.SharePoint.WebPartPages;
   8:   
   9:   
  10:  namespace SPAdmin
  11:  {
  12:      class Program
  13:      {
  14:          static Options options = new Options();
  15:          static StringBuilder fileOutput;
  16:          static void Main(string[] args)
  17:          {
  18:              ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
  19:              if (!parser.ParseArguments(args, options))
  20:                  Environment.Exit(1);
  21:   
  22:              if (string.IsNullOrEmpty(options.operation))
  23:              {
  24:                  Console.WriteLine(options.GetUsage());
  25:                  Console.WriteLine("Press any key to exit...");
  26:                  Console.ReadKey();
  27:                  Environment.Exit(1);
  28:              }
  29:   
  30:              switch (options.operation.ToLower())
  31:              {
  32:                  case "Operation1":
  33:                      DoOperation1();
  34:                      break;
  35:                  case "Operation2":
  36:                      DoOperation2();
  37:                      break;
  38:              }
  39:   
  40:              Console.WriteLine("Press any key to exit");
  41:              Console.ReadKey();
  42:          }
  43:          private static void DoOperation1()
  44:          {
  45:             
  46:          }
  47:          private static void DoOperation1()
  48:          {
  49:             
  50:          }
  51:          private sealed class Options
  52:          {
  53:              [CommandLine.Option("o", "operation",
  54:                  Required = true,
  55:                  HelpText = "Name of operation\n ")]
  56:              public string operation;
  57:            
  58:   
  59:              [HelpOption(HelpText = "Dispaly this help screen.")]
  60:              public string GetUsage()
  61:              {
  62:                  var help = new HelpText("Test");
  63:                  help.AddOptions(this);
  64:                  return help;
  65:              }
  66:          }
  67:   
  68:      }
  69:   
  70:   
  71:  }

Details:
1. Create a new console app. (If you have CSK templates installed, you can create new sharepoint console app project.)
2. For defining options, add options class as an inner class to your Program.cs
3. In main method, parse the passed parameters.
4. try it with SPAdmin -o "operation1" and see what happens
Add options as you need! Nice and clean way to implement various operations.

If you have any questions you can reach out our SharePoint Consulting team here.

November 6, 2009

Backup and Restore site in MOSS 2007

It happens to everyone that you need to move the same Microsoft Office SharePoint (MOSS) site to new location. At this time you can use the inbuild commands to backup the site from existing location and restore it to somewhere else.

MOSS has a powerfull command "stsadm" using that you can do it easily.

To use stsadm commands, open the command prompt and navigate to

c:\program files\common files\microsoft shared\web server extensions\12\bin


To Backup Site type the command

stsadm -o backup -url http://servername:portnumber/siteName -filename DestinationFolderPath/Backup Filename(c:\backupfilename)

This command will create a file to destination folder containing backup of the source site.

Once you have the backup file, that can be restored in MOSS by a single commands as below in sequence.
Steps to Restore Site
  • Create a web application and create a site with same site template using Central Administrator which you have backed up.
  • Type the command
stsadm -o restore -url http://servername:portnumber/siteName -filename backupfilepath(c:/backupfilename) -overwrite

This will restore the site to your defined location.

"Make sure you have full rights on the content database to take backup or restore site"

If you have any questions you can reach out our SharePoint Consulting team here.
By: Piyush