Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add & delete attachments possibility #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pierrejochem
Copy link

@alexsdutton @cchurch @chris-bradley Please have a look.

@cchurch
Copy link
Contributor

cchurch commented Aug 30, 2016

@pierrejochem Unfortunately (or maybe fortunately?) I haven't had to use this library in awhile, and don't have access to a SharePoint environment to test. However, any implementation seems better than raising NotImplementedError to me.

@pierrejochem
Copy link
Author

But I have 😄 Great work from you guys!

@pierrejochem
Copy link
Author

pierrejochem commented Aug 31, 2016

@cchurch I could contribute to the Project. If this is possible?

@pierrejochem
Copy link
Author

@cchurch I would make the merges and testing

@bash-j
Copy link

bash-j commented Sep 26, 2016

@pierrejochem I ended up writing the exact same code for the add function. When I try to add an attachment using a str object as the content I receive a vague error:
``File "", line unknown
XMLSyntaxError

@pierrejochem
Copy link
Author

Hello @bash-j

I need more Details. Could you please try first my version from here:

https://github.com/pierrejochem/python-sharepoint

@bash-j
Copy link

bash-j commented Sep 27, 2016

Hi @pierrejochem

I found the content returned by the request was empty because the server was returning a 400 error, so lxml had nothing to parse. I discovered SharePoint is expecting a base64 array. After a lot of trial and error I found the following code works with a string, a text file and binary files.

def add(self, filename=None, content=None, upload_file=None):
        if upload_file:
            if not filename:
                filename = upload_file
            with open(upload_file, "rb") as filein:
                f = filein.read()
                econtent = base64.b64encode(f).decode()
        else:
            econtent = base64.encodebytes(content.encode()).decode()
        xml = SP.AddAttachment(SP.listName(self.list_id),
                               SP.listItemID(str(self.row_id)),
                               SP.fileName(filename),
                               SP.attachment(econtent))
        response = self.opener.post_soap(LIST_WEBSERVICE,
                                         xml,
                                         soapaction='http://schemas.microsoft.com/sharepoint/soap/AddAttachment')

@pierrejochem
Copy link
Author

Sorry, yes of course:

https://msdn.microsoft.com/en-us/library/lists.lists.addattachment(v=office.12).aspx

attachment => A byte array that contains the file to attach by using base-64 encoding.

It was self-evidently for me so I have not ask for it. But you found it by yourself and that is important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants