cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
jared_flaherty
Community Contributor

Update Wiki Page HTML via API

Jump to solution

Heylo-

So, anybody know a way to update the HTML of a Wiki Page via the API?    

We created thousands of Practice Courses for our instructors when we switched to Canvas last year.    In one of the Wiki Pages, there is an iframe that has a google drive doc url placed in it.      Since google drive will no longer support web hosting starting August 31, 2016, I was hoping there was a way to rewrite the HTML for all of the pages via the API and replace the iframe url.

Thanks!

1 Solution

Accepted Solutions
James
Community Champion

 @jared_flaherty ​,

Yes.

If the pages have differing content, then you do a two-step approach. First get the page, make the changes, and then update the page. Note that when you fetch the page, the fields are not prefixed with wiki_page, but when you update them, they need to be. That is, you can't just take the downloaded object, modify it, and send it back. When you fetch the page, you want the body field. When you update it, store that in wiki_page[body]

If the pages have a consistent content that you can just overwrite without worrying about what was already on the page, then you can just update the page without fetching it first. Put the new body in wiki_page[body] parameter for the PUT.

Note that the body is the html that goes within the <body> ... </body> tags, but it does not include those tags.

Both the Show page and Update/create page API endpoints are documented with the Pages documentation.

You'll need to know the Course ID and URL for each page. The only piece you should need to return for an update is the wiki_page[body] field and it will leave the rest alone.

View solution in original post

4 Replies
James
Community Champion

 @jared_flaherty ​,

Yes.

If the pages have differing content, then you do a two-step approach. First get the page, make the changes, and then update the page. Note that when you fetch the page, the fields are not prefixed with wiki_page, but when you update them, they need to be. That is, you can't just take the downloaded object, modify it, and send it back. When you fetch the page, you want the body field. When you update it, store that in wiki_page[body]

If the pages have a consistent content that you can just overwrite without worrying about what was already on the page, then you can just update the page without fetching it first. Put the new body in wiki_page[body] parameter for the PUT.

Note that the body is the html that goes within the <body> ... </body> tags, but it does not include those tags.

Both the Show page and Update/create page API endpoints are documented with the Pages documentation.

You'll need to know the Course ID and URL for each page. The only piece you should need to return for an update is the wiki_page[body] field and it will leave the rest alone.

View solution in original post

jared_flaherty
Community Contributor

Thank you!     You are incredible...  just what I needed to know.  

Thanks!!!

tjackson
Community Participant

I'm wondering if you can help me. I'm teaching myself how to use some of this API stuff and I'm stuck. I can get a new page created but I cannot get the new page to contain text or update the page with text. I also would like to use some HTML rather than just straight text. I'm using Terminal on a MAC.

Example:

-d wiki_page[body]=“<p><a href="https://googledrive.com/host/0B7BTX-Tq5LgHQUhDTEdHb0hDcDQ/Bacterial%20Grow%20Lab.htm">Bacteria Grow Lab</a></p>”

Gives:

-bash: syntax error near unexpected token ‘<‘

Example:

-d wiki_page[body]=New+page+2

Gives:

a blank wiki page

Creating the pages is great but unless I can figure out where I'm going wrong on updating the text I'm not sure I'll be able to use this. Would you be able to offer some direction?

maguire
Community Champion

To get all of the pages you can use my python script:
cgetall.py canvas_course_page_url|course_id [destination_directory]

available from https://github.com/gqmaguirejr/Canvas-tools 

I have another program: cput.py canvas_course_page_url

to put pages back, it is basically just calling a function:

def put_course_page(canvas_course_page_url):
       # Use the Canvas API to GET the page
       #GET /api/v1/courses/:course_id/pages/:url

       #extract course_id from URL
       course_id=canvas_course_page_url[canvas_course_page_url.find("courses/")+8:canvas_course_page_url.find("pages/")-1]
       if Verbose_Flag:
              print("course_id: {}".format(course_id))

       #extract the file name portion of the URL
       page_url=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]
       if Verbose_Flag:
              print("page_url: {}".format(page_url))

       new_file_name=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]+'.html'
       if Verbose_Flag:
              print("new_file_name: {}".format(new_file_name))

       # read .html page
       with open(new_file_name, 'rb') as f:
              file_input=f.read()
              
       url = baseUrl + '%s/pages/%s' % (course_id, page_url)
       if Verbose_Flag:
              print(url)
       payload={"wiki_page[body]": file_input}
       r = requests.put(url, headers = header, data=payload)
       if Verbose_Flag:
              print("r.status_code: {}".format(r.status_code))
       if r.status_code == requests.codes.ok:
              page_response = r.json()
              pprint(page_response)
              return True
       else:
              print("No such page: {}".format(canvas_course_page_url))
              return False
       return False


To create pages - one can use a function such as:

# canvas_course_page_url will be of the form: https://kth.instructure.com/courses/11/pages/notes-20160716
def create_course_page(canvas_course_page_url, page_title):
       # Use the Canvas API to GET the page
       #GET /api/v1/courses/:course_id/pages/:url

       #extract course_id from URL
       course_id=canvas_course_page_url[canvas_course_page_url.find("courses/")+8:canvas_course_page_url.find("pages/")-1]
       if Verbose_Flag:
              print("course_id: {}".format(course_id))

       #extract the file name portion of the URL
       page_url=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]
       if Verbose_Flag:
              print("page_url: {}".format(page_url))

       new_file_name=canvas_course_page_url[canvas_course_page_url.rfind("/")+1:]+'.html'
       if Verbose_Flag:
              print("new_file_name: {}".format(new_file_name))

       # read .html page
       with open(new_file_name, 'rb') as f:
              file_input=f.read()
              
       # note that you cannot provide the page_url for the page - as this page_url does not yet exist
       url = baseUrl + '%s/pages' % (course_id)
       if Verbose_Flag:
              print(url)
       payload={'wiki_page[title]': page_title, 'wiki_page[published]': False, "wiki_page[body]": file_input}
       r = requests.post(url, headers = header, data=payload)
       if Verbose_Flag:
              write_to_log(r.text)    
       if Verbose_Flag:
              print("r.status_code: {}".format(r.status_code))
       if r.status_code == requests.codes.ok:
              page_response = r.json()
              if Verbose_Flag:
                     print("Created page")
                     pprint(page_response)
              return True
       elif r.status_code == requests.codes['unprocessable_entity']:
              # a list of all the status codes is at https://github.com/kennethreitz/requests/blob/master/requests/status_codes.py
              print("unprocessable_entity - probably because you specified a page_url, but this page does not yet exist")
              print("Error when creating page: {}".format(canvas_course_page_url))
              return False
       else:
              print("Error when creating page: {}".format(canvas_course_page_url))
              return False
       return False