In PowerShell, object not loading though I call LoadWithPartialName

17,363

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
Share:
17,363
oglester
Author by

oglester

Believer, husband, dad, &amp; developer. I enjoy occasionally answering some questions on here and of course find many good answers.

Updated on June 04, 2022

Comments

  • oglester
    oglester almost 2 years

    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.

  • Keith Hill
    Keith Hill about 12 years
    Then execute this command after you've attempted to load the assembly and see if it is in the list: [System.AppDomain]::CurrentDomain.GetAssemblies().
  • oglester
    oglester about 12 years
    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. ??
  • Keith Hill
    Keith Hill about 12 years
    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.