views:

215

answers:

2
RunspaceConfiguration psConfig = RunspaceConfiguration.Create();
Runspace psRunspace = RunspaceFactory.CreateRunspace(psConfig);
psRunspace.Open();
using (Pipeline psPipeline = psRunspace.CreatePipeline())
            {

            // Define the command to be executed in this pipeline
            Command command = new Command("Add-spsolution");

            // Add a parameter to this command
            command.Parameters.Add("literalpath", @"c:\project3.wsp");

            // Add this command to the pipeline 
            psPipeline.Commands.Add(command);


                // Invoke the cmdlet
            try
            {
                Collection<PSObject> results = psPipeline.Invoke();
                Label1.Text = "hi"+results.ToString();
                // Process the results
            }
            catch (Exception exception)
            {
                Label1.Text = exception.ToString();// Process the exception here
            }

        }

It is throwing the exception:

System.Management.Automation.CommandNotFoundException: The term 'add-spsolution' is not recognized as the name of a cmdlet, function, script file, or operable program.

Any suggestions why?

A: 

You will have to load the Microsoft.SharePoint assembly

[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")

before you run the add-spsolution cmdlet

Shoban
Should i write the above line in my c# code???Bcoz when i do it gives a compilation error.....where exactly shouls i write it??
Ramnik
That's a line of PowerShell code, obviously you need to execute it within the PowerShell runspace.
Joey
yes Johannes is right,, Its a powershell cmdlet so before you run "add-spsolution" you have to run the above line of cmdlet.. The error says it clearly "Dude.. I dnt know what add-spsolution means... What r u trying to do?"... now when we load the assembly it will tell powersheel what it shuld do when u run add-solution
Shoban
Thanx for your suggestions.I m trying to execute a powershell command using c#.and when i do so the above exception is thrown.Now can you tell me what to do?
Ramnik
-1: loading an assembly via reflection does not make any cmdlets within available to execute.
x0n
Care to explain a lil bit?
Shoban
Okay.. got it...Have worked with powershell but have not done it programatically. +1 for ur post. :)
Shoban
+1  A: 

You must use the import-module command to load the correct module for sharepoint. Use get-module to find available modules.

To do this programmatically, see my post on the subject:

http://www.nivot.org/2010/05/03/PowerShell20DeveloperEssentials1InitializingARunspaceWithAModule.aspx

-Oisin

x0n