Coder Social home page Coder Social logo

python-sharepoint's Introduction

python-sharepoint

A Python library and command-line utility for gettting data out of SharePoint.

If you're more a Perl person, you might also want to try SharePerltopus.

Installation

Either install the latest development from git:

$ git clone git://github.com/ox-it/python-sharepoint.git
$ cd python-sharepoint
$ sudo python setup.py install

... or, install the latest stable version using pip:

$ pip install sharepoint

You will need lxml, which you can install using your package manager or pip. Run one of the following if it's not already installed:

$ sudo apt-get install python-lxml  # Debian, Ubuntu
$ sudo yum install python-lxml      # RedHat, Fedora
$ sudo pip install lxml             # pip

Usage

First, you need to create a SharePointSite object. We'll assume you're using basic auth; if you're not, you'll need to create an appropriate urllib2 Opener yourself.

from sharepoint import SharePointSite, basic_auth_opener

server_url = "http://sharepoint.example.org/"
site_url = server_url + "sites/foo/bar"

opener = basic_auth_opener(server_url, "username", "password")

site = SharePointSite(site_url, opener)

Lists

First, get a list of SharePoint lists available:

for sp_list in site.lists:
    print sp_list.id, sp_list.meta['Title']

You can look up lists by ID, or by name:

# By ID, without braces
print site.lists['1EF5668C-0AB4-4020-98EF-26325E412C3C']
# By ID, with braces
print site.lists['{1EF5668C-0AB4-4020-98EF-26325E412C3C}']
# By name
print site.lists['ListName']

Given a list, you can iterate over its rows:

sp_list = site.lists['ListName']
for row in sp_list.rows:
    print row.id, row.FieldName

rows is a list, which doesn't help you if you want to find rows by their SharePoint row IDs. For this use a list's rows_by_id attribute, which contains a mapping from row ID to row.

You can assign to fields as one would expect. Values will be coerced in mostly-sensible ways. Once you're done, you'll want to sync your changes using the list's save() method:

sp_list = site.lists['ListName']

# Set both the URL and the text
sp_list.rows[5].Web_x0020_site = {'url': 'http://example.org/',
                                  'text': 'Example Website'}
# Set the URL; leave the text blank
sp_list.rows[6].Web_x0020_site = 'http://example.org/'
# Clear the field
sp_list.rows[7].Web_x0020_site = None

sp_list.save()

Consult the descriptor_set() methods in sharepoint.lists.types module for more information about setting SharePoint list fields.

Document libraries

Support for document libraries is limited, but SharePointListRow objects do support a is_file() method and an open() method for accessing file data.

Command-line utility

Here's how to get a list of lists from a SharePoint site:

$ sharepoint lists -s http://sharepoint.example.org/sites/foo/bar \
             -u username -p password

And here's how to get one or more lists as XML:

$ sharepoint exportlists -s http://sharepoint.example.org/sites/foo/bar \
             -l FirstListName -l "Second List Name" \
             -u username -p password

You can also specify a file containing username and password in the format 'username:password':

$ sharepoint --credentials=path/to/credentials [...]

If you want to manipulate SharePoint sites from a Python shell, use the shell command:

$ sharepoint shell -s http://sharepoint.example.org/sites/foo/bar \
             -u username -p password

Once you're in the Python shell, there will be a site variable for the site you specified. See above for things to do with your site.

