How can I upload a document to SharePoint with Per

2020-07-14 12:46发布

I have a Perl app that runs some perforce operations, in the end I would like it to upload the results to SharePoint website.

  • What is the simplest Perl script that can accomplish a task of adding a document to SharePoint?

The script would need to run on Solaris and use as few as possible external libraries as possible (definitely pure classic Perl) getting anything additional installed on these unix boxes is a pain and would have to be done by remote team.

If this can uploading document can easily be done with wget, that would be of interest too. Anyways, I am looking for 1 or a couple liner that's easy to understand.

UPDATES based on comments:

  • Perl Mechanize sounded like a good idea, but for some reason I am not able to authenticate, Error GETing http://sharepoint Unauthorized ....

I had this:

my $m = WWW::Mechanize->new(); 
$m->credentials($user => $pass); 
$m->get($url);

But mechanize won't authenticate against sharepoint for some reason.

  • Does anybody have a link or a sample on how to use sharepoint webdav from unix via perl?

I installed and tried to open my typical sharepoint site via "dave" webdav browser, but I get ** Unauthorized. ** error.

  • How to solve it with the webdav approach with perl on unix?

5条回答
混吃等死
2楼-- · 2020-07-14 12:53

To make NTLM authentication work in WWW::Mechanize you need to use this format

use URI;
my $u    = URI->new($url);
my $host = $u->host;
my $port = $u->port;
my $hostport = "$host:$port";

$agent->$self->credentials($hostport, $realm, $user, $password);
查看更多
小情绪 Triste *
3楼-- · 2020-07-14 12:58

Can you use the Webdav interface? Each SharePoint list has a webdav folder associated with it.

查看更多
冷血范
4楼-- · 2020-07-14 13:03

Just found an easy way on windows from perlmonks forum:

http://www.perlmonks.org/?node_id=527182
under Windows, you can access a sharepoint site via a UNC name. The URL:
sharepoint.domain.dom/sites/Roboticus/Test
is accessible via:
\\sharepoint.domain.com\sites\Roboticus\Test

just adding as answer to myself, now I got to figure out how to script this from Perl and whether there is way to do the same from script running on unix.

查看更多
地球回转人心会变
5楼-- · 2020-07-14 13:12

You can connect to SharePoint (or any website) using curl, which is able to authenticate and perform negotiation with Kerberos/NTLM:

curl --ntlm -u domain/userid:passwd -T <file> "http://sharepoint URL" 
  • --ntlm
    (HTTP) Enables NTLM authentication. The NTLM authentication method was designed by Microsoft and is used by IIS web servers. It is a proprietary protocol, reverse-engineered by clever people and implemented in curl based on their efforts. This kind of behavior should not be endorsed, you should encourage everyone who uses NTLM to switch to a public and documented authentication method instead, such as Digest.

    If you want to enable NTLM for your proxy authentication, then use --proxy-ntlm.

    This option requires a library built with SSL support. Use -V, --version to see if your curl supports NTLM.

    If this option is used several times, only the first one is used.

  • -u, --user

    Specify the user name and password to use for server authentication. Overrides -n, --netrc and --netrc-optional.

    If you simply specify the user name, curl will prompt for a password.

    The user name and passwords are split up on the first colon, which makes it impossible to use a colon in the user name with this option. The password can, still.

    When using Kerberos V5 with a Windows based server you should include the Windows domain name in the user name, in order for the server to successfully obtain a Kerberos Ticket. If you don't then the initial authentication handshake may fail.

    When using NTLM, the user name can be specified simply as the user name, without the domain, if there is a single domain and forest in your setup for example.

    To specify the domain name use either Down-Level Logon Name or UPN (User Principal Name) formats. For example, EXAMPLE\user and user@example.com respectively.

    If you use a Windows SSPI-enabled curl binary and perform Kerberos V5, Negotiate, NTLM or Digest authentication then you can tell curl to select the user name and password from your environment by specifying a single colon with this option: "-u :".

    If this option is used several times, the last one will be used.

  • -T, --upload-file

    This transfers the specified local file to the remote URL. If there is no file part in the specified URL, Curl will append the local file name. NOTE that you must use a trailing / on the last directory to really prove to Curl that there is no file name or curl will think that your last directory name is the remote file name to use. That will most likely cause the upload operation to fail. If this is used on an HTTP(S) server, the PUT command will be used.

    Use the file name "-" (a single dash) to use stdin instead of a given file. Alternately, the file name "." (a single period) may be specified instead of "-" to use stdin in non-blocking mode to allow reading server output while stdin is being uploaded.

    You can specify one -T for each URL on the command line. Each -T + URL pair specifies what to upload and to where. curl also supports "globbing" of the -T argument, meaning that you can upload multiple files to a single URL by using the same URL globbing style supported in the URL, like this:

    curl -T "{file1,file2}" http://www.uploadtothissite.com

    or even

    curl -T "img[1-1000].png" ftp://ftp.picturemania.com/upload/

查看更多
▲ chillily
6楼-- · 2020-07-14 13:13

This sounds like a job for WWW::Mechanize. It has excellent support for dealing with forms.

查看更多
登录 后发表回答