Question

The title pretty much says it.

My target is to deploy a custom alerttemplates.xml to my SharePoint 2010 solution programmatically. Which would decrease the steps for an installation of the solution, since this gets done automatically when the feature gets activated.

The only Problem, this doesn't work as it should be. That's what I've done: I've created a method to execute stsadm operations from within my code; the critical part is: (Note: Normally, the arguments(string) is created dynamically and is a parameter of the method; Not shown here for simplicity, but that's how it would look like.)

string prgFilePath = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
    + @"\common files\microsoft shared\web server extensions\14\bin\stsadm.exe";
string arguments =  "-o updatealerttemplates -url http://spserver/ "
    + @"-filename + C:\custom\alerttemplates.xml";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = prgFilePath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = arguments;
worked = proc.Start();
proc.WaitForExit();

When I use the SharePoint Management-Shell and do it manually "stsadm -o update[...]" this works just fine. But this doesn't seem to work, and I can't figure out why.

Did I forget something? Is this the wrong way to do it? (Call Powershell instead and call the stsadm.exe) I'm not quite sure what to do here since I rarely work with stsadm.exe/(Power)shell related stuff. I'm just the guy that uses Visual Studio. :)

Could it be an issue that the paths in the Verbatin-strings (@"") get translated and ultimately send to the stsadm.exe as "C:\custom\alerttemplates.xml"? (Normally that's no issue, I know. But maybe, maybe in this case it is?)

I'd really appreciate when someone could give me hint on that matter.

Also: What's with filepaths that have blank spaces? Just set " in front and at the end?

Was it helpful?

Solution

In general, I've found two superior alternatives to launching stsadm as a separate process. First is using the SharePoint Object Model. In your case, this would be with the function SPAlertTemplateCollection.InitAlertTemplatesFromFile. This gets you the best error handling story.

If there's something that's not exposed via the OM or it's more work than you want to spend, you can load the stsadm assembly and call directly into it. For instance:

Assembly stsAss = Assembly.LoadFile(StsadmPath);
Type stsAdmType = stsAss.GetType("Microsoft.SharePoint.StsAdmin.SPStsAdmin");
MethodInfo main = stsAdmType.GetMethod("Main");
main.Invoke(null, new Object[] { new String[] { "-o", "updatealerttemplates" } }); // add other parameters to end of the string array

Both of these require that your program is running x64 and with admin priviliges.

OTHER TIPS

I've been successful with the following script

Process exportSite = new Process();

string commonFilesPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonProgramFiles);
string commandLine = " -o export -url " + SPContext.Current.Web.Url + " -filename c:\\" + tempName + ".exp -overwrite -includeusersecurity";

exportSite.StartInfo.UseShellExecute = true;
exportSite.StartInfo.FileName = commonFilesPath + @"\Microsoft Shared\web server extensions\12\BIN\" + "stsadm.exe ";
exportSite.StartInfo.Arguments = commandLine;
exportSite.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
exportSite.Start();
exportSite.Close();

HTH

I think the problem is with your arguments:

string arguments =  "-o updatealerttemplates -url http://spserver/ "
    + @"-filename C:\custom\alerttemplates.xml";

Remove the +.

For filepaths with spaces in your arguments, you can escape them using \". For example:

string arguments =  "-o updatealerttemplates -url http://spserver/ -filename \"C:\\custom\\my alert templates.xml\"";
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top