2

I have a .ps1 file that I execute from PS prompt. At the top of the file I have:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

and later in the code, it has:

$site = new-object Microsoft.SharePoint.SPSite $url;

I get the following error:

Unable to find type [Microsoft.SharePoint.SPWeb]: make sure that the assembly containing this type is loaded.

If I run the LoadWithPartialName statement from the prompt directly, then I can execute the script.

What am I doing wrong?

--Update--

When I remove the void, making the code:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

the error is unchanged because the LoadWithPartialName is executing without error.

--New Information--

It has something to do with adding a function with typed parameter of SPWeb.

This works:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$url = "http://siteurl/"
$site = new-object Microsoft.SharePoint.SPSite $url;
$site.Dispose();

And this works:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function doSomething(){ }
$url = "http://siteurl/"
$site = new-object Microsoft.SharePoint.SPSite $url;
$site.Dispose();

But this breaks if (you have to start a new PS session before it is an issue. Also it doesn't matter if the function is before or after the first instantiation of SPSite:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function doSomething(    [Microsoft.SharePoint.SPWeb] $web  ){ }
$url = "http://siteurl/"
$site = new-object Microsoft.SharePoint.SPSite $url;
$site.Dispose();

I suppose a secondary work-around is to not type the parameter or put the load in another ps1.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
oglester
  • 6,605
  • 8
  • 43
  • 63
  • If you remove the cast to [void] in the call to LoadWithPartialName in your script, to you get any meaningful message? Also, are you running in a mixed 32bit/64bit environment? – David Brabant Mar 29 '12 at 17:07
  • It is a 64bit Windows Server 2003 SP2. It is running on a VMWare host. It is using WSS rather than MOSS – oglester Mar 29 '12 at 21:55
  • Is it the 64 bit version of WSS? – Andy Arismendi Mar 30 '12 at 04:59
  • In the GAC it shows: `processorArchitecture=MSIL`. It is installed in Program Files, not Program Files (x86). So I either the version, because it is .NET, doesn't care which processor, or it is 64bit. – oglester Mar 30 '12 at 12:38
  • Also, it isn't a matter of the assembly not loading, but rather a matter of it only loading when run directly from the prompt. – oglester Mar 30 '12 at 12:41
  • @GregOgle Watch out for this syntax `new-object Microsoft.SharePoint.SPSite($url)`. You're not calling a constructor. This is a cmdlet invocation and should use the following syntax: `new-object Microsoft.SharePoint.SPSite $url`. Turns out that your syntax happens to work in this case but will fail when you have a ctor that takes more than one parameter. It would fail because this syntax `($p1, $p2)` represents an array with two elements. That array gets assigned to the first ctor parameter leaving the second parameter unassigned. – Keith Hill Apr 02 '12 at 02:17
  • @KeithHill, thanks, I'm still learning the ropes of PS. – oglester Apr 02 '12 at 13:16

1 Answers1

2

Is it possible that this assembly is not in the GAC? If not, then it would need to be in the PowerShell install dir for LoadWithPartialName to find it. FYI, the LoadWithPartialName method is obsolete and not recommended. If you're on PowerShell V2, try locating the SharePoint.dll on your file system and use Add-Type e.g.:

Add-Type -Path <path>\Microsoft.SharePoint.dll
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    Then execute this command after you've attempted to load the assembly and see if it is in the list: `[System.AppDomain]::CurrentDomain.GetAssemblies()`. – Keith Hill Mar 30 '12 at 13:49
  • It doesn't get far enough to execute the GetAssemblies. I think what is happening is that PS is checking to see if it knows what the function parameter types are before moving forward. ?? – oglester Mar 30 '12 at 15:19
  • 2
    That command should work on any Powershell system. Just fire up a fresh console, execute `[System.AppDomain]::CurrentDomain.GetAssemblies()`, then execute your assembly load command, then execute `[System.AppDomain]::CurrentDomain.GetAssemblies()` again. – Keith Hill Mar 30 '12 at 15:34