Thursday, January 18, 2007

"Manually" importing Sitecore Items

Well back from Ukraine, I better deserve the kind mention on Alexey's blog with a couple of Sitecore hints.

As many others I've on occassions spend some time struggling with packages in Sitecore. Especially with large packages, the scaling of the build-in packager doesn't seem optimal (at least in versions prior to, and when running through a webbased environment the entire installation process of a large package could easily fail do to browser / IIS timeouts.
All in all, I must admit to having been a bit annoyed with the entire webbased architecture of the packager - it was difficult to generate a package as part of an automated build-process, it was difficult to work with programmatically and as mentioned, it would crash often (it does seem to look somewhat better in Sitecore 5.3). So naturally I set out on a small quest to make my own console-based packager for Sitecore, however still based on the same package format (zip files with a mixture of files and xml-items). The goal of this tool was to run in a build-test enviroment for packages doing the following:
  1. After a new compilations of the components of a package is completed, extract the items and files necessary and make a package automatically
  2. Automatically install a number of packages on a dev-server to prepare it for regression-testing.
  3. A tool for manually syncronising certain items in Sitecore
I examined the architecture of the packages a bit and decided that it would probably be easiest to let my source-control system handle the files, extract them to a "files" folder, then add the items extracted through the Sitecore WebService (from a list of items defined in a source-controlled file) and put them as xml files in an "items" folder, then add meta-data and zip the two folders. However I never got my solution working - my best guess is that zip-format used by Sitecore isn't standard. This could probably be brought to work - but not in the time-frame I had available.

However for the second task I did manage to create a small, but ugly, tool that could assist in manually installing packages (the ordinary packager crashing was a good motivation here).
Since I never completed the tool, I won't put it here for download, but let me just share the code-bit that uses the Sitecore webservice (located at "/sitecore/shell/webservice/service.asmx") for updating items. Perhaps it will be of use to someone out there facing the same problems as I was.
To manually install items extract the items from a sitecore (5.2) package using a zip-program. Then run this code on the folder containing the items.

string importpath = args[1];
ws.Credentials c = new;
c.UserName = Properties.Settings.Default.Username;
c.Password = Properties.Settings.Default.Password;
ws.VisualSitecoreService service = new;
List<String> Paths = new List<string>();
GetPaths(importpath, ref Paths);
foreach (string p in Paths.ToArray())
StreamReader sr=File.OpenText(p);
string xml = sr.ReadToEnd();
string db = "";
string id = "";
ExamineItem( p, ref db, ref id);
XmlNode xn=service.InsertXML(id, xml, false, db, c);
if (xn.InnerText == "ok") Console.WriteLine("Success: " + p);
else Console.WriteLine(xn.InnerText+" "+p);

The "ws" is a namespace pointing to a web service reference, the credentials used is the admin login for Sitecore and it uses the method "ExamineItem" for some simple string manipulation (getting the database and db-item-path from the file-path of the item xml file).

No comments: