Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello I'm trying to access a sharepoint list from powershell. The powershell script is executed from a c# application.

If I run the from c# application I get the following exception:

Exception calling ".ctor" with "1" argument(s): "The Web application at http://pc/websites/Test4/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application."

My Code:

C#

     string cmdArg = "C:\\Scripts\\GroupChangeGroup.ps1 1";
                    Runspace runspace = RunspaceFactory.CreateRunspace();
                    runspace.ApartmentState = System.Threading.ApartmentState.STA;
                    runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
                        runspace.Open();
                    Pipeline pipeline = runspace.CreatePipeline();

 Command newcom = new Command("Add-PSSnapin");
                      newcom.Parameters.Add("Name", "microsoft.exchange.management.powershell.e2010");
                      pipeline.Commands.Add(newcom);

                  Command newcom2 = new Command("Add-PSSnapin");
                  newcom2.Parameters.Add("Name", "Microsoft.SharePOint.POwershell");
                  pipeline.Commands.Add(newcom2);

                    pipeline.Commands.AddScript(cmdArg);
                    pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
                    Collection<PSObject> results = pipeline.Invoke();
                    var error = pipeline.Error.ReadToEnd();
                    runspace.Close();

Powershell

$site = New-Object Microsoft.SharePoint.SPSite("http://pc/websites/Test4/")
$web = $site.OpenWeb()
$list = $web.Lists["GroupsList - ListInstance1"]
$listitem = $list.Items.GetItemByID(3)

If I execute the application from PS there is no problem. (same user context)

The User is Member of WSS_ADMIN_WPG

share|improve this question

1 Answer 1

SharePoint does not work with .Net 4.0. PowerShell runs with .Net 3.5, that's why it works with PS.

You should switch back to .Net 3.5 SP1.

Also, be sure to set up your project to not run with X86 compilation settings.

Set it to AnyCPU or x64.

share|improve this answer
    
I changed the Framework to 3.5 and set Target CPU to x64 without success. I do not think that it is framework that makes the trouble, because the sharepoint actions are in a powershellscript? ... –  HW90 Jun 27 '12 at 8:34
    
try to add in you script $farm = Get-Farm and check if it's loaded correctly. –  Steve B Jun 27 '12 at 8:45
    
I thik you mean $farm = Get-SPFarm? If I execute this just in a ps-script it works. If I start the script from c# I get following failure: The term 'Get-SPFarm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. –  HW90 Jun 27 '12 at 9:11
    
In the script, did you include Add-PSSnapin Microsoft.SharePOint.POwershell ? if no, you should do. It's not mandatory regarding the content of your script, but it may help to diagnose issues. –  Steve B Jun 27 '12 at 9:21
    
I've edited my questions code on your comment, but I still get the same error! –  HW90 Jun 27 '12 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.