Wednesday, October 11, 2006

Programmatically Manipulating WSS v3 Solution Packages Part 1: Adding a Package to the Solution Store


This is the first installment in a multi-part tutorial I'll be publishing on the topic of how to pragmatically add, deploy, retract and delete Windows SharePoint Services v3 Solution packages. Each installment in this series will be kept very simple and focused on a particular task. These articles are intended for intermediate level SharePoint developers and as such will rely heavily on the code examples themselves to illustrate the necessary patterns and practices. Why keep it short and simple? Because when I'm surfing the web for a code snippet to get a particular job done, I usually don't want to read a bunch of background information. I just want to see some code, grab it, and then move on!

In this first article we'll take a look at how to add a Solution package to the WSS v3 Solution store. If you'd like to build the complete solution along with me as I publish these articles, then create a new C# console application in Visual Studio 2005 and copy/paste the code below into your Program.cs file. If you are just looking for the snippet of code to accomplish adding a Solution package to the WSS v3 Solution store, then skip down to the AddSolution() method listed below (reference line 38) and grab it.

Here's the code:

[Listing 1]

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Text.RegularExpressions;
6 using Microsoft.SharePoint.Administration;
7
8 namespace solmgr
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 try
15 {
16 Arguments cli = new Arguments(args);
17
18 if (cli["add"] != null && cli["lcid"] != null)
19 AddSolution(cli["add"], uint.Parse(cli["lcid"]));
20 else if (cli["add"] != null)
21 AddSolution(cli["add"], 0);
22 else
23 PrintUsage();
24 }
25 catch (Exception ex)
26 {
27 Console.WriteLine("solmgr encountered an error:\r\n");
28 Console.WriteLine(ex.Message);
29 }
30 }
31
32 /// <summary>
33 /// Adds a solution to the store
34 /// </summary>
35 /// <param name="fname">Solution package file</param>
36 /// <param name="lcid">Locale</param>
37 /// <returns>Newly added SPSolution package</returns>
38 internal static SPSolution AddSolution(string fname, uint lcid)
39 {
40 if (ValidateSolutionName(Path.GetFileName(fname)))
41 return SPFarm.Local.Solutions.Add(fname, lcid);
42 else
43 throw new ArgumentException(
44 string.Format("The solution name {0} was invalid.",
45 Path.GetFileName(fname)));
46 }
47
48 /// <summary>
49 /// Validates solution package name
50 /// </summary>
51 /// <param name="name">Solution package file name</param>
52 /// <returns>true if solution file name is valid</returns>
53 internal static bool ValidateSolutionName(string name)
54 {
55 Regex regex1 = new Regex("[\\\\/\\*\\?\"<>|]");
56 return !regex1.Match(name).Success;
57 }
58
59 /// <summary>
60 /// Prints usage information
61 /// </summary>
62 internal static void PrintUsage()
63 {
64 Console.WriteLine("solmgr usage:\r\n");
65 Console.WriteLine("\t-add <solution package>\tAdd solution to store");
66 Console.WriteLine("\t[-lcid <LCID>]\t\tLocale of solution");
67 Console.WriteLine("\r\nexample: solmgr -add mypackage.wsp\r\n");
68 }
69 }
70 }
71

To handle the parsing of command line arguments in the example above (reference line 16), I'm using R. Griffon's Command Line Parser for C# as published on The Code Project.

In you have any questions, please add a comment to this post and I'll try to answer it as soon as I can. In part 2, I'll illustrate how to remove a WSS v3 Solution from the store.

Categories: , , , , ,

No comments: