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.