Connect Sharepoint remotely using Powershell

Usually working with Sharepoint list using Powershell (PWS) means you need to run a PWS script locally on the given sharepoint server or at least you have to install a “little”, playground Sharepoint (foundation or services) to be able to import the sharepoint dll at the begining of your script.

i’m just writing about this line: [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

Ohh yeah. But as a little child i’m frustrated: I don’t want to install sharepoint just to import such a dll… i would like to do something else…
And here we go. Not so simple but i think it worths doing. Every cookbook starts with the sentence: have a saucepan.

PreDos – Create your own DLL

you need to do this just one time, this DLL can be used to connect to all other default sharepoint list using webservice in the future. (Or you can download a created Lists.dll file from HERE ~70KB if you are a bit lazy)

Please install Visual Studio. (in this post i installed VS8)

We need to install the .Net SDK to a developer machine from the link: http://www.microsoft.com/downloads/details.aspx?familyid=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en 350MB

Then we need to set the environment parameters to have a well-working translator-environment (for wsdl.exe and csc.exe). I just copied the rows below and had three errors… i had no time to debug. The solution worked in this way also.

$env:VSINSTALLDIR="C:\Program Files\Microsoft Visual Studio 8"
$env:VCINSTALLDIR="C:\Program Files\Microsoft Visual Studio 8\VC"
$env:DevEnvDir="$env:VSINSTALLDIR\Common7\IDE"
$env:FrameworkSDKDir="$env:VSINSTALLDIR\SDK\v2.0"
$FrameworkPath=$([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
$env:FrameworkDir=$(split-path $FrameworkPath -Parent)
$env:FrameworkVersion=$(split-path $FrameworkPath -Leaf)
$env:PATH="$env:VSINSTALLDIR\Common7\IDE;$env:VCINSTALLDIR\BIN;$env:VSINSTALLDIR\Common7\Tools;$env:VSINSTALLDIR\Common7\Tools\bin;$env:VCINSTALLDIR\PlatformSDK\bin; $env:FrameworkSDKDir\bin;$env:FrameworkDir\$env:FrameworkVersion;$env:VCINSTALLDIR\VCPackages;$env:PATH"
$env:INCLUDE="$env:VCINSTALLDIR\ATLMFC\INCLUDE;
$env:VCINSTALLDIR\INCLUDE;$env:VCINSTALLDIR\PlatformSDK\include;$env:FrameworkSDKDir\include;$env:INCLUDE"
$env:LIB="$env:VCINSTALLDIR\ATLMFC\LIB; $env:VCINSTALLDIR\LIB;$env:VCINSTALLDIR\PlatformSDK\lib;$env:FrameworkSDKDir\lib;$env:LIB"
$env:LIBPATH="$FrameworkPath;
$env:VCINSTALLDIR\ATLMFC\LIB"

*IMPORTANT: on a 64 bit machine you need to put the first variable ($env:VSINSTALLDIR) to be “C:\Program Files (x86)\Microsoft Visual Studio 8”

Let’s generate the source for the Classes

The webservice is a cool thing as it tells everything about itself (attributes and methods). We need just to create the .Net sourcecode of it. We can use the WSDL.exe application for that. 


wsdl http://intranet/_vti_bin/Lists.asmx

Having run this command we’ll have such a code which can use the services connected to this URL by default. Now you have a CS file with the line in it:


public Lists() {
this.url = "http://intranet/_vti_bin/Lists.asmx;
}

Let’s Render the code

Running the next command we have the DLL itself (reference: http://msdn.microsoft.com/en-us/library/78f4aasd.aspx).


csc /t:library Lists.cs

And we can use our fresh new-brabded DLL at every machine (Windows ūüôā )

Use your own DLL at your PWS script

You need just to load your brand-new. dll in a normal way as you did with Sharepoint dll till now. (The DLL can be downloaded from HERE ~70KB also)


Reflection.Assembly]::LoadFrom("D:\Lists.dll")

And here we are, we can create our new List object ūüôā hurray


$list = New-Object Lists
$list.Credentials=[System.Net.CredentialCache]::DefaultCredentials

If you need other credential:


$list.Credentials = (Get-Credential).GetNetworkCredential())

So we can use methods as we have an instance –


$docs = $list.GetList(‚ÄúShared Documents‚ÄĚ)
$docs.Title
$docs.Description
$docs.ItemCount
$docs | Get-Member

# If you downloaded the Lists.dll you should change the URL property for the instance to your one.
$list.url = "http://yourintranet/listname/_vti_bin/lists.asmx"

PS d:\>$list
SoapVersion                          : Default
AllowAutoRedirect                    : False
CookieContainer                      :
ClientCertificates                   : {}
EnableDecompression                  : False
UserAgent                            : Mozilla/4.0 (compatible; MSIE 6.0; MS We
 b Services Client Protocol 2.0.50727.495
 2)
Proxy                                :
UnsafeAuthenticatedConnectionSharing : False
Credentials                          :
UseDefaultCredentials                : False
ConnectionGroupName                  :
PreAuthenticate                      : False
Url                                  : http://wwwin/minosegugy/_vti_bin/Lists.a
 smx
RequestEncoding                      :
Timeout                              : 100000
Site                                 :
Container                            :

Resume

I know that we can use remote pws scripting in Powershell v2 and other webproxy possibilities.. but somehow this process is a bit closer to me.. i don’t know why ūüėõ

anyway no need to turn on the remote session at the destination (Sharepoint) server

In the near future i would like to post an article on how to use this method to manipulate rows in sharepoint list.

Reference

http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?ID=9 – Zach Rosenfield

And more

If you need more webservice just use some below:

Administration Service             :           http://<server-url:port-number>/_vti_adm/admin.asmx
Alerts Service                           :           http://<server-url>/_vti_bin/alerts.asmx
Document Workspace Service :           http://<server-url>/_vti_bin/dws.asmx
Forms Service                          :           http://<server-url>/_vti_bin
/forms.asmx
Imaging Service                       :           http://<server-url>/_vti_bin
/imaging.asmx
List Data Retrieval Service        :           http://<server-url>/_vti_bin/dspsts.asmx
Lists Service                            :           http://<server-url>/_vti_bin/lists.asmx
Meetings Service                     :           http://<server-url>/_vti_bin/meetings.asmx
Permissions Service                 :           http://<server-url>/_vti_bin/permissions.asmx
Site Data Service                      :           http://<server-url>/_vti_bin/sitedata.asmx
Site Service                              :           http://<server-url>/_vti_bin/sites.asmx
Users and Groups Service        :           http://<server-url>/_vti_bin/usergroup.asmx
Versions Service                      :           http://<server-url>/_vti_bin/versions.asmx
Views Service                           :           http://<server-url>/_vti_bin/views.asmx
Web Part Pages Service           :           http://<server-url>/_vti_bin/webpartpages.asmx
Webs Service                           :           http://<server-url>/_vti_bin/webs.asmx
Area Service                             :           http://<server-url>/_vti_bin/areaservice.asmx
Query Service                          :           http://<server-url>/_vti_bin/search.asmx
User Profile Service                  :           http://<server-url>/_vti_bin/userprofileservice.asmx
SPS Crawl Service                     :           http://<server-url>/_vti_bin/spscrawl.asmx
Outlook Adapter Service          :           http://<server-url>/_vti_bin/outlookadapter.asmx

HAVE FUN ūüôā

3 thoughts on “Connect Sharepoint remotely using Powershell”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s