For help (including to see more options to configure the output, use -h:

$ sharepoint -h

python-sharepoint's People

Contributors

alexdutton avatar cchurch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-sharepoint's Issues

Always Using Default View

It's only getting the records from the default view when I try to access a list in Sharepoint 2010.

The error is probably around line 175 in:
python-sharepoint / sharepoint / lists / init.py

ImportError: DLL load failed

Hello everyone,
I am trying to use this library to connect to sharepoint.
I have installed sharepoint and I already head lxml as shown below:

image

I am working in Spyder and every time I execute the line: from sharepoint import SharePointSite,basic_auth_opener
I get the same error:

"from lxml import etree"

ImportError: DLL load failed: No se encontró el proceso especificado.

image

Can please anybody help me?

Connect to sharepoint using auth cookies

I need to connect to sharepoint using auth cookies - rTFa and FedAuth.

Can you share the substitute command for this :
opener = basic_auth_opener(server_url, "username", "password")

Problem using hasattr() and getattr() to SharePointListRow objects

Both functions - hasattr() andgetattr() as welll - result in infinite loops when accessing them.

example code:

for sp_list in sharepoint.lists[list_id].rows:
    if hasattr(sp_list, 'attribute'):
        value = sp_list.attribute

I haven't had a look into SharePointListRow but I'm pretty sure that the class contains a big problem when dealing with those two standard functions.

Maybe someone can try this and verify the behavior...

Does this still work with Python 3

I appear to be getting the following error

"TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"

Which appears to be related to urllib3.

bin/sharepoint error

When trying to use the command line script "sharepoint" (in Windows) I was getting an error that the "module cmd was not found."

After a lot of investigation I realized the problem was happening because the script is the same name as the package. It was importing itself and getting confused.

This happens I believe because I was in the Python\Scripts folder, as it is not on my path. Seems to work from other folders.

Usage example; what is site_url?

Hi,

Thanks for writing python-sharepoint. I need to read a table from a sharepoint site.
What should I enter for site_url in the usage example?
The URL for the table is https://mysite.example.com/personal/dom1_123444/MyTable/default.aspx
Before I visit this page from Site Content, and move my mousepointer over the link, I can see the javascript function that is going to get executed by clicking it. It has the following parameters:
30:WebNode:34jhkjf-23rfndf-23rfnfj, \u002fpersonal\u002fdom1_123444\u002fMyTable

I tried to execute the example with the first URL I mentioned, and got a "HTTP Error 401: Unauthorized " in self.opener.open(request) when iterating.

So maybe there is another problem with the authentication, since I use https?

Thanks a lot,
joe

Failure when list contains a field named 'list'

I have a field with this definition (notice the Name and StaticName are both list):

<Field 
        xmlns="http://schemas.microsoft.com/sharepoint/soap/" 
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        Type="Text"
        DisplayName="list" 
        Required="TRUE" 
        EnforceUniqueValues="TRUE" 
        Indexed="TRUE" 
        MaxLength="255"
        ID="{some-very-long-id}" 
        SourceID="{some-very-long-source-id}" 
        StaticName="list"
        Name="list" 
        ColName="nvarchar6" 
        RowOrdinal="0"
/>

This breaks in various ways because it ends up replacing the SharePointListRow.list attribute which is otherwise a reference back to the SharePointList instance.

As a start, an assert could be added to not accept fields named fields, list or opener:

self._fields[field.name] = field

A better solution would probably be to rename the list attribute to something else.

There are probably other "dangerous" field names to avoid or work around.

I know other projects handle this by either collecting all default instance attributes under a single instance object that is unlikely to cause problems (e.g. SharePointListRow()._meta.parent_list) or by having logic in the __getattr__ to distinguish between class and instance attribute access.

Update URLField to handle file urls

I work in a corporate sharepoint site and we have list content linked back to actual files on our network (more of just a reference list than a live edited list). Issue is that the hyperlinks are linked to href='file://...' links.

I ended up editing the lists/types.py file, URLField class to accept "file:" url links.
was:
...value['href'].startswith(prefix) for prefix in ('mailto:', 'http:', 'https:')
is:
...value['href'].startswith(prefix) for prefix in ('mailto:', 'http:', 'https:', 'file:')

Any reason why this couldn't be standard behavior moving forward?

"pip install sharepoint" broken

(test-sp) C:\repos\test\code\python\sp>pip install sharepoint
Downloading/unpacking sharepoint
Running setup.py egg_info for package sharepoint
Traceback (most recent call last):
File "", line 16, in
File "C:\Users\deniz.dogan.virtualenvs\test-sp\build\sharepoint\setup.py", line 10, in
long_description=open('README.rst').read(),
IOError: [Errno 2] No such file or directory: 'README.rst'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "", line 16, in

File "C:\Users\deniz.dogan.virtualenvs\test-sp\build\sharepoint\setup.py", line 10, in

long_description=open('README.rst').read(),

IOError: [Errno 2] No such file or directory: 'README.rst'

Command python setup.py egg_info failed with error code 1 in C:\Users\deniz.dogan.virtualenvs\test-sp\build\sharepoint
Storing complete log in C:\Users\deniz.dogan\pip\pip.log

open()

is_file returns True, but when I call open() I get a 400 error. If I copy-paste the url from the exception to my browser it works. any ideas?

error iterating over rows - no attribute 'FieldName'

following the documentation and running (after successfully connecting and setting sp_list = site.lists['sitename']):

for row in sp_list.rows:
    print row.id, row.FieldName

returns this error:

AttributeError: 'SharePointListRow' object has no attribute 'FieldName'

Any ideas on this one, or am I missing something?

Alwasy get urllib2.HTTPError: HTTP Error 403: Forbidden

Hi. This is how I use the code. And I always get the 403 error. In the web browser, I always use the email account to login. I don't know the username here is the Email address I used to login the www.office.com website? The username I used is [email protected]. Is this right? Or I should use some translated names like pku_edu_cn\\xxx?

from sharepoint import SharePointSite, basic_auth_opener

server_url = "https://pkueducn-my.sharepoint.com/"
site_url = server_url + "personal/xxx_pku_edu_cn"

opener = basic_auth_opener(server_url, "[email protected]", "my_password")

site = SharePointSite(site_url, opener)

for sp_list in site.lists:
    print sp_list.id, sp_list.meta['Title']

My error:

C:\Python27\python.exe J:/github_repos/xxx/sp.py
Traceback (most recent call last):
  File "J:/github_repos/xxx/sp.py", line 17, in <module>
    for sp_list in site.lists:
  File "C:\Python27\lib\site-packages\sharepoint\lists\__init__.py", line 80, in __iter__
    return iter(self.all_lists)
  File "C:\Python27\lib\site-packages\sharepoint\lists\__init__.py", line 36, in all_lists
    result = self.opener.post_soap(LIST_WEBSERVICE, xml)
  File "C:\Python27\lib\site-packages\sharepoint\site.py", line 32, in post_soap
    response = self.opener.open(request, timeout=self.timeout)
  File "C:\Python27\lib\urllib2.py", line 435, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

Process finished with exit code 1

Building own urllib2 handler

I got a problem while building my own urllib2 opener because I may need a proxy sometimes.

I did this:

from sharepoint import SharePointSite
from sharepoint.auth import PreemptiveBasicAuthHandler

# copy'n'paste from auth.py :)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, "https://server/", "server\user", "myPass")
auth_handler = PreemptiveBasicAuthHandler(password_manager)

ph = urllib2.ProxyHandler( { } )
opener = urllib2.build_opener(ph, auth_handler)

site = SharePointSite(sharepoint_url, opener)

But it ends up giving me a 401 HTTP status - the credentials are valid, I double checked that. I'm not into sharepoint a lot, but I assume that it's a problem with NTLM authentication as all browsers work very well with the same credentials.

NTLM authentication

Doest this library provides NTLM authentication? The site in which I'm trying to use has NTLM authentication!

list.rows doesn't iterate over all rows

I'm getting massive discrepancies between the meta ItemCount (which is correct when viewing in sharepoint) and traversing the rows:

sp_list = site.lists['Support Database']

print(sp_list.meta['ItemCount'])

index = 1

for row in sp_list.rows:
    index += 1

print(index)

In this instance, meta reports 11470. The index shows 97! There are no warnings or exceptions thrown. I'm about at my wits end.

Fails on nested folders

i'm attempting to access documents in subfolders of "Shared Documents". However, this fails as only the top level folder gets added to the opened url.

Minimal example:

s = site.lists['Shared Documents']
r = s.rows_by_id[2310] # because I can't find a way to navigate folders...
print r.ServerUrl
# Correct: /sites/cq/Shared Documents/Teams/Foo/Bar/file.csv
f = r.open()
print f.geturl()
# Missing Foo/Bar folder structure: https://server.com/sites/cq/Shared%20Documents/file.csv

Is there a way to navigate directories? e.g.
s.lists gives the same result as site.lists.

Unknown calculated type

/usr/local/lib/python2.7/dist-packages/sharepoint/lists/types.py:381: UserWarning: Unknown calculated type 'string'
warnings.warn("Unknown calculated type '%s'" % type_name)

Add subfolders and items into subfolders

Im trying to add a subfolder to a existing folder in the root directory. But instead of appering in the folder, the subfolder appears in the root directory.

My code:
new_row = list.Row()
new_row.ContentType = 'Folder'
new_row.Title = name
new_row.BaseName = name
new_row._EditMenuTableStart = name
new_row.EncodedAbsUrl = self.complete_url+ "/" + url + "/" + name
new_row.ServerUrl = self.site_url +"/"+ url + "/" + name
new_row.LinkFilenameNoMenu = name
new_row.LinkFilename = name
list.append(new_row)
list.save()

Is this feature not implemented or am i doing something wrong here?

traversing through multiple folders

I got this problem while fetching the list of rows inside a folder.

This is the structure of my sharepoint.

A -> F ->E->test.doc
B
C
D
E
(A-E -> all are folders)

server_url = "https://server/"
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, server_url, 'username', 'password')
auth_handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
site = SharePointSite(server_url, opener)
shared_docs = site.lists.all_lists[0] # to get the first folder A
shared_docs_rows = shared_docs.rows
vow_action_plan = shared_docs_rows[0] # to get the Folder F

Now i want to go inside the Folder E and get the list of documents in the Folder E but i am unable to do so.

mutli line records on sp issue

Hi, I notice that for records on the list on sp where there is multiple lines this information is not available when accessing through this library - there is any notice that there was new line on sharepoint

Disable ssl verification

Hello!

I am trying to access our sharepoint system but I fail to login due to an ssl verification issue. How do I temporarily disable ssl verification in your library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.