1

I am creating a python script that needs to create Folder Directories, and upload files into a SharePoint sub-site document library. I currently am using Sharepy for authentication, and the REST API for the requests.

Oddly enough, posting to the main SharePoint site works successfully. I am authorized to upload files and create folders without an issue. When I move to sub-sites using the API call similar to below:

 p = s.post("https://"+SPUrl+"/sites/"+siteName+"/_api/web/folders",
    json={
        "__metadata": { "type": "SP.Folder" },
        "ServerRelativeUrl": libraryName +'/' +destFolder[-1]
        })

(where s is the sharepy object) I get an access denied message.

For reference, the credentials used are that of a global administrator, or SharePoint administrator. And both work at the main site.

Below is also how I am looking to upload a singular file:

 p = s.post(f"https://{SPUrl}/sites/{siteName}/_api/web/GetFolderByServerRelativeUrl('{libraryName}/{destFolder[-1]}')/Files/add(url='{fileToUpload}',overwrite=true)", data=content, headers=headers)

I have done a lot of research and searched forums, but nothing that I have found seems remotely similar to my issue.

I am very new to SharePoint and the REST API, and any help in resolving my issue would be most appreciated.

My Question: How exactly do I connect to a SharePoint sub-site using the REST API in order to upload documents and create Files. In my code I attempt to do this but get privilege errors regardless of success in the main site. I would like to know if my code is invalid, or perhaps there are restrictions on what you can do with sub-sites. (or a special setting that might need to be changed on SharePoint's side.)

4
  • Welcome to SharePoint Stack Exchange. This sounds like an interesting issue. But I don't see a question in your question. What are you wanting the community to answer?
    – Jammin4CO
    Commented Sep 7, 2018 at 20:09
  • 1
    @Jammin4CO Thank you for looking into this. To answer, my question is how exactly do I connect to a Sharepoint Subsite using the REST API in order to upload documents and create Files. In my code I attempt to do this but get privilege errors. I would like to know if my code is invalid, or perhaps there are restrictions on what you can do with sub-sites. I hope that makes sense and will be sure to edit the question. Commented Sep 7, 2018 at 20:14
  • This question can't be answered based on what you've given us. SharePoint doesn't think you're authorized to do what you're trying to do. So either your SharePy auth setup is wrong, or you don't actually have access to those resources. If you're getting a 401 in the response then SharePoint can't figure out who you are (not authenticated). If you're getting 403 then it knows who you are but thinks you're not allowed to access the resources (not authorized). Are you able to Fiddle the underlying traffic? Commented Sep 7, 2018 at 20:42
  • @DerekGusoff Thank you for your response. I am able to add files, and folders when talking about the main site (ie www.examplesite.sharepoint.com) so I know that I am properly authenticated. When referring to Subsites, I simply only want to confirm that my REST request seems valid with Sharepoint syntax. The reason the response from Microsoft is misleading, is that it will give you the same response if a directory doesn't exist. So it has nothing to do with your permissions as it did your own personal error. If I know my request is right (syntactically), I know where to look. Commented Sep 7, 2018 at 20:57

1 Answer 1

0

as an update to this, it was not a problem with the Authentication or User access privileges, it was simply how the API was being called.

For example, I was doing the following:

p = s.post("https://"+SPUrl+"/sites/"+siteName+"/_api/web/folders",
json={
    "__metadata": { "type": "SP.Folder" },
    "ServerRelativeUrl": libraryName +'/' +destFolder[-1]
    })

Where the document library was set as "Documents"... This is correct only on the root directory. When looking at sub-sites (in the api XML that can be evaluated) you can see that it is actually listed as "Shared%20Documents" So the 403 I was getting actually pertained to a document library that didn't exist. (Or that SharePoint did not know how to recognize) You can find out what type of values are expected by simply going to

https://<siteURL>.sharepoint.com/sites/<Your subsite>/_api/web/folders 

and review the XML in your browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.