Hi all,
I'm really a novice at SP programming, but I went through most of the posts and even the basics doesn't look like to work for me.
I would like to upload some files to SP site.
For this reason I wrote (copied from everywhere :) ) the below script (I'm not even at the point of uploading file, I'm just trying to connect)
#Load SharePoint client dlls
$a = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$ar = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
if( !$a ){
$a = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
}
if( !$ar ){
$ar = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
}
if( !$a -or !$ar ){
throw "Could not load Microsoft.SharePoint.Client.dll or Microsoft.SharePoint.Client.Runtime.dll"
}
$assemblies = @( $a.FullName, $ar.FullName, "System.Core")
#Add overload to the client context.
#Define new load method without type argument
$csharp = "
using Microsoft.SharePoint.Client;
namespace SharepointClient
{
public class PSClientContext: ClientContext
{
public PSClientContext(string siteUrl)
: base(siteUrl)
{
}
// need a plain Load method here, the base method is a generic method
// which isn't supported in PowerShell.
public void Load(ClientObject objectToLoad)
{
base.Load(objectToLoad);
}
}
}"
Add-Type -TypeDefinition $csharp -ReferencedAssemblies $assemblies
$site = "My-site-root"
$listname = "Certificates"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
[Microsoft.SharePoint.Client.Web]$web = $context.Web
[Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($listName)
$context.executeQuery()
$web = $context.Web
$context.load($web)
When I run that I get 2 errors:
1. Load (which I thoght could be remediated by above $csharp code)
Cannot find an overload for "Load" and the argument count: "1".
At C:\temp\uploadCertToSharepoint.ps1:69 char:14
+ $context.load <<<< ($web)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
2. $web variable is empty, however it seems that executeQuery is done, since in $context I see the server version, absent before executing the query
If I run a few lines in powershell
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$context.ExecuteQuery()
Either $context.web or $context.site return the error
format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
+ CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand
Can someone point me in the right direction please? What am I doing wrong?
Thanks,
Marco










